In this blog, I will explain how to send an invoice 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.

An invoice will be sent to the email. if the email address already exists then it will be sent to this email else we have to give a particular email on which we have to send an invoice. or you can also give a particular email on which you want to send an invoice.

Below are a few steps to send an invoice,

  • 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 will get Invoice details in objInvoiceFound if there is an invoice that exists with ID.
  • After that, We have to create a DataService object by passing a ServiceContext object as a parameter.
  • Now, we will check the email address in the objInvoiceFound object.
  • If the email exists then, add objInvoiceFound object in dataService.SendEmail<Invoice>() for sending an invoice.
  • If an email does not exist, add objInvoiceFound object and email in dataService.SendEmail<Invoice>() as a parameter for sending an invoice. we have to give an email on which we want to send an invoice.
  • code is as below,
public ActionResult SendInvoiceById(string InvoiceID)
{
  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}'", InvoiceID);
    var queryService = new QueryService<Invoice>(serviceContext);
    Invoice objInvoiceFound = queryService.ExecuteIdsQuery(EXISTING_INVOICE_QUERYBYID).FirstOrDefault<Invoice>();

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

        if (objInvoiceFound.BillEmail != null)
        {
            var SentPDF = dataService.SendEmail<Invoice>(objInvoiceFound);
            ViewBag.IsSuccess = true;
        }
        else
        {
            //if you want to then, you can set ToEmail from database or somewhere else
            String ToEmail = "tabishzrangrej.vision@gmail.com";
            var SentPDF = dataService.SendEmail<Invoice>(objInvoiceFound, ToEmail);
            ViewBag.IsSuccess = true;
        }

    }
    return View();
  }
  catch (IdsException ex)
  {
    return View();
  }
  catch (Exception ex)
  {
    return View();
  }
}
  • View Code is as below,
@{
    ViewBag.Title = "SendInvoiceById";
}

<h2>Send Invoice By Id</h2>

@if (ViewBag.IsSuccess != null && ViewBag.IsSuccess == true)
{
    <div class="row">
        <label class="label label-success"> Invoice Sent Successfully </label>
    </div>
}
  • The above code will send an invoice to email.

You can also set a send email link in the list view of the invoice. if you don’t know how to get Invoices from Quickbooks Online you can find it here.

  • View Code(cshtml) of the invoice list with the Send email link is as below.
@model List<Intuit.Ipp.Data.Invoice>

@{
    ViewBag.Title = "GetAllInvoice";
}

<h2>Quickbooks online Invoice List</h2>

<div>
    <table class="table table-bordered">
        <tr>
            <th>QBO ID</th>
            <th>Invoice Number</th>
            <th>Invoice Date</th>
            <th>Customer</th>
            <th>Total Amount</th>
            <th style="text-align:center;">Download</th>
            <th style="text-align:center;">Send Email</th>
        </tr>

        @foreach (var item in Model)
        {
            <tr>
                <td>@item.Id</td>
                <td>@item.DocNumber</td>
                <td>@item.TxnDate.ToString("MM/dd/yyyy")</td>
                <td>@item.CustomerRef.name</td>
                <td>@item.TotalAmt</td>
                <td style="text-align:center;"><a href="@Url.Action("GetInvoicePdfById", "Home", new { InvoiceID = item.Id })" target="_blank"><img src="~/Content/Images/downloadICON.png" height="25" width="25" /></a></td> 
                <td style="text-align:center;"><a href="@Url.Action("SendInvoiceById", "Home", new { InvoiceID = item.Id })" target="_blank"><img src="~/Content/Images/SendImageICON.png" height="25" width="25" /></a></td>
            </tr>
        }

    </table>
</div>
  • In the above code, on click of send email icon invoice will be sent.

OUTPUT:

  • Invoice List

  • View of SendInvoiceById