+ Start a Discussion
golugolu 

list returning null

Hi,
I am trying to run the below code but line 20 is giving me null. Can anyone please help me. I am able to reurn the list of activity ids but i am not able to pass the value in method in line 19
public class SMSHelper {
    public static void createActivities(List<Id> recordIdSet ,String message, Datetime scheduleDateTime){
        List<id> activityIds ;
        SMS_Configuration__c smsConfig = SMS_Configuration__c.getOrgDefaults();
        String status = scheduleDateTime == null?'Completed':'Scheduled';
        if(smsConfig.Use_Salesforce_Activity__c){
            createSalesforceActivities(recordIdSet, message, scheduleDateTime, status);
        }
        else{
            createCommunicatoActivities(recordIdSet, message, scheduleDateTime, status);
        }
        SMSHelper.createCommunicatOHistory(activityIds,message,scheduleDateTime);
        
        if(status == 'Completed'){
            
        } 
    }
    
    public static void createCommunicatOHistory(List<Id> activityIds, String message , Datetime scheduleDateTime){
        system.debug('activityIds' + activityIds);
        Communicato_History__c historyrec = new Communicato_History__c();
        historyrec.Message__c = message;
        historyrec.Schedule_Date_Time__c = scheduleDateTime;
        historyrec.communicat_o__Activity_Ids__c = String.valueOf(activityIds);
        // historyrec.Status__c = status;
        insert historyrec;
        system.debug('historyrec' + historyrec);
    }
    
    public static List<Id> 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;
        List <id> activityIds = new List<Id>();
        if(lstTask.size()>0){
            for(Task t : lstTask){
                activityIds.add(t.id);
            }
        }
        return activityIds;
        
    }
    
    public static List<id> createCommunicatoActivities(List<Id> recordIdSet ,String message, Datetime scheduleDateTime, String status){
        List<Communicat_O_activity__c> lstComActivity = new List<Communicat_O_activity__c>();
        for(String id: recordIdSet){
            Communicat_O_activity__c comObj = new Communicat_O_activity__c();
            
            comObj.Schedule_Date_Time__c = scheduleDateTime;
            comObj.Status__c = status;
            comObj.Message__c = message;
            comObj.Related_To__c = id;
            lstComActivity.add(comObj); 
        }
        
        insert lstComActivity;
        List <id> activityIds = new List<Id>();
        if(lstComActivity.size()>0){
            for(Communicat_O_activity__c com : lstComActivity){
                activityIds.add(com.id);
            }
        }
        return activityIds;
    }
}

 
Best Answer chosen by golu
Raj VakatiRaj Vakati
try t his'
public class SMSHelper {
    public static void createActivities(List<Id> recordIdSet ,String message, Datetime scheduleDateTime){
        List<id> activityIds = new List<Id>();
        SMS_Configuration__c smsConfig = SMS_Configuration__c.getOrgDefaults();
        String status = scheduleDateTime == null?'Completed':'Scheduled';
        if(smsConfig.Use_Salesforce_Activity__c){
          activityIds=  createSalesforceActivities(recordIdSet, message, scheduleDateTime, status);
        }
        else{
            activityIds =createCommunicatoActivities(recordIdSet, message, scheduleDateTime, status);
        }
        createCommunicatOHistory(activityIds,message,scheduleDateTime);
        
        if(status == 'Completed'){
            
        } 
    }
    
    public static void createCommunicatOHistory(List<Id> activityIds, String message , Datetime scheduleDateTime){
        system.debug('activityIds' + activityIds);
        Communicato_History__c historyrec = new Communicato_History__c();
        historyrec.Message__c = message;
        historyrec.Schedule_Date_Time__c = scheduleDateTime;
        historyrec.communicat_o__Activity_Ids__c = String.valueOf(activityIds);
        // historyrec.Status__c = status;
        insert historyrec;
        system.debug('historyrec' + historyrec);
    }
    
