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
shakila Gshakila G 

IF records getting created inSalesforce how to send data as JSON Response to Third party application from salesforce?

Hello All,

If the custom object record getting created  in salesforce i want to send that data to third party application as JSON Response.

If any having sample code please share to review the steps

Thanks,
Shakila

 

Ajay K DubediAjay K Dubedi
Hi Shakila,
Following code is for sending a request to 3rd Party app "Twilio"
public class HttpCalloutSample {
  // Pass in the endpoint to be used using the string url
  @AuraEnabled
  public static String getCalloutResponseContents1(String phoneNumber) {
    // Instantiate a new http object
    Http h = new Http();
    //String instBody = 'Url=http://demo.twilio.com/docs/voice.xml&To=+919838454466&From=+12672140815';
    String instBody = 'Url=http://demo.twilio.com/docs/classic.mp3&To='+phoneNumber+'&From=+12672140815';
    String inEndPoint = 'https://api.twilio.com/2010-04-01/Accounts/ACfae022703791c240df9da8a2c1d21ab8/Calls.json';
    String uname = 'ACfae022703791c240df9da8a2c1d21ab8';
    String pswd = 'e941a85679ec5d0d27a44eede6c6f6c0';
    String new1 = uname+':'+pswd;
    Blob mediate = Blob.valueOf(new1);
    String credential = 'BASIC ' +EncodingUtil.base64Encode(mediate);
    System.debug('Credential-->'+credential);
    HttpRequest req = new HttpRequest();
    req.setEndpoint(inEndPoint);
    req.setHeader('Content-Type', 'application/x-www-form-urlencoded');
    req.setMethod('POST');
    req.setBody(instBody);
    req.setHeader('Authorization', credential);
    // Send the request, and return a response
    HttpResponse res = h.send(req);
    System.debug('Response-->'+res.getBody());
    return res.getBody();
  }
}

Further as per your requirement you can send JSON data in setbody. You just need to serialize it to JSON, use JSON.serialize(data) for this.
For more info on sending data refer:
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_restful_http_httprequest.htm
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks,
Ajay Dubedi 
Khan AnasKhan Anas (Salesforce Developers) 
Hi Shakila,

Greetings to you!

You can use the serialize method of JSON class to convert the data into a JSON format. Use this code logic in your Insert Update Trigger:
 
List<Account> newAccounts = new List<Account>();
for(Account acc : Trigger.new){
    newAccounts.add(acc);
}
String jsonString = JSON.serialize(newAccounts);

So, you can collect all the newly created accounts into a list and then make a single call to the API.

I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.

Thanks and Regards,
Khan Anas