-2

I have a REST API class which will send the response to external system as

    {
"AccountId" : "0098",
"email" : "Email-ID",
"status" : "active" 
}

Below is the REST API class that will send data to external system from the given endpoint

public class AccUpdateController {
    @future(callout=true)
    public static void AccupdateController(Set<Id> accountIdset) {       
        string resultBodyGet = '';

        list<Account> accts = [SELECT Unique_ID__c, Account_Status__c, Email__c from Account where Account_Partner_Status__c = 'ACTIVE' and Id IN:accountIdset];

        system.debug('>>>>>>>>>>' + accts);        
        for(Account c : accts){         

            MAp<String, String> tags = new Map<String, String>();
            tags.put('accId', c.Unique_ID__c);
            tags.put('email', c.Email__c);
            tags.put('status', c.Account_Status__c);            
            system.debug('#### Input JSON: ' + JSON.serialize(tags));            
            try{
                string endpoint = 'https://my-endpoint.com';
                HttpRequest req = new HttpRequest();
                req.setEndpoint(endpoint);
                req.setMethod('POST');
                req.setHeader('Content-type', 'application/json');
                req.setbody(JSON.serialize(tags));
                Http http = new Http();
                system.debug('Sending User to update status');
                HTTPResponse response = http.send(req); 
                system.debug('Status updated');
                resultBodyGet = response.getBody();
                system.debug('Output response:' + resultBodyGet);
                accResponse myAccResponse = new accResponse();
                myAccResponse = (accResponse) JSON.deserialize(resultBodyGet, accResponse.class);
                system.debug('#### myAccResponse: ' + myAccResponse);

            }
            catch (exception e) {                              
            }   
        }
    }    
    public class accResponse {
        public string message {get;set;}
    }
}

Now, we have a condition that the callout should be done only when the account status is updated. I suppose that can be achieved using trigger. Can anyone help me with the trigger w.r.t the above REST API class so that it fires the records whenever the account record status is updated.

Thanks!

  • Please take a few minutes to take the Tour and read through How to Ask. Please bear in mind that SFSE is not a code-writing service: we expect to see the work you've done so far on this trigger so that the community can assist you with a specific problem. – David Reed Jul 6 at 18:30
0

Yes, you can collect the list of accounts whose status changed from the trigger. In update triggers, we have all old field values in the old and oldmap trigger context variables.

so you can check for a change in the update trigger, specifically after the update for your scenario. consider your field is Status__c. Below is the code you need to write in a trigger context.

Set<Id> accountIdSet = new Set<Id>();
for(Account accObj: trigger.new){
   if(accObj.Status__c != trigger.oldmap.get(accObj.id).Status__c ){
       accountIdSet.add(accObj.id);
   }
}
if(!accountIdSet.isEmpty()){
    AccUpdateController.AccupdateController(accountIdSet);
}

You need to make some changes in your future method too. 1. Add parameter to accept Set<Id> accountIdset. 2. Query accounts based on accountIdset.

@future(callout=true)
    public static void AccupdateController(Set<Id> accountIdset) {       
        string resultBodyGet = '';

        list<Account> accts = [SELECT Unique_ID__c, Account_Status__c, Email__c from Account where Account_Partner_Status__c = 'ACTIVE' and Id IN:accountIdset ];
        // other code here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.