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> }