Favorite #Salesforce Developer Help regarding api call I have integration, while this, I need to create customer in another system. Customer name is Account's Name. Now Customer is created successfully, i want to preform that if customer already exist in other system, first i need to get that customer if the customer is exist i need to call get api, and update the customer, and customer is not exist then only create new customer with post api.
here is my main function.
public with sharing class Maia_Customer_Service {
static QuickBook_API_Log__c Log = new QuickBook_API_Log__c();
static Maia_Customer_Request_Wrapper requestWrapper = new Maia_Customer_Request_Wrapper();
//Business Logic
@future(callout=true)
public static void createCustomer(List<String> revenuProjectionId_list){
create_customer(revenuProjectionId_list);
}
public static Maia_Customer_Request_Wrapper create_customer(List<String> revenuProjectionId_list){
String request_JSON;
List<Mirror_Revenue_Projection__c> revenuProjection_list = Maia_FetchProjection_Service.getProjectionData(revenuProjectionId_list[0]);
if(!revenuProjection_list.isEmpty()){
Mirror_Revenue_Projection__c projection = revenuProjection_list[0];
try{
request_JSON = createRequestPayload(projection);
system.debug('request_JSON:'+request_JSON);
// CustomerResponse is a wrapper class
String CustomerResponse = Maia_Rest_API.restApiCall(request_JSON, 'customer');
Maia_Customer_Response_Wrapper wrapperResponse = (Maia_Customer_Response_Wrapper)JSON.deserialize(CustomerResponse,Maia_Customer_Response_Wrapper.class);
}
catch(Exception e){
System.debug('Exception: '+e);
Log.QuickBook_Request__c = request_JSON;
Log.QuickBook_Error__c = e.getMessage();
upsert log;
//responseWrapper.error = e.getMessage();
}
}else{
system.debug('No Projection data found');
}
return requestWrapper;
}
private static Maia_Customer_Request_Wrapper createCustomerpayload (Mirror_Revenue_Projection__c projection){
Maia_Customer_Request_Wrapper customer = new Maia_Customer_Request_Wrapper();
try{
customer.FullyQualifiedName = projection.Account__r.Name;
customer.DisplayName = projection.Account__r.Name;
// customer.GivenName = 'James';
Maia_Customer_Request_Wrapper.cls_PrimaryEmailAddr createPrimaryEmailAddressPayload = new Maia_Customer_Request_Wrapper.cls_PrimaryEmailAddr();
createPrimaryEmailAddressPayload.Address = projection.Account__r.Billing_Contact_Email__c;
customer.PrimaryEmailAddr = createPrimaryEmailAddressPayload;
Maia_Customer_Request_Wrapper.cls_PrimaryPhone createPrimaryPhonePayload = new Maia_Customer_Request_Wrapper.cls_PrimaryPhone();
createPrimaryPhonePayload.FreeFormNumber = string.valueOf(projection.Account__r.Phone);
customer.PrimaryPhone = createPrimaryPhonePayload;
Maia_Customer_Request_Wrapper.cls_BillAddr createBillAddressPayload = new Maia_Customer_Request_Wrapper.cls_BillAddr();
createBillAddressPayload.CountrySubDivisionCode = projection.Account__r.BillingState;
createBillAddressPayload.City = projection.Account__r.BillingCity;
createBillAddressPayload.PostalCode = projection.Account__r.BillingPostalCode;
createBillAddressPayload.Line1 = projection.Account__r.BillingStreet;
createBillAddressPayload.Country = projection.Account__r.BillingCountry;
customer.BillAddr = createBillAddressPayload;
}catch(Exception e){
System.debug('Exception: '+e);
}
return customer;
//system.debug('customer'+customer.id);
}
private static String createRequestPayload(Mirror_Revenue_Projection__c projection){
Maia_Customer_Request_Wrapper wrapper = new Maia_Customer_Request_Wrapper();
wrapper = createCustomerpayload(projection);
String request_JSON = JSON.serialize(wrapper, true);
return request_JSON;
}
I have separate rest_api class
//request.setEndpoint('callout:dpforce/v3/company/4620816365284208290/'+apiName);
apiname = query ==> for get api
apiame = customer ==> create customer post api
public static string restApiCall(String requestJson,String apiName){
String body;
body = requestJson;
Http http = new Http();
HttpRequest request = new HttpRequest();
HttpResponse response = new HttpResponse();
system.debug('body>>>'+body);
Map<String, String> headerMap = new Map<String, String>();
headerMap.put('Content-Type','application/json');
headerMap.put('Accept','application/json');
//request.setHeader('Content-Type', 'application/json');
request.setHeader('Content-Type', 'application/text');
request.setHeader('Accept' , 'application/json');
request.setMethod('POST');
System.debug('Authorization= '+request.getHeader('Authorization'));
request.setEndpoint('callout:dpforce/v3/company/4620816365284208290/'+apiName);
request.setBody(requestJson);
response = http.send(request);
system.debug('>>>>response'+response);
system.debug('>>>>response body'+response.getBody());
Map<String, Object> responseMap = (Map<String, Object>) JSON.deserializeUntyped(response.getBody());
String recordId = (String)responseMap.get('Id');
System.debug('>>>>New record created with ID: ' + recordId);
return response.getBody();
}
}
Also have separate Fetch_service class,it having return query. How to get customer first based on this query = select * from customer where DisplayName = 'Test Account' DisplayName is wrapper class's variable. How can i get this.