QuickBooks Online

How To Update Invoice In Quickbooks Online Using C#

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.
Tabish Rangrej

Tabish Rangrej is an Experienced .NET Team Leader, software engineer, and Author with a demonstrated history of working in the IT industry. He has quite well experience in developing, communicating, managing, and writing in his field. He has strong skills and knowledge of ASP.NET C#, ASP.NET MVC, ASP.NET CORE, Angular, AngularJS, Web API, SQL, Entity Framework, JavaScript, Jquery, Different Integrations like Quickbooks, Stripe, Google APIs, Zoho, Orion, Xero, etc., Different versioning tools like TFS, SVN, GIT, etc. and Windows services. Strong engineering professional with a Master of Computer Applications - MCA focused on Computer Science from Veer Narmad South Gujarat University, Surat. Tabish is always ready to accept new challenges and learn new things, he would like to serve better for the community.

Recent Posts

Testing hk

Testing

2 years ago

Create and Used PIPE in angular

In this article, we have to show Create and Used PIPE in angular

2 years ago

Operation

Testing

2 years ago

Create and Used PIPE in angular

In this article, we have to show Create and Used PIPE in angular

2 years ago

Create and Used PIPE in angular

In this article, we have to show Create and Used PIPE in angular

2 years ago

TETS NEW

test

2 years ago