How To Update Purchase Order In Quickbooks Online Using C#

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

  • First, we have to create a ServiceContext with Auth tokens and realmId.
  • For that, we need access token and realmId
  • We have to get the purchase order from Quickbooks online by calling purchase order API.
  • We are querying a purchase order by ID.
  • For querying/get the purchase order we have to define QueryService
  • We need to pass the ServiceContext object into QueryService.
  • We must need purchase order Id and SyncToken for an update.
  • We will create a purchase order object and pass all data with updated data along with Id and SyncToken.
  • After that, We have to create a DataService object by passing a ServiceContext object as a parameter.
  • Add purchase order object in DataService.Update<PurchaseOrder>() for updating an purchase order.
  • It will return the newly updated purchase order object, you can store the required details to the database according to your needs.
  • The code is as below.
public ActionResult UpdatePurchaseOrder()
{
  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;

    string EXISTING_PURCHASEORDER_QUERYBYID = string.Format("select * from PurchaseOrder where id = '{0}'", "155");
    var queryService = new QueryService<PurchaseOrder>(serviceContext);
    PurchaseOrder objPurchaseOrderFound = queryService.ExecuteIdsQuery(EXISTING_PURCHASEORDER_QUERYBYID).FirstOrDefault<PurchaseOrder>();

    //If Purchase Order found on Quickbooks online
    if (objPurchaseOrderFound != null)
    {
        PurchaseOrder ObjPurchaseOrder = new PurchaseOrder();

        ObjPurchaseOrder.Id = objPurchaseOrderFound.Id;
        ObjPurchaseOrder.SyncToken = objPurchaseOrderFound.SyncToken;

        ObjPurchaseOrder.APAccountRef = new ReferenceType();
        ObjPurchaseOrder.APAccountRef = objPurchaseOrderFound.APAccountRef;

        ObjPurchaseOrder.VendorRef = new ReferenceType();
        ObjPurchaseOrder.VendorRef = objPurchaseOrderFound.VendorRef;

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

        for (int i = 0; i < objPurchaseOrderFound.Line.Count(); i++)
        {
            if (objPurchaseOrderFound.Line[i].DetailType == LineDetailTypeEnum.ItemBasedExpenseLineDetail)
            {
                if (!objPurchaseOrderFound.Line[i].AmountSpecified)
                {
        objPurchaseOrderFound.Line[i].AmountSpecified = true;
                }
                objPurchaseOrderFound.Line[i].Amount = 200;
                objPurchaseOrderFound.Line[i].Description = "This is test description";

                ItemBasedExpenseLineDetail itemLineDetail = new ItemBasedExpenseLineDetail();
                itemLineDetail = (ItemBasedExpenseLineDetail)objPurchaseOrderFound.Line[i].AnyIntuitObject;

                if (!itemLineDetail.QtySpecified)
                {
        itemLineDetail.QtySpecified = true;
                }
                itemLineDetail.Qty = 2;

                objPurchaseOrderFound.Line[i].AnyIntuitObject = itemLineDetail;
                LineList.Add(objPurchaseOrderFound.Line[i]);
            }
        }

        ObjPurchaseOrder.Line = LineList.ToArray();

        DataService dataService = new DataService(serviceContext);

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

<h2>Update Purchase Order</h2>

@if (ViewBag.IsSuccess != null && ViewBag.IsSuccess == true)
{
    <div class="row">
        <label class="label label-success"> Purchase Order Updated 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 update the purchase order item’s amount, description, and quantity. here we are changing the amount, description and quantity of purchase order object that we got from Quickbooks online and assigning it to our newly created object for an update.

Submit a Comment

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

Subscribe

Select Categories