In this article, we will learn how to update an invoice 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 invoice from Quickbooks online by calling Invoice API.
  • We are querying an invoice by ID.
  • For querying/get invoice we have to define QueryService
  • We need to pass the ServiceContext object into QueryService.
  • We must need invoice Id and SyncToken for an update.
  • We will create an invoice 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 invoice object in DataService.Update<invoice>() for updating an invoice.
  • It will return the newly updated invoice object, you can store the required details to the database according to your needs.
  • The code is as below.
public ActionResult UpdateInvoice()
{
  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_INVOICE_QUERYBYID = string.Format("select * from Invoice where id = '{0}'", "147");
    var queryService = new QueryService<Invoice>(serviceContext);
    Invoice objInvoiceFound = queryService.ExecuteIdsQuery(EXISTING_INVOICE_QUERYBYID).FirstOrDefault<Invoice>();

    //If Invoice found on Quickbooks online
    if (objInvoiceFound != null)
    {
        Invoice ObjInvoice = new Invoice();

        ObjInvoice.Id= objInvoiceFound.Id;
        ObjInvoice.SyncToken= objInvoiceFound.SyncToken;

        ObjInvoice.CustomerRef = new ReferenceType();
        ObjInvoice.CustomerRef = objInvoiceFound.CustomerRef;

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

        for (int i = 0; i < objInvoiceFound.Line.Count(); i++)
        {
            if (objInvoiceFound.Line[i].DetailType == LineDetailTypeEnum.SalesItemLineDetail)
            {
                objInvoiceFound.Line[i].Amount = 150;
                objInvoiceFound.Line[i].Description = "This Description is for invoice";
                LineList.Add(objInvoiceFound.Line[i]);
            }
        }

        ObjInvoice.Line = LineList.ToArray();

        DataService dataService = new DataService(serviceContext);

        Invoice UpdateEntity = dataService.Update<Invoice>(ObjInvoice);
        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 = "UpdateInvoice";
}

<h2>UpdateInvoice</h2>


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