You need to sign in to do that
Don't have an account?
Webservice callout tutorial > Is there a way
I followed this tutorial and was wondering if there are anymore examples that could expand on the callouts. I was wondering if there was a way to send out the actual ID and Name of the new account that is created. The trigger:
//This is the trigger from the tutorial that sends calls the APEX class AccountUpdater //as you can see it sends the ID and the Name of the account trigger descriptionUpdater on Account (after insert) { System.debug('Making future call to update account'); for (Account acc : Trigger.New) { //Call future method to update account //with data from external server. //This is a async calls, it returns right away, after //enqueuing the request. AccountUpdater.updateAccount(acc.Id, acc.Name); } }
This sends the ID and NAME of the account to the AccountUpdater class
The class creates the web service call to a local server I have and it works perfectly.
//This is the class from the tutorial that processes the ID and Name //and set's up the http request public class AccountUpdater { //Future annotation to mark the method as async. @Future(callout=true) public static void updateAccount(String id, String name) { //construct an HTTP request HttpRequest req = new HttpRequest(); req.setEndpoint('http://mapsdev.maxim-ic.com/data.php'); req.setMethod('GET'); //send the request Http http = new Http(); HttpResponse res = http.send(req); //check the response if (res.getStatusCode() == 200) { //update account Account acc = new Account(Id=id); acc.Description = res.getBody(); update acc; } else { System.debug('Callout failed: ' + res); } } }
What I was wondering if there is a way to actually send out the ID and/or name of the account that was created in the http request method in APEX
Thanks and I hope this makes sense!!
~Mike
Jeff,
Thanks for that, I am going to work with this one. I would rather send it via post
Your tutorial is great, but another question. Do you or have you always used a trigger to call the APEX class? I am just trying to find out what other ways this could work or other benefits this could have
~Mike
All Answers
I believe you can just attach it to the end of endpoint and retrieve it in php using $_GET.
Apex:
//construct an HTTP request
HttpRequest req = new HttpRequest();
req.setEndpoint('http://mapsdev.maxim-ic.com/data.php?accid=' + id + '&name=' + name);
req.setMethod('GET');
PHP:
$id = '';
$name = '';
if(isset($_GET['id']) $id = $_GET['id'];
if(isset($_GET['name']) $name = $_GET['name'];
edit: fixed line return on php code, and yet again for the apex code. the graphical editor hates me today.
-Richard
Take a look at the example I put together: RESTful Web Service Callout using POST with Salesforce.com.
HTH
Jeff Douglas
Appirio, Inc.
http://blog.jeffdouglas.com
http://techblog.appirio.com
Thanks for the reply sir, I figured that out when I was messing around. I appreciate the fast reply
~Mike
Jeff,
Thanks for that, I am going to work with this one. I would rather send it via post
Your tutorial is great, but another question. Do you or have you always used a trigger to call the APEX class? I am just trying to find out what other ways this could work or other benefits this could have
~Mike
No, you can certainly invoke the Apex from a controller or exposing the class as a Web service.
HTH
Jeff Douglas
Appirio, Inc,
http://blog.jeffdouglas.com
http://techblog.appirio.com
Jeff
Is there a chance that I could talk to you offline sometime for a few min? Sorta to pick your brain!
~Mike
Thanks Richard, Much appreciated, got that figured out!
No problem.
-Richard