+ Start a Discussion
Diwakar G 7Diwakar G 7 

Post Request in Trigger

Hi, 

I am trying to make a post request if a field value updates. Below is the code.
trigger StatusChange on User_Story__c (after update) {
    
    User_Story__c u = trigger.new[0];
    User_Story__c updacc=[select id,name,userStory_reason__c from User_Story__c where id = :u.id];
    
    if(Trigger.oldmap.get(updacc.id).userStory_reason__c  != Trigger.newmap.get(updacc.id).userStory_reason__c ){

       Getrequest.apicall();
 }

}
Apex Class:
global class Getrequest {

    @Future(callout=true)
    public static void apicall(){
            HttpRequest req = new HttpRequest();
        	String endpoint = 'https://xxxxxxxx.xxxxxxxxxxxxx.com/xxxx';
        	String temp ='A';
        	String temp1 = 'B';
        	String jsonData = '[{"name":"'+temp+'"},{"Request":"'+temp1+'"}]';
      		req.setBody(jsonData);
        	req.setEndpoint(endpoint);
            req.setHeader('Accept', 'application/json');
      		req.setMethod('POST');
      		Http http = new Http();
       		http.send(req);
    }

}

Above code is working fine for single record update. If it is batch update then only field value is getting updated and it is not making a post call. Please help me.

Thanks and Regards,
Diwakar G
Snehil_JaiswalSnehil_Jaiswal

Hi Diwakar ,

Try below code
Reference : https://developer.salesforce.com/forums/?id=9060G000000BdVjQAK

global class BatchSync implements Database.Batchable<sObject>,   Database.AllowsCallouts {
    
     public String query = 'Select ID, Name from Account';
     global Database.QueryLocator start(Database.BatchableContext BC) {
        return Database.getQueryLocator(query);
     }
    
         global void execute(Database.BatchableContext BC, List<Account> records) {         
                  
    
             for ( integer i = 0; i< records.size(); i++ ){
                 // do your job
             }
        }   
    
        global void finish(Database.BatchableContext BC){    
            String endpoint; 
            try {                  
                HttpRequest req = new HttpRequest();
                HttpResponse res = new HttpResponse();
                Http http = new Http();
                // Set values to Params
                
                endpoint = 'Your endpoint';
                
                req.setHeader('Authorization', header);
                req.setHeader('Content-Type', 'application/json');
                req.setEndpoint(endpoint);
                req.setMethod('POST');
                req.setBody('Information you wanna send');
                req.setCompressed(true); // This is imp according to SF, but please check if
                // the webservice accepts the info. Mine did not :P
                // Had to set it to false
                
                if (!Test.isRunningTest()) {      
                    res = http.send(req);
                    String sJson = res.getBody();
                    System.debug('Str:' + res.getBody());
                }             
                // now do what u want to with response.               
            }
            catch (Exception e) {         
                System.debug('Error:' + e.getMessage() + 'LN:' + e.getLineNumber() );           
            }
        }
    }