You need to sign in to do that
Don't have an account?
golu
callout from the trigger using queuable
In the below code , i am doing a callout in line 23 and 24. But in debug logs it throws error saying "i cannot do a callout from a trigger". I tried using future . But in future i cannot pass list of SObject. I am not able to pass the code in queueable context. Can anyone please help to make it work.
public class CommunicatoHistoryHelper implements Queueable, Database.allowscallouts{ public static void createUpdateActivity(List<Communicato_History__c> lstComHistory){ SMS_Configuration__c smsConfig = SMS_Configuration__c.getOrgDefaults(); List<id> activityids = new List<Id>(); List<Task> updateLstTask = new List<Task>(); List<Communicat_O_activity__c> updateComActivity = new List<Communicat_O_activity__c>(); for (Communicato_History__c com : lstComHistory) { String s = com.communicat_o__Activity_Ids__c; if(s.contains('(') || s.contains(')')) { s=s.replace('(','').replace(')',''); } system.debug('s' + s); if (com.Status__c == CommunicatoUtilityClass.ACTIVITY_STATUS_COMPLETED){ activityids.add(s); system.debug('activityids14' + activityids); } } List<API_Configuration__mdt> apiConfig; apiConfig = [SELECT Class_Name__c FROM API_Configuration__mdt LIMIT 1]; try{ if(activityids.size() > 0 && activityids.size() == 1){ Type t = Type.forName(apiConfig[0].class_name__c); MessageService message = (MessageService)t.newInstance(); message.sendSingleMessage('xxxxxxxxxx','testsms'); } } catch(Exception e){ system.debug('Class name is not set in api configuration metadata record'); } try{ if(activityids.size() > 1){ Type t = Type.forName(apiConfig[0].class_name__c); MessageService message = (MessageService)t.newInstance(); message.sendBulkMessage(); } } catch(Exception e){ system.debug('Class name is not set in api configuration metadata record'); } List<SObject> sObj = new List<SObject>(); String strQuery; List<Task> lstTask = new List<Task>(); List<Communicat_O_activity__c> comActivity = new List<Communicat_O_activity__c>(); if(smsConfig.Use_Salesforce_Activity__c){ strQuery = 'Select Status FROM Task WHERE Id IN ';strQuery += ':activityids'; } else{ strQuery = 'Select Status__c FROM Communicat_O_activity__c WHERE Id IN ';strQuery += ':activityids'; } sObj = Database.query(strQuery); for(sObject s : sObj){ if(smsConfig.Use_Salesforce_Activity__c){ Task t = (Task)s; t.status = CommunicatoUtilityClass.ACTIVITY_STATUS_COMPLETED; lstTask.add(t); } else{ Communicat_O_activity__c com = (Communicat_O_activity__c)s; com.status__c = CommunicatoUtilityClass.ACTIVITY_STATUS_COMPLETED; comActivity.add(com); } } if(lstTask.size()>0){ update lstTask; } if(comActivity.size()>0){ update comActivity; } } public void execute(QueueableContext context) { // this.createUpdateActivity(); } }
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.
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.
I hope you find the above solution helpful. If it does mark as best answer to help others too.
Thanks,
Ramesh D
You can try something like below
I hope you find the above solution helpful. If it does mark as best answer to help others too.
Thanks,
Ramesh D