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
Sankar Landa 15Sankar Landa 15 

autopopulate contact description with account name as wiprocontact1 for the first contact,wiprocontact2 for second contact so on for a partcular account record.Below is my trigger code.please suggest me with correct logic.Thanks in advance


trigger AccountContact on Contact (before insert) {
   
 List<Contact> cons=trigger.new;
      for(integer i=1;i<trigger.new.size();i++){
    for(Contact c:cons){
       
    Account acc=[select id,Name from Account  WHERE id=:c.Accountid];
        system.debug('----------------test1--------:'+acc);
        
     system.debug('----------------test1--------:'+i);
            c.Description=acc.Name+'-'+'contact'+i;
            system.debug('----------------test1--------:'+c.Description);
    }   
   
 }
}
Virendra ChouhanVirendra Chouhan
hi Sankar,

To achive this requirnment follow below steps;
  • Variables to use:
    • List of contacts.
    • One Map to hold accountId as an Key and list<Contact> as an value of Map.
  • Query contacts where accountId is in trigger.new
  • Now itreate above contact list and put values on Map<accoutnId, list<contact>>
  • Now iterate trigger.new and get value from Map based on accountId (hold value in a list)
  • find the size of list and add one init
    • list<contact> conlist = myMap.get(c.accountid);
      integer i = conList.size();
      c.Description='contact'+(i+1);