+ Start a Discussion
Amr MohsenAmr Mohsen 

Update case values on task creating using apex trigger

Hello, 
I'm trying to update values in case when a task is created inside it, I want to update string value 'Case Status' and increase numeric field by one

My Code is 
 
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;
    
}

But i got error 

caused by: System.NullPointerException: Attempt to de-reference a null object: Trigger.OnTaskAdded: line 6, column 1
Mahesh DMahesh D
Hi Amar,

Please find the below modified code:

Here c.Open_Tasks__c is always null, I am not sure why you want to convert into Integer and adding 1.
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';
			if(c.Open_Tasks__c != null)
				c.Open_Tasks__c = Integer.valueOf(c.Open_Tasks__c) + 1;
            cList.add(c); 
            }
          
        }
    }
    
    if(!cList.isEmpty())
        update cList;
    
}


Open_Tasks__c is a field on Task object then below code works.
 
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';
			if(t.Open_Tasks__c != null)
				c.Open_Tasks__c = Integer.valueOf(t.Open_Tasks__c) + 1;
            cList.add(c); 
            }
          
        }
    }
    
    if(!cList.isEmpty())
        update cList;
    
}

please do let me know if it helps 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();
    }    
}

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

Regards,
Mahesh
Prerna BhallaPrerna Bhalla
trigger CreatTask on Task (after insert) {
 
    List<Id>  cList = new List<Id>();
    List<Case>  cList1 = new List<Case>();
 
    for(Task t: Trigger.new) {
    
    system.debug('pppppp'+t.whatId.getSObjectType().getDescribe().getName());
        If(t.whatId.getSObjectType().getDescribe().getName() == Case.getSObjectType().getDescribe().getName()){
     
           clist.add(t.whatId);
             system.debug('oooo'+clist);
            }
          
        }
    
    if(!cList.isEmpty())
      {

      cList1=[select count__c from Case where Id in :clist];
             system.debug('oooo'+cList1);
      for(Case c :cList1)
      {
      c.count__c+=1;
       Update c;
      }
     
      }
    
}