+ Start a Discussion
golugolu 

passing a list value from void method

How do i pass the value of list from a static void method to another method.
 
public static void abcd(List<Id> activityIds, String message, Datetime scheduleDateTime){
            
        }





public static void createSalesforceActivities(List<Id> recordIdSet ,String message, Datetime scheduleDateTime, String status){
            List<Task> lstTask = new List<Task>();
            
            for(String id: recordIdSet){
                Task taskObj = new Task();
                taskObj.ActivityDate = Date.valueOf(scheduleDateTime);
                taskObj.Status = status;
                taskObj.Description = message;
                taskObj.WhoId = id;
                
                lstTask.add(taskObj); 
            }
            insert lstTask;
        }

So basically i want to pass the list of inserted task record values to static void abcd method. 
what is the best possible way?
Best Answer chosen by golu
Tad Aalgaard 3Tad Aalgaard 3
Collect all the Ids from the inserted Tasks into a Lilst and pass them to the method.
 
insert lstTask;
List<Id> leadIds = new List<Id>();
for(Task t : lstTask){
leadIds.add(t.Id);
}
System.debug('------' + leadIds);
abcd(leadIds, message, scheduleDateTime);

 

All Answers

Khan AnasKhan Anas (Salesforce Developers) 
Hi Golu,

Greetings to you!

Below is the sample code which I have tested in my org and it is working fine. Kindly modify the code as per your requirement.
 
public class SampleClass {
    
    public static List<Task> lstTask;
    
    public SampleClass(){
        lstTask = new List<Task>();
    }
    
    public static void abcd(List<Id> activityIds, String message, Datetime scheduleDateTime){
        for(Task t : lstTask){
            System.debug('Task -> ' + lstTask);
            // Do something
        }
    }
    
    public static void createSalesforceActivities(List<Id> recordIdSet ,String message, Datetime scheduleDateTime, String status){
        for(String id: recordIdSet){
            Task taskObj = new Task();
            taskObj.ActivityDate = Date.valueOf(scheduleDateTime);
            taskObj.Status = status;
            taskObj.Description = message;
            taskObj.WhoId = id;
            
            lstTask.add(taskObj); 
        }
        INSERT lstTask;
    }
}

I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.

Thanks and Regards,
Khan Anas
golugolu
Hi Khan Anas,

​​​​​​i want  public static void abcd(List<Id> activityIds, String message, Datetime scheduleDateTime) to take the parameter values from lsttask list. Is there a way to do it ? String message and schedule datetime will remain the same for all the records. only difference will be list<id> .
Tad Aalgaard 3Tad Aalgaard 3
Collect all the Ids from the inserted Tasks into a Lilst and pass them to the method.
 
insert lstTask;
List<Id> leadIds = new List<Id>();
for(Task t : lstTask){
leadIds.add(t.Id);
}
System.debug('------' + leadIds);
abcd(leadIds, message, scheduleDateTime);

 
This was selected as the best answer