function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Aaron AttardAaron Attard 

JSON POST send details from created record to external endpoint

I have this apex trigger & apex class that trigger when a record is created. This works well when sending text as string in the request.setbody, however I need to replace the "name" & "external_id" with values from the created record within inverted commas.

The end result should be sending JSON text as follows:

{
"options": [ {"name": "Job Name A", "priority": 0, "external_id": "a1H0J000005xP6E"} ]
}

//Apex Trigger
trigger [ApexTriggerName] on [ObjectRecord] (after insert)
{AfterInsertCreateJob.jobs();}
//Apex Class
public class AfterInsertCreateJob {
    
    @future(callout=true)
    public static void jobs(){
        Http http = new Http();
        HttpRequest request = new HttpRequest();
        request.setEndpoint('https://example.com');
        request.setMethod('POST');
        request.setHeader('Content-Type','application/json');
        request.setHeader('On-Behalf-Of','[userid]');
        request.setHeader('Authorization','[password]');
        request.setBody('{"options":[{"name":"record.name","priority":0,"external_id":"record.id"}]}');
        System.debug('1: ' + request);
        HttpResponse response = http.send(request);
        System.debug('2: ' + response);
    }

}

 
Best Answer chosen by Aaron Attard
mukesh guptamukesh gupta
Hi Aaron,

Please use below code:-
 
//Apex Trigger
trigger AccountTrigger on Account(after insert)
{
Set<Id> accIds = new Set<Id>();

for(Account acc : Trigger.New){
   accIds.add(acc.Id);
}

AfterInsertCreateJob.jobs(accIds);

}
 
//Apex Class
public class AfterInsertCreateJob {
    
    @future(callout=true)
    public static void jobs(Set<Id> accIds){
	
	    List<Account> accList = [Select Id, Name from Account Where Id IN:accIds ];
        Http http = new Http();
        HttpRequest request = new HttpRequest();
        request.setEndpoint('https://example.com');
        request.setMethod('POST');
        request.setHeader('Content-Type','application/json');
        request.setHeader('On-Behalf-Of','[userid]');
        request.setHeader('Authorization','[password]');
        request.setBody('{"options":[{"name":accList[0].Name,"priority":0,"external_id":"accList[0].id"}]}');
        System.debug('1: ' + request);
        HttpResponse response = http.send(request);
        System.debug('2: ' + response);
    }

}

if you need any assistanse, Please let me know!!

Kindly mark my solution as the best answer if it helps you.

Thanks
Mukesh

All Answers

mukesh guptamukesh gupta
Hi Aaron,

Please use below code:-
 
//Apex Trigger
trigger AccountTrigger on Account(after insert)
{
Set<Id> accIds = new Set<Id>();

for(Account acc : Trigger.New){
   accIds.add(acc.Id);
}

AfterInsertCreateJob.jobs(accIds);

}
 
//Apex Class
public class AfterInsertCreateJob {
    
    @future(callout=true)
    public static void jobs(Set<Id> accIds){
	
	    List<Account> accList = [Select Id, Name from Account Where Id IN:accIds ];
        Http http = new Http();
        HttpRequest request = new HttpRequest();
        request.setEndpoint('https://example.com');
        request.setMethod('POST');
        request.setHeader('Content-Type','application/json');
        request.setHeader('On-Behalf-Of','[userid]');
        request.setHeader('Authorization','[password]');
        request.setBody('{"options":[{"name":accList[0].Name,"priority":0,"external_id":"accList[0].id"}]}');
        System.debug('1: ' + request);
        HttpResponse response = http.send(request);
        System.debug('2: ' + response);
    }

}

if you need any assistanse, Please let me know!!

Kindly mark my solution as the best answer if it helps you.

Thanks
Mukesh
This was selected as the best answer
Aaron AttardAaron Attard

Hi Mukesh

Thanks a lot, that worked!

I also needed to amend the request.setBody line as follows to ensure that id and name values are sent within "".

request.setBody('{"options":[{"name":"'+accList[0].Name+'","priority":0,"external_id":"'+accList[0].id+'"}]}');

Regards

Aaron