How To Add Item In Quickbooks Online Using C#

In this blog, I will give a little description of Quickbooks items and we will learn how to add an item in Quickbooks online from .NET MVC web application using SDK.

Before using any Quickbooks online API we need access token, if you don’t know how to get access token then you can find it here.

Now, let talk about Quickbooks online Items.

  • An item is a thing that we buy, sell, or re-sell, such as products and services.
  • There are three types of items in Quickbooks online
  1. Inventory: its goods which the company sells and buys and tracked as inventory.
  2. Service: its non-tangible goods the company sells and buys, that are not tracked as inventory. For example professional fees.
  3. NonInventory: its goods the company sells and buys that are not tracked as inventory. For example office supplies.

In this blog, We are adding an Inventory type item.

  • A minimum required element for creating an item is as below,
  1. Name: Unique name of the item
  2. Type: specify the item type.
  3. IncomeAccountRef: Must be an account of type “Sales of Product Income”, this element is required for Inventory and Service item types.
  4. AssetAccountRef: Must be an account of type “Other Current Asset“, this element is required for the Inventory item type.
  5. ExpenseAccountRef: Must be an account of type “Cost of Goods Sold“, this element is required for Inventory, NonInventory, and Service item types.
  6. InvStartDate: Date of opening balance for the inventory transaction, this element is required for the Inventory item type. if we define this element then InvStartDateSpecified is required and we have to set it to true.
  7. QtyOnHand: number of quantity on hand, this element is required for the Inventory item type. if we define this element then TrackQtyOnHand, TrackQtyOnHandSpecified, QtyOnHandSpecified are required and we have to set it to true.

Let’s create an Inventory type item.

  • First, we have to create a ServiceContext with Auth tokens and realmId.
  • For that, we need access token and realmId
  • Then, we have to create an Item object and assign data into an object.
  • in Item object, we have to give account reference to IncomeAccountRef, AssetAccountRef, and ExpenseAccountRef.
  • So first we will get a list of all accounts from Quickbooks, and then we will select an account from a list according to our requirements for providing a reference.
  • After assigning data into an Item object, We have to create a DataService object by passing a ServiceContext object as a parameter.
  • Add Item object in DataService.Add() for adding an Item.
  • If an Item created successfully, it will return the newly created Item object, from this object we can store details like Id in the database if you want to.
  • The code is as below.
