+ Start a Discussion
Amr MohsenAmr Mohsen 

Trigger to change Case status after adding new task

Hello, 
I need to change Case status After adding task to that case 

My Code is 
trigger OnTaskAdded on Task (before insert) {
    try{
        Task insertedTask = trigger.new[0];
        Case caseToUpdate = insertedTask.What;
        caseToUpdate.Status = 'In Progress';
        update caseToUpdate;
    }catch(DmlException e) {
        
        System.debug('An unexpected error has occurred: ' + e.getMessage());
        
    }
}

But I get the following error when I try to add new task

Error: Invalid Data. 
Review all error messages below to correct your data.
Apex trigger OnTaskAdded caused an unexpected exception, contact your administrator: OnTaskAdded: execution of AfterInsert caused by: System.NullPointerException: Attempt to de-reference a null object: Trigger.OnTaskAdded: line 6, column 1


I need help please.
Mahesh DMahesh D
Hi Amr,

Please check the below code.
 
trigger CreatTask on Task (after update) {
    
    List<Case>  cList = new List<Case>();
    for(Task t: Trigger.new) {
        If(t.whatId.getsObjectType() == Case.sObjectType){
            Case c = new Case();
            c.Id = t.whatId;
            c.Status = 'In Progress';
            cList.add(c);
        }
    }
    
    if(!cList.isEmpty())
        update cList;
    
}

Please do let me know if it helps you.

Regards,
Mahesh
Amr MohsenAmr Mohsen
Thank you this really helps , but i want to make some modifications, 
I need to add a condition of task status is opened and also update numeric field in the case 
trigger CreatTask on Task (after insert) {
 
    List<Case>  cList = new List<Case>();
    for(Task t: Trigger.new) {
        If(t.whatId.getsObjectType() == Case.sObjectType){
            Case c = new Case();
            c.Id = t.whatId;
            if(t.Status == 'Opened'){
            c.Status = 'In Progress';
            c.Open_Tasks__c = Integer.valueOf(c.Open_Tasks__c) + 1;
            cList.add(c); 
            }
          
        }
    }
    
    if(!cList.isEmpty())
        update cList;
    
}

I get error again, can you help me please.
Mahesh DMahesh D
What error you are getting and what is this statement for

c.Open_Tasks__c = Integer.valueOf(c.Open_Tasks__c) + 1;

is it something like

c.Open_Tasks__c = Integer.valueOf(t.Open_Tasks__c) + 1;

Regards,
Mahesh
Amr MohsenAmr Mohsen
Thank you Mahesh, but I got the same previous error , also I want to increase the field itself in case since if the current value is 1 increase it by one , Something like c.open_tasks__c++ moreover when I try to check the status of tasks it raises the same error.
Mahesh DMahesh D
As you are creating the Case object here itself, open_tasks__c will be always null.
If you can explain more about your actual requirement then we will be able to help you.

Regards,
Mahesh
Mahesh DMahesh D
Hi Amar,

Please find the below modified code:
 
trigger CreateTask on Task (after insert) {
    
    Set<Id> caseIdSet = new Set<Id>();
        
    for(Task t: Trigger.new) {
        If(t.whatId.getsObjectType() == Case.sObjectType && t.Status == 'Not Started'){
            caseIdSet.add(t.whatId);
        }       
    }
    
    if(!caseIdSet.isEmpty()) {
        Map<Id, Case> caseMap = new Map<Id, Case>([Select Id, Status, Open_Tasks__c from Case where Id =: caseIdSet]);
        for(Task t: Trigger.new) {
            If(t.whatId.getsObjectType() == Case.sObjectType && t.Status == 'Not Started'){
                Case c = caseMap.get(t.whatId);
                if(c != null) {
                    c.Status = 'In Progress';
                    if(c.Open_Tasks__c == null)
                        c.Open_Tasks__c = 0;
                    c.Open_Tasks__c++;
                }
            }
        }
        
        if(!caseMap.isEmpty())
            update caseMap.values();
    }    
}

Here I used the status as 'Not Started', if you want, you can change it accordingly.

I also tested the above code in my DE environment and everything looks good.

Regards,
Mahesh