I want to display count of the Contacts related to particular Account on a field in Account object.I want to make sure that it will work properly when I inserted new Contact or Deleted or Undelete also.In this post i'm going to explain how to implement this in Step by Step.
Choose The Right Object for Trigger
OutPut:
Notes:
1.Whenever your working with Undelete operation in Trigger you have to make sure that you have to use only Trigger.new or Trigger.newMap context variables to get the run time values of records.
2.Apex will treats Undelete operation as an insert operation so if you refer any Trigger.old or Trigger.oldMap you will not get any data so you will end up with an System.NullPointerException:Attempt to de-reference a null object
3. In after delete operation if you refer any Trigger.new you will be end with an same System.NullPointerException:Attempt to de-reference a null object because Trigger.new will support only Insert and Update operation .So we have to Trigger.old here.
Enjoy.......!!!
Choose The Right Object for Trigger
If you have doubt on which object I need to write trigger whether it's on Contact or Account object?My answer is blindly Contact because here we are going to track the number of contacts so it means that we need to have a glance on each and every change on a contact object.So we have to write a trigger on Contact Object
Source Code of the Trigger
Source Code of the Trigger
Trigger ContactCountTrigger on Contact(After insert,After Delete,After Undelete) { Set<Id> setAccountIds = new Set<Id>(); //Whenever your working with After Undelete operation you can access data through //Trigger.new or Trigger.newMap but not with Trigger.old or Trigger.oldmap variables if(Trigger.isInsert || Trigger.isUndelete) { for(Contact con : Trigger.new) { setAccountIds.add(con.AccountId); } } if(Trigger.isDelete) { //if you use Trigger.new below in place of Trigger.old you will end up with //System.NullPointerException:Attempt to de-reference a null object for(Contact con : Trigger.old) { setAccountIds.add(con.AccountId); } } List<Account> listAccs = [Select id,name,number_of_contacts__c ,(Select id from contacts) from Account where Id in : setAccountIds]; for(Account acc :listAccs) { acc.number_of_contacts__c = acc.contacts.size(); } update listAccs; }
OutPut:
Notes:
1.Whenever your working with Undelete operation in Trigger you have to make sure that you have to use only Trigger.new or Trigger.newMap context variables to get the run time values of records.
2.Apex will treats Undelete operation as an insert operation so if you refer any Trigger.old or Trigger.oldMap you will not get any data so you will end up with an System.NullPointerException:Attempt to de-reference a null object
3. In after delete operation if you refer any Trigger.new you will be end with an same System.NullPointerException:Attempt to de-reference a null object because Trigger.new will support only Insert and Update operation .So we have to Trigger.old here.
Enjoy.......!!!
Fantastic solution
ReplyDeleteThe job of a coder is full of challenges, stress, and confusion. Its requires you to be swift, accurate, and clear; or else you might face a DENIAL-a much dreaded word and one that every coder would probably want to remove from his or her dictionary.ergonomic chair for back pain
ReplyDeleteThis comment has been removed by the author.
ReplyDeletecan we write trigger on account to count related contact can u help?
ReplyDeleteNo
Deletethank you
ReplyDeleteSimple and Easy, Thank you so much
ReplyDeleteWhy did you skip after update event?
ReplyDeleteWhat if i insert two or more contacts on two different account using data loader or workbench, will this Trigger work fine.
ReplyDeleteYes
DeleteEA6FB
ReplyDeleteAydın Şehir İçi Nakliyat
Hakkari Parça Eşya Taşıma
Ardahan Evden Eve Nakliyat
Iğdır Evden Eve Nakliyat
Mardin Şehirler Arası Nakliyat
Hatay Şehir İçi Nakliyat
Probit Güvenilir mi
Bilecik Parça Eşya Taşıma
Ünye Oto Elektrik
F688F
ReplyDeleteAmasya Lojistik
Ardahan Parça Eşya Taşıma
Adıyaman Parça Eşya Taşıma
Samsun Şehir İçi Nakliyat
Bilecik Lojistik
Edirne Şehirler Arası Nakliyat
Ankara Parça Eşya Taşıma
Çankaya Boya Ustası
Milyon Coin Hangi Borsada
1D33D
ReplyDeleteeskişehir sohbet chat
ordu canli sohbet
canlı sohbet bedava
chat sohbet
kadınlarla rastgele sohbet
rize en iyi ücretsiz görüntülü sohbet siteleri
niğde rastgele görüntülü sohbet
manisa ücretsiz sohbet odaları
çankırı sesli görüntülü sohbet
886CD
ReplyDeleteparibu
btcturk
ilk kripto borsası
kraken
kucoin
coin nasıl alınır
bybit
gate io
huobi
F989D
ReplyDeletebinance ne demek
en az komisyon alan kripto borsası
mexc
bitcoin nasıl oynanır
filtre kağıdı
okex
probit
vindax
copy trade nedir
2C939
ReplyDeletefiltre kağıdı
referans kodu
bitexen
sohbet canlı
mexc
binance ne demek
kripto para telegram grupları
gate io
paribu
FEE0E
ReplyDeletegate io
mexc
bingx
bybit
copy trade nedir
en düşük komisyonlu kripto borsası
paribu
kripto ne demek
kripto kanalları telegram
F62FC
ReplyDeletegörüntülü canlı şov
E23CA
ReplyDeleteshow
F75DF
ReplyDeleteücretli canlı şov
D215B
ReplyDeletewhatsapp görüntülü show güvenilir
2B23937544
ReplyDeleteücretli şov
canli web cam show
görüntülü şov whatsapp numarası
ereksiyon hapı
themra macun
stag
geciktirici
vigrande
vega
48A39C0764
ReplyDeletebeğeni satın al