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
Sam AlexSam Alex 

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.
 
kiranmutturukiranmutturu
callouts cannot be made from triggers as that would hold up the database transaction until the callout completed, which can be up to 120 seconds from a limits perspective. This could obviously cause significant contention with other transactions and impact performance.

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
Sam AlexSam Alex
Thanks for you reply Kiran,

If I want to send the request when I am inserting the record, cant I do it sychronously? 

Thanks.
kiranmutturukiranmutturu
if it from a trigger its NO... 

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.
Sam AlexSam Alex
Ohhh... I think I want is Synchronous. If synchronous what is the best way to do this? Could you please help me on this? Thanks in advance Kiran.