You need to sign in to do that
Don't have an account?
Niraj Kumar 9
Getting error in trigger:System.NullPointerException: Attempt to de-reference a null object:
Hi guys,
I am getting error :::Apex trigger UpdateActivityCountonAccountContactOptytask caused an unexpected exception, contact your administrator: UpdateActivityCountonAccountContactOptytask: execution of AfterInsert caused by: System.NullPointerException: Attempt to de-reference a null object: Trigger.UpdateActivityCountonAccountContactOptytask: line 135, column 1
I am getting error :::Apex trigger UpdateActivityCountonAccountContactOptytask caused an unexpected exception, contact your administrator: UpdateActivityCountonAccountContactOptytask: execution of AfterInsert caused by: System.NullPointerException: Attempt to de-reference a null object: Trigger.UpdateActivityCountonAccountContactOptytask: line 135, column 1
trigger UpdateActivityCountonAccountContactOptytask on task (after insert, after delete, after update) { map<id,integer> mopportunityCount = new map<id,integer>(); map<id,integer> maccountCount = new map<id,integer>(); map<id,integer> mcontactCount = new map<id,integer>(); Integer OptyCount = 0; Integer AccountCount = 0; Integer ContactCount = 0; if (trigger.isinsert || trigger.isUpdate ){ for(task t :trigger.new) { if(t.whatId != null) { if(string.valueOf(t.WhatId).left(3) == '001') { if(maccountCount .containsKey(t.WhatId)) { AccountCount = maccountCount.get(t.WhatId); } AccountCount = AccountCount + 1; maccountCount.put(t.WhatId,AccountCount ); } if(string.valueOf(t.WhatId).left(3) == '006') { if(mopportunityCount.containsKey(t.WhatId)) { OptyCount = mopportunityCount.get(t.WhatId); } OptyCount = OptyCount + 1; mopportunityCount.put(t.whatid,OptyCount); } } if(t.whoId != null) { if(string.valueOf(t.WhoId).left(3) == '003') { if(mcontactCount.containsKey(t.WhoId)) { ContactCount = mcontactCount.get(t.WhoId); } ContactCount = ContactCount + 1; mcontactCount.put(t.whoid,ContactCount ); } } } } if (trigger.isdelete){ for(task t :trigger.old) { if(t.whatId != null) { if(string.valueOf(t.WhatId).left(3) == '001') { if(maccountCount .containsKey(t.WhatId)) { AccountCount = maccountCount.get(t.WhatId); } AccountCount = AccountCount - 1; maccountCount.put(t.WhatId,AccountCount ); } if(string.valueOf(t.WhatId).left(3) == '006') { if(mopportunityCount.containsKey(t.WhatId)) { OptyCount = mopportunityCount.get(t.WhatId); } OptyCount = OptyCount - 1; mopportunityCount.put(t.whatid,OptyCount); } } if(t.whoId != null) { if(string.valueOf(t.WhoId).left(3) == '003') { if(mcontactCount.containsKey(t.WhoId)) { ContactCount = mcontactCount.get(t.WhoId); } ContactCount = ContactCount - 1; mcontactCount.put(t.whoid,ContactCount ); } } } } if(mcontactCount.keyset().size()>0) { list<contact> contactToUpdate = new list<contact>([SELECT id,Count_of_Activity__c FROM contact WHERE Id IN :mcontactCount.keyset()]); if(contactToUpdate.Size() > 0) for(contact c :contactToUpdate) { if(c.Count_of_Activity__c != null) c.Count_of_Activity__c = c.Count_of_Activity__c + mcontactCount.get(c.id); } if(contactToUpdate.size()>0) { update contactToUpdate; } } if(mopportunityCount.keyset().size()>0) { list<opportunity> opportunityToUpdate = new list<opportunity>([SELECT id,Count_of_Activity__c FROM opportunity WHERE Id IN :mopportunityCount.keyset()]); for(opportunity o :opportunityToUpdate ) { o.Count_of_Activity__c = o.Count_of_Activity__c + mopportunityCount.get(o.id); } if(opportunityToUpdate.size()>0) { update opportunityToUpdate; } } if(maccountCount.keyset().size()>0) { list<account> accountToUpdate = new list<account>([SELECT id,Count_of_Activity__c FROM account WHERE Id IN :maccountCount.keyset()]); for(account a :accountToUpdate ) { a.Count_of_Activity__c = a.Count_of_Activity__c + maccountCount.get(a.id); } if(accountToUpdate.size()>=0) { update accountToUpdate; } } }
Thanks
AMit Chaudhary
Counting is increaing by two while created task is one.
thanks.
Niraj Kumar.
Sorry Its working.
Thanks.
When i gone for checking with old account created (old means account created brfore active this trigger), Same error getting at here::::a.Count_of_Activity__c = a.Count_of_Activity__c + maccountCount.get(a.id);
if(maccountCount.containsKey(a.id) && maccountCount.get(a.id) != null)
{
a.Count_of_Activity__c = a.Count_of_Activity__c + maccountCount.get(a.id);
}
Let us know if this will help you.
During creation of field assigned default value is 0.
For Old records, Count of activity field is not showing default value 0 . If made it to 0 manually (By me) for old records, working fine.
For newly created records, by default its show and working fine.
Here I stuck.
Thanks.
I am using your code. Its already there.
if(maccountCount.containsKey(a.id) && maccountCount.get(a.id) != null)
{
a.Count_of_Activity__c = a.Count_of_Activity__c + maccountCount.get(a.id);
}
Thanks.