Take the 2-minute tour ×
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.

I have a requirement where I have to send out a web service to a third party system every time a Lead is inserted. I can do it by calling the web service via trigger. However I want to make sure that one lead information is going per web service call i.e. one lead per SOAP envelope. (This is how the third party wsdl accepts Leads).

I was thinking of creating multiple web-service call within the trigger however I will be hitting governor limits left and right.

Lead's will be added to Salesforce one at a time. Will this restrict inert trigger to have only one lead record or there is still a possibility of having multiple lead records in the trigger list?

share|improve this question
add comment

2 Answers 2

up vote 3 down vote accepted

If Leads are added one by one then the trigger size will always be 1.

You could build a check into the trigger to e.g. block batch size larger than 1.

Alternatively, you could maximize the batch size to 10, since that is the max amount of callouts in 1 transaction. You could even make it 100 if you really want to, because you can start 10 @future contexts in 1 transaction, each of which can do 10 callouts.

Best practice, however, is to ask third party webservices to be batch-proof.

share|improve this answer
    
Thank you @guy! –  Yash Mehta Apr 3 at 19:42
    
Maybe I'm missing something, but this won't work unless you use future methods or a Salesforce batch to get the callout out of the trigger transaction. –  Daniel Ballinger Apr 3 at 19:58
    
That's correct and will be required anyway. I interpreted the question to be more about the # of records restriction than on how to do a callout from a trigger. Anyway, let's hope the combintion of our answers will help Yash to get where he wants to :-) –  guy Apr 3 at 20:51
    
OK. Just wanted to be clear :) The implication that a callout could be made directly from a trigger made me uneasy. –  Daniel Ballinger Apr 3 at 21:19
add comment

Triggers can not directly make a web service callout
You can't make callouts after performing DML operations as the operation might be rolled back, and the callout can't be. In a trigger you are in the middle of a DML operation.

You need to use either the @future(callout=true) annotation on a method that does the actual callout or start a Salesforce batch process (that uses Database.AllowsCallouts), which in turn does the callout. As per the limits, you can make up to 10 callouts per transaction.

I would suggest starting a Salesforce batch from the trigger to do the callout and passing it a query (or the direct lead ids) of the records that need to be synced. You can directly set the size of the batch to stay within the callout limits and still allow the lead trigger to be correctly bulkified. There is no need to limit lead insertion to one record at a time.

In the trigger, remember to check if you are already in a future (System.isFuture()) or batch context (System.isBatch()) as you can't call a future method from a future method.

The one lead at a time restriction could be really problematic going forward if you ever need to bulk load data or use a managed package from another party that doesn't have the same restriction.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.