Skip to main content
Hello there,

I am having below requirement.

In salesforce when a record is updated then send data of that record to external system using reset API JSON file.

I have written below code but I am not sure whether I am going in right direction 

Trigger

rigger ImplementationPlanActivity on Implementation_Plan_Activity__c (after update) {

    Trigger_Settings__c triggerSettings = Trigger_Settings__c.getInstance();

    if(triggerSettings.ImplementationPlanActivity__c == false){

        return;

    }

            set<Id> IPAIdSet=new Set<Id>();

            for(Implementation_Plan_Activity__c IPArecord : trigger.new ){

                IPAIdSet.add(IPArecord .id);

            }

            IPAResetAPIClass.IPAData(IPAIdSet);

        }

    }

Webservice class

global class IPAResetAPIClass{

@future(callout=true)

    global static void IPAData(Set<Id> newIPASetId){

    

        List<Implementation_Plan_Activity__c> ipaNewList=[SELECT id,Name,

                                                                    Account_Manager__r.Name,

                                                                    Account_Manager__r.Email,

                                                                    Account_Specialist__r.Name,

                                                                    Account_Specialist__r.Email,    

                                                                    SFID18__c ,

                                                                    RecordTypeId

                                                            FROM Implementation_Plan_Activity__c where Id IN : newIPASetId];

        

        List<impWarpperClass> warpperIPAList=new List<impWarpperClass>();

        

            warpperIPAList.add(new impWarpperClass(ipaNewList[0].id,

                                                    ipaNewList[0].Name,

                                                    ipaNewList[0].Account_Manager__r.Name,

                                                    ipaNewList[0].Account_Manager__r.Email,

                                                    ipaNewList[0].Account_Specialist__r.Name,

                                                    ipaNewList[0].Account_Specialist__r.Email,

                                                    ipaNewList[0].SFID18__c,

                                                    ipaNewList[0].RecordTypeId));

        

 

       RecordType recordTypeRecord=[Select id,Name from RecordType  where id=:ipaNewList[0].RecordTypeId limit 1];

       

       string  resource=null;

       if(recordTypeRecord.Name=='Auto Provisioning'){

           resource='Auto Provisioning';

       }

       else if(recordTypeRecord.Name =='Workbook'){

           resource='Workbook';

        }      

        system.debug('*********warpperIPAList='+warpperIPAList);

        

        string JSONString=JSON.serializePretty(warpperIPAList);

        system.debug('*********serialized imp='+JSONString);

        

        string endURL;

        

        endURL=geURL+resource+'/'+ipaNewList[0].id;

        system.debug('*********endURL='+endURL);

        HttpRequest req=new HttpRequest();

        //Set HttpRequest Method

        req.setMethod('POST');

      

        req.setHeader('content-type', 'application/json');

        //Set HTTPRequest Endpoint dummy url

        req.setEndpoint('http://requestb.in/13auzf41');

        //req.setEndpoint(endURL);

        

        //Set HTTP Request Body

        req.setBody(JSONString);

        Http http = new Http();

        try{

            HTTPResponse res = http.send(req);

            //Helpful debug messages

            System.debug(res.toString());

            System.debug('STATUS:'+res.getStatus());

            System.debug('STATUS_CODE:'+res.getStatusCode());

            System.debug('Content: ' + res.getBody());

            

            //we can create success object record

        }

        catch(Exception e){

            System.debug('******* error message='+e);

            //we can create error object record

        }

    

    }

    

    global class impWarpperClass{

    

        global string IPAId{get;set;}

        global string IPAName{get;set;}   

        global string IPAAccManagerName{get;set;}   

        global string IPAAccManagerEmail{get;set;} 

        global string IPAAccSpecialListrName{get;set;}   

        global string IPAAccSpecialListEmail{get;set;} 

           

        global string IPASFDCId{get;set;}

        global string IPARecordTypeId{get;set;}

        

        global impWarpperClass(string IPAId,string IPAName,

                                        string IPAAccManagerName,

                                        String IPAAccManagerEmail,

                                        string IPAAccSpecialListrName,

                                        string IPAAccSpecialListEmail,

                                        string IPASFDCId,

                                        string IPARecordTypeId){

            this.IPAId=IPAId;

            this.IPAName=IPAName;

            this.IPAAccManagerName=IPAAccManagerName;

            this.IPAAccManagerEmail=IPAAccManagerEmail;

            this.IPAAccSpecialListrName=IPAAccSpecialListrName;

            this.IPAAccManagerEmail=IPAAccSpecialListEmail;

            this.IPASFDCId=IPASFDCId;

            this.IPARecordTypeId=IPARecordTypeId;

            

        }

       

    }

  

       }

}

am I going right dirction. is this correct way to send data from salesforce to external system using rest api JSON file.

If I am wrong can any please let me know how can I expose salesforce data to external system when records is updted in salesforce.

Thank you.
5 answers
  1. Dec 28, 2016, 9:08 PM
    The basic Idea of calling a @future method to send the data seems to be the correct approach for this requirement.  As long as other code is developed properly in this instance, and doesn't call updates on Implementation_Plan_Activity__c in a loop - you should be fine.  Otherwise that type of code (other than being badly written) may cause you to exceed your limit of 50 future invocations in a single context.

    Please remember to mark a comment as best answer (even if its not this one). This marks the thread as solved and helps the community!
0/9000