    public static List<Id> 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;
        List <id> activityIds = new List<Id>();
        if(lstTask.size()>0){
            for(Task t : lstTask){
                activityIds.add(t.id);
            }
        }
        return activityIds;
        
    }
    
    public static List<id> createCommunicatoActivities(List<Id> recordIdSet ,String message, Datetime scheduleDateTime, String status){
        List<Communicat_O_activity__c> lstComActivity = new List<Communicat_O_activity__c>();
        for(String id: recordIdSet){
            Communicat_O_activity__c comObj = new Communicat_O_activity__c();
            
            comObj.Schedule_Date_Time__c = scheduleDateTime;
            comObj.Status__c = status;
            comObj.Message__c = message;
            comObj.Related_To__c = id;
            lstComActivity.add(comObj); 
        }
        
        insert lstComActivity;
        List <id> activityIds = new List<Id>();
        if(lstComActivity.size()>0){
            for(Communicat_O_activity__c com : lstComActivity){
                activityIds.add(com.id);
            }
        }
        return activityIds;
    }
}

 

All Answers

Raj VakatiRaj Vakati
try this code
 
public class SMSHelper {
    public static void createActivities(List<Id> recordIdSet ,String message, Datetime scheduleDateTime){
        List<id> activityIds= new List<Id>() ;
        SMS_Configuration__c smsConfig = SMS_Configuration__c.getOrgDefaults();
        String status = scheduleDateTime == null?'Completed':'Scheduled';
        if(smsConfig.Use_Salesforce_Activity__c){
            createSalesforceActivities(recordIdSet, message, scheduleDateTime, status);
        }
        else{
            createCommunicatoActivities(recordIdSet, message, scheduleDateTime, status);
        }
        SMSHelper.createCommunicatOHistory(activityIds,message,scheduleDateTime);
        
        if(status == 'Completed'){
            
        } 
    }
    
    public static void createCommunicatOHistory(List<Id> activityIds, String message , Datetime scheduleDateTime){
        system.debug('activityIds' + activityIds);
        Communicato_History__c historyrec = new Communicato_History__c();
        historyrec.Message__c = message;
        historyrec.Schedule_Date_Time__c = scheduleDateTime;
        historyrec.communicat_o__Activity_Ids__c = String.valueOf(activityIds);
        // historyrec.Status__c = status;
        insert historyrec;
        system.debug('historyrec' + historyrec);
    }
    
    public static List<Id> 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;
        List <id> activityIds = new List<Id>();
        if(lstTask.size()>0){
            for(Task t : lstTask){
                activityIds.add(t.id);
            }
        }
        return activityIds;
        
    }
    
    public static List<id> createCommunicatoActivities(List<Id> recordIdSet ,String message, Datetime scheduleDateTime, String status){
        List<Communicat_O_activity__c> lstComActivity = new List<Communicat_O_activity__c>();
        for(String id: recordIdSet){
            Communicat_O_activity__c comObj = new Communicat_O_activity__c();
            
            comObj.Schedule_Date_Time__c = scheduleDateTime;
            comObj.Status__c = status;
            comObj.Message__c = message;
            comObj.Related_To__c = id;
            lstComActivity.add(comObj); 
        }
        
        insert lstComActivity;
        List <id> activityIds = new List<Id>();
        if(lstComActivity.size()>0){
            for(Communicat_O_activity__c com : lstComActivity){
                activityIds.add(com.id);
            }
        }
        return activityIds;
    }
}

 
golugolu
Hi @Raj,

This code is not working. It is still returning null. I want to pass the return values from createSalesforceActivities method.
Raj VakatiRaj Vakati
try t his'
public class SMSHelper {
    public static void createActivities(List<Id> recordIdSet ,String message, Datetime scheduleDateTime){
        List<id> activityIds = new List<Id>();
        SMS_Configuration__c smsConfig = SMS_Configuration__c.getOrgDefaults();
        String status = scheduleDateTime == null?'Completed':'Scheduled';
        if(smsConfig.Use_Salesforce_Activity__c){
          activityIds=  createSalesforceActivities(recordIdSet, message, scheduleDateTime, status);
        }
        else{
            activityIds =createCommunicatoActivities(recordIdSet, message, scheduleDateTime, status);
        }
        createCommunicatOHistory(activityIds,message,scheduleDateTime);
        
        if(status == 'Completed'){
            
        } 
    }
    
