QuickBooks Online

How To Update Employee In Quickbooks Online Using C#

In this article, we will learn how to update an employee 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 employee from Quickbooks online by calling Employee API.
  • We are querying employee by employee id.
  • For querying/get employee we have to define QueryService
  • We need to pass the ServiceContext object into QueryService.
  • We must need employee Id and SyncToken for an update.
  • We will create a vendor 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 employee object in DataService.Update<Employee>() for updating an employee.
  • it will return the newly updated employee object, you can store the required details to the database according to your needs.
  • The code is as below.
public ActionResult UpdateEmployee()
{
  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_EMPLOYEE_QUERYBYID = string.Format("select * from Employee where id = '{0}'", "59");

    var queryService = new QueryService<Employee>(serviceContext);
    Employee ObjEmployeeFound = queryService.ExecuteIdsQuery(EXISTING_EMPLOYEE_QUERYBYID).FirstOrDefault<Employee>();

    if (ObjEmployeeFound != null)
    {
        Employee ObjEmployee = new Employee();

        ObjEmployee.Id = ObjEmployeeFound.Id;
        ObjEmployee.SyncToken = ObjEmployeeFound.SyncToken;

        ObjEmployee.GivenName = ObjEmployeeFound.GivenName;
        ObjEmployee.FamilyName = "EmployeeEmployee";
        ObjEmployee.CompanyName = ObjEmployeeFound.CompanyName;

        EmailAddress ObjEmail = new EmailAddress();
        ObjEmail.Address = "tabish.Employee@test.com";
        ObjEmployee.PrimaryEmailAddr = ObjEmail;

        PhysicalAddress ObjAddress = new PhysicalAddress();
        ObjAddress.PostalCode = ObjEmployeeFound.PrimaryAddr.PostalCode;
        ObjAddress.Country = ObjEmployeeFound.PrimaryAddr.Country;
        ObjAddress.Line1 = ObjEmployeeFound.PrimaryAddr.Line1;
        ObjAddress.City = ObjEmployeeFound.PrimaryAddr.City;
        ObjEmployee.PrimaryAddr = ObjAddress;

        TelephoneNumber ObjTelephoneNumber = new TelephoneNumber();
        ObjTelephoneNumber.FreeFormNumber = ObjEmployeeFound.PrimaryPhone.FreeFormNumber;
        ObjEmployee.PrimaryPhone = ObjTelephoneNumber;

        DataService dataService = new DataService(serviceContext);

        Employee UpdateEntity = dataService.Update<Employee>(ObjEmployee);
        if (UpdateEntity != null && !string.IsNullOrEmpty(UpdateEntity.Id))
        {
            ViewBag.IsSuccess = true;
        }
    }

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

<h2>Update Employee</h2>



@if (ViewBag.IsSuccess != null && ViewBag.IsSuccess == true)
{
    <div>
        <label class="label label-success">Employee Updated Successfully</label>
    </div>
}

 

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

3 years ago