How To Add Bill In Quickbooks Online Using C#

In this article, we will learn how to add a bill 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.

VendorRef and Line elements are required for creating a bill, we will use these two required elements for creating a bill.

We have to give the vendor reference in VendorRef, and account reference in Line.

Account must be already added in Quickbooks before adding it to the bill.

Let’s create a Bill.

  • 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 a Bill object and assign data into an object, the following are some required data information that we have to give.
    1. VendorRef: we have to give Quickbooks online Vendor ID as a reference for the vendor.
    2. Line: we have to give account details as a reference for the bill item or line item. for that we have to specify Quickbooks online Account ID or Account Name or Both, DetailType and Amount. if we provide both then QuickBooks will take Account ID and ignore Account Name. in DetailType we need to Set AccountBasedExpenseLineDetail for this type of line. in Amount, we need to set the amount of the line item.
  • After that, We have to create a DataService object by passing a ServiceContext object as a parameter.
  • Add Bill object in DataService.Add() for adding a bill.
  • If a bill created successfully, it will return the newly-created bill object, from this object you can store details like bill Id in the database if you want to.
  • The code is as below.
public ActionResult CreateBill()
{
  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;

    Bill ObjBill = new Bill();

    ObjBill.VendorRef = new ReferenceType();
    ObjBill.VendorRef.Value = "60";//Quickbooks online Vendor Id
    
    List<Line> LineList = new List<Line>();

    Line objLine = new Line();
    objLine.DetailTypeSpecified = true;
    objLine.DetailType = LineDetailTypeEnum.AccountBasedExpenseLineDetail;

    objLine.AmountSpecified = true;
    objLine.Amount = 100;

    AccountBasedExpenseLineDetail ItemLineDetail = new AccountBasedExpenseLineDetail();
    ItemLineDetail.AccountRef = new ReferenceType();
    ItemLineDetail.AccountRef.Value = "78"; //Quickbooks online Account Id
    // We can give Account Name insted of Account Id, if we give Account Id and Account Name both then Account name will be ignore.
    //ItemLineDetail.AccountRef.name = "Purchases"; //Quickbooks online Account Name

    objLine.AnyIntuitObject = ItemLineDetail;

    LineList.Add(objLine);

    ObjBill.Line = LineList.ToArray();

    DataService dataService = new DataService(serviceContext);

    Bill BillAdd = dataService.Add(ObjBill);
    if (BillAdd != null && !string.IsNullOrEmpty(BillAdd.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)
  {
    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,Exception occurs";
    }
    return View();
  }
}
  • View code is as below
@{
    ViewBag.Title = "CreateBill";
}

<h2>Create Bill</h2>

@if (ViewBag.IsSuccess != null && ViewBag.IsSuccess == true)
{
    <div class="row">
        <label class="label label-success"> Bill 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 add a bill in Quickbooks Online. we can define more than one line item and add it in LineList List, it will add a list of items in the bill.
  • Before Adding the bill, we have to make sure one thing that the Vendor ID and Account ID we gave as a reference must exist on Quickbooks online.
  • You can also query/get Vendor and Account before adding the bill and then add it references. or you can store it in the database at the time of add Vendor/Account and use it accordingly.
  • So this way we can add a bill in Quickbooks Online.

OUTPUT:(bill preview on Quickbook online)

Submit a Comment

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

Subscribe

Select Categories