    public static void createCommunicatOHistory(List<Id> activityIds, String message , Datetime scheduleDateTime){
        system.debug('activityIds' + activityIds);
        Communicato_History__c historyrec = new Communicato_History__c();
        historyrec.Message__c = message;
        historyrec.Schedule_Date_Time__c = scheduleDateTime;
        historyrec.communicat_o__Activity_Ids__c = String.valueOf(activityIds);
        // historyrec.Status__c = status;
        insert historyrec;
        system.debug('historyrec' + historyrec);
    }
    
    public static List<Id> 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;
        List <id> activityIds = new List<Id>();
        if(lstTask.size()>0){
            for(Task t : lstTask){
                activityIds.add(t.id);
            }
        }
        return activityIds;
        
    }
    
    public static List<id> createCommunicatoActivities(List<Id> recordIdSet ,String message, Datetime scheduleDateTime, String status){
        List<Communicat_O_activity__c> lstComActivity = new List<Communicat_O_activity__c>();
        for(String id: recordIdSet){
            Communicat_O_activity__c comObj = new Communicat_O_activity__c();
            
            comObj.Schedule_Date_Time__c = scheduleDateTime;
            comObj.Status__c = status;
            comObj.Message__c = message;
            comObj.Related_To__c = id;
            lstComActivity.add(comObj); 
        }
        
        insert lstComActivity;
        List <id> activityIds = new List<Id>();
        if(lstComActivity.size()>0){
            for(Communicat_O_activity__c com : lstComActivity){
                activityIds.add(com.id);
            }
        }
        return activityIds;
    }
}

 
This was selected as the best answer
Ajay K DubediAjay K Dubedi
Hi Golu,

Yes, the output it gives is null, and it is because you don't put any data into the 'activityIds' List. I point out some mistakes which are in the following.
1. Correct Line-3:List<id> activityIds=new List<id>();
2. Your list of 'Id' is null and you pass them to the method 'SMSHelper.createCommunicatOHistory(activityIds,message,scheduleDateTime)'.
3. When you debug 'activityIds', it Obviously gave the null value.
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Try the below code:
public class SMSHelper {
    public static void createActivities(List<Id> recordIdSet ,String message, Datetime scheduleDateTime){
        List<id> activityIds=new List<id>();
        SMS_Configuration__c smsConfig = SMS_Configuration__c.getOrgDefaults();
        String status = scheduleDateTime == null?'Completed':'Scheduled';
        if(smsConfig.Use_Salesforce_Activity__c){
            activityIds=createSalesforceActivities(recordIdSet, message, scheduleDateTime, status);
        }
        else{
            activityIds=createCommunicatoActivities(recordIdSet, message, scheduleDateTime, status);
        }
        if(activityIds.size()>0){
        SMSHelper.createCommunicatOHistory(activityIds,message,scheduleDateTime);
        }
        if(status == 'Completed'){
            
        }
    }
    
    public static void createCommunicatOHistory(List<Id> activityIds, String message , Datetime scheduleDateTime){
        system.debug('activityIds' + activityIds);
        Communicato_History__c historyrec = new Communicato_History__c();
        historyrec.Message__c = message;
        historyrec.Schedule_Date_Time__c = scheduleDateTime;
        historyrec.communicat_o__Activity_Ids__c = String.valueOf(activityIds);
        // historyrec.Status__c = status;
        insert historyrec;
        system.debug('historyrec' + historyrec);
    }
    
    public static List<Id> 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;
        List <id> activityIds = new List<Id>();
        if(lstTask.size()>0){
            for(Task t : lstTask){
                activityIds.add(t.id);
            }
        }
        return activityIds;
        
    }
    
    public static List<id> createCommunicatoActivities(List<Id> recordIdSet ,String message, Datetime scheduleDateTime, String status){
        List<Communicat_O_activity__c> lstComActivity = new List<Communicat_O_activity__c>();
        for(String id: recordIdSet){
            Communicat_O_activity__c comObj = new Communicat_O_activity__c();
            
            comObj.Schedule_Date_Time__c = scheduleDateTime;
            comObj.Status__c = status;
            comObj.Message__c = message;
            comObj.Related_To__c = id;
            lstComActivity.add(comObj);
        }
        
        insert lstComActivity;
        List <id> activityIds = new List<Id>();
        if(lstComActivity.size()>0){
            for(Communicat_O_activity__c com : lstComActivity){
                activityIds.add(com.id);
            }
        }
        return activityIds;
    }
}
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks,
Ajay Dubedi