function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
viswadaviswada 

trigger for no contacts in accounts

HI ALL,

 

          I am new to devleopment.  i have  one custom filed in  account  named as 'NoofContacts ' data type is Integer,  i need tigger code  how many contacts are there in particular account, when i am inserting one contact in particular account, hte noofcontact fileld  in account increased one automatically, same as delete contact, the noofcontact  will be decreased, i am able  to do this, but i am logged ate updating the contact , if  i update the contact ,i will change the account,then old acocunt noofcontact filed  willbe decreased by one and new account noofcontacts filed is deleted by one, iam logged here ,plz any one help

 

ibtesamibtesam

why dont you use a roll up summary here??

i feel it would cut down all the pain of trigger.

Nageswara  reddyNageswara reddy

thz for responding,,  

      

 

            rollup summery fields are  performed when masterdetail  relationship between  objects. but   lookup relationship we have in between account and contact. that's why   i choosen  trigger..

Murthy vvrMurthy vvr

include trigger event 'after update' in the trigger header .

 

eg : trigger trig_name on obj_name (after update,after insert,after delete) {

            }

 

and moreover seperate the code into two categories, 

write noofcontacts increment code under

 if(isInsert or isUpdate)

{

 //write increment code here

}

if(isDelete or isUpdate)

{

 //write decrement code here

}

 

hope it'll help u.

correct me if i'm wrong