2

I am just curious to know how to send JSON data with post method using @future call-outs to external system with a trigger (for after insert and update) , any one can help me to show some example on this scenario.

I am just trying to see outbound message of workflow but i can't send JSON format in it .

Help is appreciated. Thanks sfdev

3
  • What data re you trying to send, is it List of Objects or Trigger.new/old or something else ? Commented Jul 17, 2015 at 11:00
  • I wanted to send list of records from 3 objects , Account and two custom objects Commented Jul 17, 2015 at 11:01
  • Do you want to evaluate the records to be sent in future method itself or you just want to send callout with JSON in future method? Commented Jul 17, 2015 at 11:05

3 Answers 3

3

Given that for a @future annotated method:

one of the restrictions of future annotations is that you can not pass sObjects or objects as arguments

you have to create the JSON string in the trigger and then pass it to the method:

trigger MyTrigger on Contact (after insert, after update) { // This is sending an array Sender.post(JSON.serialize(Trigger.new)) } public class Sender { @future(callout=true) public static void post(String jsonString) { HttpRequest req = new HttpRequest(); req.setMethod('POST'); req.setBody(jsonString); ... } } 

Alternatively you can pass an Id or Ids and query in the class. As in your comment you say there are objects of different types involved, probably best to pass the Ids and query in the class.

3

You can use HttpRequest class:

 // This creates json of an object/sObject/List instance String JSONString = JSON.serialize(innerClassORobjectInstance); HttpRequest request = new HttpRequest(); // set method type request.setMethod('POST'); // send JSON by this request.setBody(JSONString); request.setEndpoint(EndpointURL_External_Webservice); request.setHeader('Accept', 'application/json'); // Finally send the request to external server HttpResponse response = new Http().send(request); System.debug('Response: response ' +response); 
0

There is now a new appExchange app that let you do this without code:

Declarative Webhooks - https://appexchange.salesforce.com/appxListingDetail?listingId=a0N3u00000MSv8REAT

You can even do scheduled, and custom button in addition to making it triggered via Process Builder or Flow.

It has a free tier that you can try out before buying.

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.