How To Add Invoice In Quickbooks Online Using C#

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

CustomerRef and Line elements are required for creating an invoice, we will use these two required elements for creating an invoice.

We have to give the customer reference in CustomerRef, and items or item reference in Line.

Item must be already added in Quickbooks before adding it to the invoice.

Let’s create an Invoice.

  • 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 an Invoice object and assign data into an object, the following are some required data information that we have to give.
    1. CustomerRef: we have to give Quickbooks online Customer ID as a reference for the customer.
    2. Line: we have to give items details as a reference for the invoice item or line item, we need to provide at least one item reference.for that we have to specify Quickbooks online Item ID, Amount, and Qty.
  • After that, We have to create a DataService object by passing a ServiceContext object as a parameter.
  • Add Invoice object in DataService.Add() for adding an invoice.
  • If an invoice created successfully, it will return the newly-created invoice object, from this object you can store details like invoice Id in the database if you want to.
  • The code is as below.
public ActionResult CreateInvoice()
{
  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;

    Invoice ObjInvoice = new Invoice();

    ObjInvoice.CustomerRef = new ReferenceType();
    ObjInvoice.CustomerRef.Value = "58"; //Quickbooks online Customer Id

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

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

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

    SalesItemLineDetail salesItemLineDetail = new SalesItemLineDetail();
    salesItemLineDetail.QtySpecified = true;
    salesItemLineDetail.Qty = 1;
    salesItemLineDetail.ItemRef = new ReferenceType();
    salesItemLineDetail.ItemRef.Value = "19"; //Quickbooks online Item Id

    objLine.AnyIntuitObject = salesItemLineDetail;

    LineList.Add(objLine);

    ObjInvoice.Line = LineList.ToArray();

    DataService dataService = new DataService(serviceContext);

    Invoice InvoiceAdd = dataService.Add(ObjInvoice);
    if (InvoiceAdd != null && !string.IsNullOrEmpty(InvoiceAdd.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 = "CreateInvoice";
}

<h2>Create Invoice</h2>


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

OUTPUT:(invoice preview on Quickbook online)

 

Submit a Comment

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

Subscribe

Select Categories