function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Ashu sharma 38Ashu sharma 38 

assigning task

Hi,

I am setting a crieteria on lead,on which when it meets it changes the owner name and assigning them as the task,


please have a look into it.
I am able to update the owner name but not able to assign the task on it..
global class baychApexTask01 implements Database.Batchable<sObject>{
    
   public String query = 'select id,name,Last_Activity_Date__c from lead where Last_Activity_Date__c > 25';
    
    global database.queryLocator start(Database.BatchableContext BC) {
        return database.getQueryLocator(query);
        
    } //close start method
    
    global void execute(Database.BatchableContext BC, list<lead> scope) {
        List<Task> taskList = new List<Task>();
        user u=[select id, firstName from user where alias='ushar'];
        
        
        for(lead c : scope) {            
            c.OwnerId=u.Id;
            Task tsk = new Task();
            tsk.WhoId = c.Id;
            tsk.OwnerId = u.Id;
            tsk.ActivityDate = System.today();
            tsk.Status = 'Completed';
            tsk.Subject = 'Ownership Transferred';
            tsk.Priority ='Normal';
            taskList.add(tsk);
        }
        try{
            update scope;
        }
        catch(exception e){
            system.debug('=====' +e);
        }
        try{
            update scope;
        }
        catch(exception e){
            system.debug('=========' +e);
        }
        
        
    }    
    
    global void finish(Database.BatchableContext BC) {
        
        AsyncApexJob a = [Select Id, Status, NumberOfErrors, JobItemsProcessed,
                          TotalJobItems, CreatedBy.Email
                          from AsyncApexJob where Id =
                          :BC.getJobId()];
        
        
    } //close finish method
} //close class
sowmya Inturi 9sowmya Inturi 9
Hi Nitish,
Please remove the first try catch block and add the below code instaed.
if(taskList.size()>0){
            try{
                
                insert taskList;
                
            }
            catch(exception e){
                system.debug('=====' +e);
            }
        }

You are not inserting the task that is the reason the task is not created.


Thanks,
Sowmya