How To Send Purchase Order From Quickbooks Online Using C#

In this blog, I will explain how to send a purchase order 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.

A purchase order will be sent to the email. if the email address already set in purchase order Email or it already exists in purchase order’s “PurchaseOrder.POEmail.Address” then it will be sent to that email else we have to give a particular email on which we have to send a purchase order.

Below are a few steps to send a purchase order,

  • 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 purchase order we have to define QueryService
  • We need to pass the ServiceContext object into QueryService.
  • We will get purchase order details in objPurchaseOrderFound if there is a purchase order 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 objPurchaseOrderFound object.
  • If the email exists then, add objPurchaseOrderFound object in dataService.SendEmail<PurchaseOrder>() for sending a purchase order.
  • If an email does not exist, add objPurchaseOrderFound object and email in dataService.SendEmail<PurchaseOrder>() as a parameter for sending a purchase order. we have to give an email on which we want to send a purchase order. this will also update email in purchase order so next time we don’t need to add email manually.
  • code is as below,
public ActionResult SendPurchaseOrderById(string PurchaseOrderID)
{
  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}'", PurchaseOrderID);
    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)
    {
        DataService dataService = new DataService(serviceContext);

        if (objPurchaseOrderFound.POEmail != null && !string.IsNullOrEmpty(objPurchaseOrderFound.POEmail.Address))
        {
            var SentPDF = dataService.SendEmail<PurchaseOrder>(objPurchaseOrderFound);
            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<PurchaseOrder>(objPurchaseOrderFound, ToEmail);
            ViewBag.IsSuccess = true;
        }
    }
    return View();
  }
  catch (IdsException ex)
  {
    return View();
  }
  catch (Exception ex)
  {
    return View();
  }
}
  • View Code is as below,
@{
    ViewBag.Title = "SendPurchaseOrderById";
}

<h2>Send Purchase Order By Id</h2>


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

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

  • View Code(cshtml) of the purchase order list with the Send email link is as below.
@model List<Intuit.Ipp.Data.PurchaseOrder>
@using Intuit.Ipp.Data;

@{
    ViewBag.Title = "GetAllPurchaseOrder";
}

<h2>Quickbooks online Purchase Order List</h2>

<div>
    <table class="table table-bordered">
        @foreach (var item in Model)
        {
            <tr style="border: 2px solid black;border-bottom: none;background-color:#a9a9a9">
                <th>QBO Purchase Order ID</th>
                <th>Purchase Order Number</th>
                <th>Purchase Order Date</th>
                <th>Vendor</th>
                <th>Total Amount</th>
                <th style="text-align:center;">Download</th>
                <th style="text-align:center;">Send Email</th>
            </tr>

            <tr style="border: 2px solid black;border-bottom: none;border-top: none;">
                <td>@item.Id</td>
                <td>@item.DocNumber</td>
                <td>@item.TxnDate.ToString("MM/dd/yyyy")</td>
                <td>@item.VendorRef.name</td>
                <td>@item.TotalAmt</td>
                <td style="text-align:center;"><a href="@Url.Action("GetPurchaseOrderPdfById", "Home", new { PurchaseOrderID = 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("SendPurchaseOrderById", "Home", new { PurchaseOrderID = item.Id })" target="_blank"><img src="~/Content/Images/SendImageICON.png" height="25" width="25" /></a></td>
            </tr>
            <tr style="border: 2px solid black;border-top: none;">
                <td colspan="7">
                    <table class="table table-bordered">
                        <tr>
                            <td colspan="5" style="text-align:center;"><label>Items Details</label></td>
                        </tr>
                        <tr>
                            <th>Line Id</th>
                            <th>Item Name</th>
                            <th>Qty</th>
                            <th>Total Amount</th>
                            <th>Description</th>
                        </tr>
                        @foreach (var LineItem in item.Line)
                        {
                            <tr>
                                <td>@LineItem.Id</td>
                                @{
                                    ItemBasedExpenseLineDetail Expenseitem = (ItemBasedExpenseLineDetail)LineItem.AnyIntuitObject;
                                }
                                <td>@Expenseitem.ItemRef.name</td>
                                <td>
                                    @if (Expenseitem.QtySpecified)
                                    {
                                        @Expenseitem.Qty
                                    }
                                </td>
                                <td>
                                    @if (LineItem.AmountSpecified)
                                    {
                                        @LineItem.Amount
                                    }
                                </td>
                                <td>@LineItem.Description</td>
                            </tr>
                        }
                    </table>
                </td>
            </tr>
        }

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

OUTPUT:

  • Purchase Order List

  • View of SendPurchaseOrderById

 

Submit a Comment

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

Subscribe

Select Categories