You need to sign in to do that
Don't have an account?
Amr 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
But i got error
caused by: System.NullPointerException: Attempt to de-reference a null object: Trigger.OnTaskAdded: line 6, column 1
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
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.
Open_Tasks__c is a field on Task object then below code works.
please do let me know if it helps you.
Regards,
Mahesh
Please find the below modified code:
I also tested the above code in my DE environment and everything looks good.
Regards,
Mahesh
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;
}
}
}