You need to sign in to do that
Don't have an account?

How to send HTTP request when inserting data to Custom Object
Hi All, I am very new to Salesforce. Please help me on this.
I have a custom Object call "Book". I wrote a class. It has function call "sendRequest"
public static void sendRequest(Book__c[] books){
string url = myUrl;
for (Book__c b :books){
//url += '?bookPrice='+ b.Price__c;
HttpRequest req = new HttpRequest();
HttpResponse res = new HttpResponse();
Http http = new Http();
req.setMethod('POST' ); // Method Type
req.setEndpoint(url); // Server Url
req.setHeader('Content-Type', 'application/x-www-form-urlencoded'); // Content Type
req.setBody('bookPrice=' + EncodingUtil.urlEncode('testValue', 'UTF-8')); // Request Parameters
try {
res = http.send(req);
if(res.getBody() != null){
// Parse Response
}
} catch(Exception e) {
System.debug('error: '+ e);
}
}
}
Then I wrote a trigger.
trigger BookRackTrigger on Book__c (after insert) {
Book__c[] books = Trigger.new;
BookRack.sendRequest(books);
}
Now when I am adding a Book, after clicking "save" I want to send a request to myURL. But it it not receiving any request. I have added myURL to remote site list as well.
What am I doing wring here? lease help me.
I have a custom Object call "Book". I wrote a class. It has function call "sendRequest"
public static void sendRequest(Book__c[] books){
string url = myUrl;
for (Book__c b :books){
//url += '?bookPrice='+ b.Price__c;
HttpRequest req = new HttpRequest();
HttpResponse res = new HttpResponse();
Http http = new Http();
req.setMethod('POST' ); // Method Type
req.setEndpoint(url); // Server Url
req.setHeader('Content-Type', 'application/x-www-form-urlencoded'); // Content Type
req.setBody('bookPrice=' + EncodingUtil.urlEncode('testValue', 'UTF-8')); // Request Parameters
try {
res = http.send(req);
if(res.getBody() != null){
// Parse Response
}
} catch(Exception e) {
System.debug('error: '+ e);
}
}
}
Then I wrote a trigger.
trigger BookRackTrigger on Book__c (after insert) {
Book__c[] books = Trigger.new;
BookRack.sendRequest(books);
}
Now when I am adding a Book, after clicking "save" I want to send a request to myURL. But it it not receiving any request. I have added myURL to remote site list as well.
What am I doing wring here? lease help me.
The only way that you can execute a callout from a trigger is to schedule it to run asynchronously, for example by executing a method with the @future method as you have found. The key word here is asynchronously - you can't wait for a response from the callout as that would require the transaction to stall until it completes, which is the reason that synchronous callouts aren't allowed.
by the way you can try workflows outbound message check this https://help.salesforce.com/htviewhelpdoc?id=workflow_defining_outbound_messages.htm
If I want to send the request when I am inserting the record, cant I do it sychronously?
Thanks.
code runs synchronously only when no further processing happens until the external Web service returns a response.
thats why we used @future annotation to make the callout run asynchronously.