@future method: Unsupported parameter type

  • 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('[email protected]','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.

  • 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.

    Will you please give an example of "passing a `Set`"? vs passing `trigger.new`?

    @teewuane -- passing trigger.new is passing List. you might want to pass trigger.newMap.keySet() so you'll be passing set instead.

  • @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.

License under CC-BY-SA with attribution


Content dated before 7/24/2021 11:53 AM