要从其他系统获取数据,如果数据存在调用get api else cretae new

问题描述 投票:0回答:0

收藏 #Salesforce 开发人员 关于 api 调用的帮助 我有集成,而这个,我需要在另一个系统中创建客户。客户名称是帐户名称。现在客户已成功创建,我想执行如果客户已经存在于其他系统中,首先我需要获取该客户如果客户存在我需要调用获取 api,并更新客户,然后客户不存在使用 post api 创建新客户。

这是我的主要功能。

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

我有单独的 rest_api 类

//request.setEndpoint('callout:dpforce/v3/company/4620816365284208290/'+apiName);

apiname = query ==> for get api

apiame = 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();
        

    }
}

还有单独的 Fetch_service 类,它有返回查询。 如何根据此查询首先获取客户 = select * from customer where DisplayName = 'Test Account' DisplayName 是包装类的变量。 我怎样才能得到这个。

salesforce integration apex salesforce-developer
© www.soinside.com 2019 - 2024. All rights reserved.