You need to sign in to do that
Don't have an account?

I had faced following error in my trigger when i try to perform delete operation but it works fine for insert operation
Hi Friends,
I have custom field called NoContact__c It is a decimal data type in Account object,. My requirement is to increment the NoContact__c field each time when i try to insert the contact for associated account.And when i delete the contact that associate with Account, The NoContact__c filed should be decremented. For this requirement i had written trigger in this trigger inserting operation is working fine but when i try to perform delete operation it throws the following error. Can any one help me what mistake did i made in this trigger.
Error: execution of BeforeDelete caused by: System.NullPointerException: Attempt to de-reference a null object: External entry point".
Apex trigger:
trigger countcontacts on Contact (after insert,before delete) {
List<Contact>conlist=[SELECT Id,Name,contact.AccountId,Contact.Account.NoContact__c FROM Contact WHERE Id IN:trigger.new];
List<Account> toupdateaccounts=new List<Account>();
List<Account> updatedlist=new List<Account>();
for(Contact c: conlist){
c.Account.NoContact__c=c.Account.NoContact__c+1;
updatedlist.add(c.Account);
}
update updatedlist;
if(trigger.isDelete){
List<Contact> contactstodelete=[SELECT Id,Name,contact.AccountId,Contact.Account.NoContact__c FROM Contact WHERE Id IN:trigger.old];
for(Contact deletedcontact:contactstodelete){
deletedcontact.Account.NoContact__c=deletedcontact.Account.NoContact__c-1;
toupdateaccounts.add(deletedcontact.Account);
}
update toupdateaccounts;
}
}
I have custom field called NoContact__c It is a decimal data type in Account object,. My requirement is to increment the NoContact__c field each time when i try to insert the contact for associated account.And when i delete the contact that associate with Account, The NoContact__c filed should be decremented. For this requirement i had written trigger in this trigger inserting operation is working fine but when i try to perform delete operation it throws the following error. Can any one help me what mistake did i made in this trigger.
Error: execution of BeforeDelete caused by: System.NullPointerException: Attempt to de-reference a null object: External entry point".
Apex trigger:
trigger countcontacts on Contact (after insert,before delete) {
List<Contact>conlist=[SELECT Id,Name,contact.AccountId,Contact.Account.NoContact__c FROM Contact WHERE Id IN:trigger.new];
List<Account> toupdateaccounts=new List<Account>();
List<Account> updatedlist=new List<Account>();
for(Contact c: conlist){
c.Account.NoContact__c=c.Account.NoContact__c+1;
updatedlist.add(c.Account);
}
update updatedlist;
if(trigger.isDelete){
List<Contact> contactstodelete=[SELECT Id,Name,contact.AccountId,Contact.Account.NoContact__c FROM Contact WHERE Id IN:trigger.old];
for(Contact deletedcontact:contactstodelete){
deletedcontact.Account.NoContact__c=deletedcontact.Account.NoContact__c-1;
toupdateaccounts.add(deletedcontact.Account);
}
update toupdateaccounts;
}
}
what happen if any contact is Undelete ?? in this case your NoContact__c field on account is not update and above trigger is not good approach for achieve this
use below trigger
Please mark it best answer if it helps you so it make proper solution for others in future
and let me inform if it helps you
Thanks
All Answers
what happen if any contact is Undelete ?? in this case your NoContact__c field on account is not update and above trigger is not good approach for achieve this
use below trigger
Please mark it best answer if it helps you so it make proper solution for others in future
and let me inform if it helps you
Thanks