11

I am trying to copy records from one org to other via web services. I am calling a class which has logic written into a trigger. But @ future method is not taking sObject list as a parameter. Can someone suggest me an alternative?

The Code:

public class webServicesTriggerClass  
{  
  @future public static void webServicesMethod(list<MyFriend__c> mfList)  
  {     
   partnerSoapSforceCom.Soap sp = new partnerSoapSforceCom.Soap();   
   partnerSoapSforceCom.LoginResult lr = sp.login('hitesh.kumar.singh@accenture.com.varun','4Rajyavardhan');  
   soapSforceComSchemasClassNiralaapp.SessionHeader_element ses = new soapSforceComSchemasClassNiralaapp.SessionHeader_element();  
   ses.sessionId = lr.sessionId;   
   soapSforceComSchemasClassNiralaapp.MyFriendsDetailSoapApi vNapp=new soapSforceComSchemasClassNiralaapp.MyFriendsDetailSoapApi();  
   for(MyFriend__c mf:mfList)  
    {  
     string  s1= mf.myEmail__c;  
     string  s2=mf.friendName__c;   
     string  s3=mf.friendEmail__c;  
     string  s4=mf.aboutFriend__c;  
     integer i1=Integer.ValueOf(mf.age__c);  
     boolean result=vNapp.addFriendDetails(s1,s2,s3,s4,i1);  
     system.debug('################' +result);      
    }  
  }   
}  

Trigger-->

trigger webServicesTrigger on MyFriend__c (after insert)   
 {  
   webServicesTriggerClass.webServicesMethod(trigger.new);  
 }  

Error--

Error: Compile Error: Unsupported parameter type LIST at line 3 column 30

  • Please post your code and the exact error message you're encountering. – Davin Casey Aug 28 '14 at 9:18
21

As per documentation

You can't pass sObjects to @future methods:

Methods with the future annotation must be static methods, and can only return a void type. The specified parameters must be primitive data types, arrays of primitive data types, or collections of primitive data types. Methods with the future annotation cannot take sObjects or objects as arguments.

The reason why sObjects can’t be passed as arguments to future methods is because the sObject might change between the time you call the method and the time it executes. In this case, the future method will get the old sObject values and might overwrite them. To work with sObjects that already exist in the database, pass the sObject ID instead (or collection of IDs) and use the ID to perform a query for the most up-to-date record. The following example shows how to do so with a list of IDs.

So I would suggest passing a Set<ID> & requerying for the sObject records in your @future method.

  • 1
    Will you please give an example of "passing a Set<ID>"? vs passing trigger.new? – teewuane Jun 23 '16 at 17:03
  • @teewuane -- passing trigger.new is passing List<your_object>. you might want to pass trigger.newMap.keySet() so you'll be passing set<id> instead. – SamuelDev Apr 24 '17 at 13:15
8

@Future parameters must be primitive data types, arrays of primitive data types, or collections of primitive data types. Methods with the future annotation cannot take sObjects or objects as arguments.

Check here for more info

There is the workaround using JSON serialization/deserialization so that you can pass the serialized string of your objects to the future method. This is especially usefull when your objects are not available in the database to query them back.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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