In this article, we will learn how to add a purchase order 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, Line and APAccountRef elements are required for creating a purchase order, we will use these required elements for creating a purchase order.

We have to give the vendor reference in VendorRef, items or item reference in Line, and account reference in APAccountRef.

Vendor, Items, and Account must be already added in Quickbooks before adding it to the purchase order.

Let’s create a Purchase Order.

  • 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 Purchase Order 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 items details as a reference for the purchase order item or line item, we need to provide at least one item reference.for that we have to specify Quickbooks online Item ID. along with that, we have to provide Amount, Qty, CustomerRef(Customer ID or Customer Name as a reference), DetailType as ItemBasedExpenseLineDetail.
    3. APAccountRef: Use Account ID or Account Name or both for giving reference of account. The specified account must-have of the type “Accounts Payable“.It is used for Specifies to which AP account the bill is credited.
  • After that, We have to create a DataService object by passing a ServiceContext object as a parameter.
  • Add Purchase Order object in DataService.Add() for adding a Purchase Order.
  • If a Purchase Order created successfully, it will return the newly-created Purchase Order object, from this object you can store details like Purchase Order Id in the database if you want to.
  • The code is as below.
public ActionResult CreatePurchaseOrder()
{
  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;

    PurchaseOrder ObjPurchaseOrder = new PurchaseOrder();

    ObjPurchaseOrder.APAccountRef = new ReferenceType();
    ObjPurchaseOrder.APAccountRef.Value = "33"; //Quickbooks online Accounts Id of type Accounts Payable
    // We can give Accounts Name insted of Accounts Id, if we give Accounts Id and Accounts Name both then Accounts name will be ignore.
    //ObjPurchaseOrder.APAccountRef.name = "Accounts Payable (A/P)";//Quickbooks online Accounts Name

    ObjPurchaseOrder.VendorRef = new ReferenceType();
    ObjPurchaseOrder.VendorRef.Value = "60";//Quickbooks online Vendor Id
    // We can give Vendor Name insted of Vendor Id, if we give Vendor Id and Vendor Name both then Vendor name will be ignore.
    //ObjPurchaseOrder.VendorRef.name = "Tabish Vendor";//Quickbooks online Vendor Name

    List<Line> LineList = new List<Line>();

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

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

    ItemBasedExpenseLineDetail itemLineDetail = new ItemBasedExpenseLineDetail();
    itemLineDetail.ItemRef = new ReferenceType();
    itemLineDetail.ItemRef.Value = "19"; //Quickbooks online Item Id
    // We can give Item Name insted of Item Id, if we give Item Id and Item Name both then Item name will be ignore.
    //itemLineDetail.ItemRef.name = "Vision Keyboard Mouse Set"; //Quickbooks online Item name

    itemLineDetail.CustomerRef = new ReferenceType();
    itemLineDetail.CustomerRef.Value = "58"; //Quickbooks online Customer Id
    // We can give Customer Name insted of Customer Id, if we give Customer Id and Customer Name both then Customer name will be ignore.
    //itemLineDetail.CustomerRef.name = "Tabish Rangrej"; //Quickbooks online Customer Name

    itemLineDetail.QtySpecified = true;
    itemLineDetail.Qty = 1;

    objLine.AnyIntuitObject = itemLineDetail;
    
    LineList.Add(objLine);

    ObjPurchaseOrder.Line = LineList.ToArray();

    DataService dataService = new DataService(serviceContext);

    PurchaseOrder PurchaseOrderAdd = dataService.Add(ObjPurchaseOrder);
    if (PurchaseOrderAdd != null && !string.IsNullOrEmpty(PurchaseOrderAdd.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 = "CreatePurchaseOrder";
}

<h2>Create Purchase Order</h2>


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

OUTPUT:(Purchase Order preview on Quickbook online)