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
Margaret FleischakerMargaret Fleischaker 

Pass webservice a collection of objects

We need to add a trigger on Lead insert that calls a web service and returns a value, setting a field on the Lead.

I know that I'll have to use the @future annotation. But am I going to run into limits if say, 500 leads are inserted at the same time? Instead of the below code...
Trigger newLeadTrigger on Trigger (after insert)
    for(Lead l : Trigger.new){
       Util.callWebService(l);
   }
} 


public class Util {
@future
  public void callWebService(Lead l){
     /* 
        Web service callout.
    */
  }
}
Would it be possible to do something like this, and only do one callout rather than 500?
Trigger newLeadTrigger on Trigger (after insert)
        Util.callWebService(Trigger.new);
}

public class Util {
@future
  public void callWebService(List<Lead> leads){
     /* 
        Web service callout.
    */
  }
}

I haven't worked with web services before and I'm obviously a little lost, so any guidance would be appreciated. Thanks!

 
pconpcon
Yeah, you cannot do that because @future calls can only have primatives passed to it.  You could do something like:
 
Trigger newLeadTrigger on Trigger (after insert) {
    List<String> leadList = new List<String>();

    for (Lead l : Trigger.new) {
        leadList.add(l.serialize());
    }

    Util.callWebService(List<String> leadList);
}
 
public class Util {
    @future
    public void callWebService(List<String> leadStrings) {
        List<Lead> leads = new List<Lead>();

        for (String s : leadStrings) {
            leads.add((Lead) JSON.deserialize(s, Lead.class));
        }

        // Do something
    }
}

This isn't the most efficient way to handle this as the Heap size can get a bit big since you'll be esentially doubling the Lead list in the trigger.  Alternately you could just pass it a Set of Lead ids and re-query them in the future call.