RESTful Web Service Callout using POST with Salesforce.com
March 16th, 2009
The addition of asynchronous Web service callouts to external services is a feature that developers have been requesting for quite awhile in Salesforce.com. Using the new @future annotation, your methods execute the callout when Salesforce.com has resources available. One of the great benefits is that it allows you to perform callouts during trigger executions.
One method of performing callouts is to import your WSDL and let Apex do all of the heavy lifting (WSDL2Apex). The major problem that I found is that Apex does not support RPC/encoded services at this time. Apex does support HTTP Service classes which will allow you to create RESTful services as an alternative. For more information and example code there is a great article entitled Apex Web Services and Callouts.
The HttpResponse class provides a simple GET example but it was hard to find any examples using POST so I thought I’d throw something together.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | public class WebServiceCallout { @future (callout=true) public static void sendNotification(String name, String city) { HttpRequest req = new HttpRequest(); HttpResponse res = new HttpResponse(); Http http = new Http(); req.setEndpoint('http://my-end-point.com/newCustomer'); req.setMethod('POST'); req.setBody('name='+EncodingUtil.urlEncode(name, 'UTF-8')+'&city='+EncodingUtil.urlEncode(city, 'UTF-8')); req.setCompressed(true); // otherwise we hit a limit of 32000 try { res = http.send(req); } catch(System.CalloutException e) { System.debug('Callout error: '+ e); System.debug(res.toString()); } } // run WebServiceCallout.testMe(); from Execute Anonymous to test public static testMethod void testMe() { WebServiceCallout.sendNotification('My Test Customer','My City'); } } |
You can execute your callout in a trigger with the following example:
1 2 3 4 5 6 7 8 9 | trigger AccountCallout on Account (after insert) { for (Account a : Trigger.new) { // make the asynchronous web service callout WebServiceCallout.sendNotification(a.Name, a.BillingCity); } } |
A couple of things to remember when using the future annotation:
- No more than 10 method calls per Apex invocation
- No more than 200 method calls per Salesforce license per 24 hours
- The parameters specified must be primitive dataypes, arrays of primitive datatypes, or collections of primitive datatypes.
- Methods with the future annotation cannot take sObjects or objects as arguments.
- Methods with the future annotation cannot be used in Visualforce controllers in either getMethodName or setMethodName methods, nor in the constructor.
Categories: Apex, Code Sample, Salesforce











well I tried this out Jeff and it doesn’t work. Did you actually test this? I had another developer test this actual code and theirs also did not work.
If you place the following code:
<?php
$temp = '
‘;
mail(‘me@email.com’, ‘APEX Data’, $temp);
?>
and then test the code it does not show any of the data that you are sending via post, unlike the _$GET example
I would like to see if you would look into this, this is a great tutorial!
Thanks!!
~Mike
Mike, what is the error you are getting?
Here are the two issues:
1) webservicecallout: Methods defined as TestMethod do not support Web service callouts, test skipped – are not supported
2) none of the post data is sent. The script is kicked off because I receive an empty email, but no post data
Just thought that I would let you know
Thanks
~Mike
Well I was messing around with it and removed the compression:
req.setCompressed(true); // otherwise we hit a limit of 32000
now it works !!
But what can happen if we do not compress it