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
yagna YSyagna YS 

Hello,When account is created contacts should be created as many shown based on the custom field number generated automatically in the account object...can any one please help me on trigger code.

mukesh guptamukesh gupta
HI Yangna
 
Can you please write done few line of code, after that we can help with you if you have any stuck point
Bhak 7Bhak 7
trigger ctcount on Account (before insert,after update,after insert) {
   set<id> setid = new set<id>();
     for(Account act : Trigger.new)
     {
        setid.add(act.Id);
        integer concount = [select count() from Contact where AccountId in: setid];
          if(act.No_of_Contacts__c > concount){
          system.debug('after increment equal' +act.No_of_Contacts__c );
        decimal createcontacts = act.No_of_Contacts__c - concount;
         system.debug('count of contacts' +concount);
            for(integer i=0 ; i<createcontacts; i++){
        List <contact> conlist = new List<contact>();
        contact con = new contact();
        con.LastName = act.Name;
        con.AccountId = act.id;
        conlist.add(con);
       insert conlist;
     }
        
          }
}
}
yagna YSyagna YS
Thanks a lot..for the code
Bhak 7Bhak 7
Hope it is working fine.. Make best answer ..