How To Update Estimate In Quickbooks Online Using C#

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

Following are a few steps for updating estimate,

  • 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 estimate from Quickbooks online by calling Estimate API.
  • We are querying an estimate by ID.
  • For querying/get estimate we have to define QueryService
  • We need to pass the ServiceContext object into QueryService.
  • We must need “estimate Id” and “SyncToken” for an update.
  • We will create an “estimate object” and pass all new data(which we want to update) along with Id and SyncToken.
  • After that, We have to create a DataService object by passing a ServiceContext object as a parameter.
  • Add estimate object in DataService.Update<estimate>() for updating an estimate.
  • It will return the newly updated estimate object, you can store the required details to the database according to your needs.
  • The code is as below.
public ActionResult UpdateEstimate()
{
  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 Estimate where id = '{0}'", "149");
    var queryService = new QueryService<Estimate>(serviceContext);
    Estimate objEstimateFound = queryService.ExecuteIdsQuery(EXISTING_INVOICE_QUERYBYID).FirstOrDefault<Estimate>();

    //If Invoice found on Quickbooks online
    if (objEstimateFound != null)
    {
        Estimate ObjEstimate = new Estimate();

        ObjEstimate.Id = objEstimateFound.Id;
        ObjEstimate.SyncToken = objEstimateFound.SyncToken;

        ObjEstimate.CustomerRef = new ReferenceType();
        ObjEstimate.CustomerRef = objEstimateFound.CustomerRef;

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

        for (int i = 0; i < objEstimateFound.Line.Count(); i++)
        {
            if (objEstimateFound.Line[i].DetailType == LineDetailTypeEnum.SalesItemLineDetail)
            {
                objEstimateFound.Line[i].Amount = 150;
                objEstimateFound.Line[i].Description = "This Keyboard and mouse is sold by vision infotech";
                LineList.Add(objEstimateFound.Line[i]);
            }
        }

        ObjEstimate.Line = LineList.ToArray();

        DataService dataService = new DataService(serviceContext);

        Estimate UpdateEntity = dataService.Update<Estimate>(ObjEstimate);
        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 = "UpdateEstimate";
}

<h2>Update Estimate</h2>

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

 

 

Submit a Comment

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

Subscribe

Select Categories