public ActionResult CreateItem()
{
  try
  {
    OAuth2RequestValidator oauthValidator = new OAuth2RequestValidator(Access_token);

    // Create a ServiceContext with Auth tokens and realmId
    ServiceContext serviceContext = new ServiceContext(RealmId, IntuitServicesType.QBO, oauthValidator);
    serviceContext.IppConfiguration.MinorVersion.Qbo = "23";
    serviceContext.IppConfiguration.BaseUrl.Qbo = QboBaseUrl;

    Item ObjItem = new Item();

    ObjItem.Name = "Vision Keyboard";

    ObjItem.TypeSpecified = true;
    ObjItem.Type = ItemTypeEnum.Inventory;

    ObjItem.TrackQtyOnHand = true;
    ObjItem.TrackQtyOnHandSpecified = true;
    ObjItem.QtyOnHandSpecified = true;
    ObjItem.QtyOnHand = 10;

    ObjItem.InvStartDateSpecified = true;
    ObjItem.InvStartDate = DateTime.Now;

    ObjItem.Description = "This Keyboard is made by vision infotech";
    ObjItem.UnitPriceSpecified = true;
    ObjItem.UnitPrice = 100;

    ObjItem.PurchaseDesc = "This Keyboard is purchase from Vision";
    ObjItem.PurchaseCostSpecified = true;
    ObjItem.PurchaseCost = 50;

    // Create a QuickBooks QueryService using ServiceContext for getting list of all accounts from Quickbooks
    QueryService<Account> querySvc = new QueryService<Account>(serviceContext);
    var AccountList = querySvc.ExecuteIdsQuery("SELECT * FROM Account").ToList();

    //Get Account of type "OtherCurrentAsset" and named "Inventory Asset" for Asset Account Reference
    var AssetAccountRef = AccountList.Where(x => x.AccountType == AccountTypeEnum.OtherCurrentAsset && x.Name == "Inventory Asset").FirstOrDefault();
    if (AssetAccountRef != null)
    {
        ObjItem.AssetAccountRef = new ReferenceType();
        ObjItem.AssetAccountRef.Value = AssetAccountRef.Id;
    }
    else
    {
        ViewBag.IsSuccess = false;
        ViewBag.Message = "Account of Type OtherCurrentAsset Does not found in QBO, We must have at least one Account which is Type of OtherCurrentAsset for Refrence";
        return View();
    }

    //Get Account of type "Income" and named "Sales of Product Income" for Income Account Reference
    var IncomeAccountRef = AccountList.Where(x => x.AccountType == AccountTypeEnum.Income && x.Name == "Sales of Product Income").FirstOrDefault();
    if (IncomeAccountRef != null)
    {
        ObjItem.IncomeAccountRef = new ReferenceType();
        ObjItem.IncomeAccountRef.Value = IncomeAccountRef.Id;
    }
    else
    {
        ViewBag.IsSuccess = false;
        ViewBag.Message = "Account of Type Income Does not found in QBO, We must have at least one Account Name as 'Sales of Product Income' which is Type of Income for Refrence";
        return View();
    }

    //Get Account of type "CostofGoodsSold" and named "Cost of Goods Sold" for Expense Account Reference
    var ExpenseAccountRef = AccountList.Where(x => x.AccountType == AccountTypeEnum.CostofGoodsSold && x.Name == "Cost of Goods Sold").FirstOrDefault();
    if (ExpenseAccountRef != null)
    {
        ObjItem.ExpenseAccountRef = new ReferenceType();
        ObjItem.ExpenseAccountRef.Value = ExpenseAccountRef.Id;
    }
    else
    {
        ViewBag.IsSuccess = false;
        ViewBag.Message = "Account of Type CostofGoodsSold Does not found in QBO, We must have at least one Account Name as 'Cost of Goods Sold' which is Type of CostofGoodsSold for Refrence";
        return View();
    }

    DataService dataService = new DataService(serviceContext);

    Item ItemAdd = dataService.Add(ObjItem);
    if (ItemAdd != null && !string.IsNullOrEmpty(ItemAdd.Id))
    {
        //you can write Database code here
        ViewBag.IsSuccess = true;
    }
    return View();
  }
  catch (IdsException ex)
  {
    ViewBag.IsSuccess = false;
    if(ex.InnerException!=null && !string.IsNullOrEmpty(ex.InnerException.Message))
    {
        ViewBag.Message = ex.InnerException.Message;
    }
    else if (!string.IsNullOrEmpty(ex.Message))
    {
        ViewBag.Message = ex.Message;
    }
    else
    {
        ViewBag.Message = "Something went wrong,IdsException occurs";
    }
    return View();
  }
  catch (Exception ex)
  {
    return View();
  }
}
  • View Code is as below,
@{
    ViewBag.Title = "CreateItem";
}

<h2>Create Item</h2>

@if (ViewBag.IsSuccess != null && ViewBag.IsSuccess == true)
{
    <div class="row">
        <label class="label label-success"> Item Created Successfully </label>
    </div>
}
else if (ViewBag.IsSuccess != null && ViewBag.IsSuccess == false)
{
    <div class="row">
        <label class="label label-danger">@ViewBag.Message</label>
    </div>
}

The above code will create an item named “Vision Keyboard” of Inventory type, we can define Type according to our requirements like Service, NonInventory.

 

Submit a Comment

Your email address will not be published. Required fields are marked *

Subscribe

Select Categories