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
RvskRvsk 

Trigger Scenario : Creates the number of contacts which are equal to the number which we will enter in the Number of Locations field on the Account Object.

I have created a Custom field called “Number of Locations” on the Account Object (Data Type=Number)
Object : Account
Trigger: After Insert

This is my code:

trigger ContactsCreation on Account (after insert) {
list<contact> listContact = new list<contact>();
map<id,decimal> mapAcc = new map<id,decimal>();
  for(Account acc:trigger.new){
      mapAcc.put(acc.id,acc.NumberofLocations__c);
     }
        if(mapAcc.size()>0 && mapAcc!=null){
            for(Id accId:mapAcc.keyset()){
               for(integer i=0;i<mapAcc.get(accId);i++){
                 contact newContact=new contact();
                 newContact.accountid=accId;
                 newContact.lastname='contact'+i;
                 listContact.add(newContact);
                 }
              }
         }
if(listContact.size()>0 && listContact!=null)
insert listContact;

Can someone please explain the logic behind the no of creation of contact records based on location field.
Sample example: I have given account name,no of locations=2 in account object, then after two contacts record got created with lastname as contact0,contact1.
Why multiple contacts are creating as per the above logic?
 
AnudeepAnudeep (Salesforce Developers) 
Please try adding a duplicate check to your map. See post for sample code

 
RvskRvsk
Thanks for your response,Anudeep. As per question, it has to create the multiple contacts based on the no of locations field. I have given the sample example as well. Here, my doubt was according to the logic, how it getting created multiple contacts.
AMAN RANAAMAN RANA
Hi Rvsk,
multiple contacts are creating because of you used in the condition for(integer i=0;i<mapAcc.get(accId);i++) 

for example-you input 2 in the no.of location so it fullfil this condition if(mapAcc.size()>0 && mapAcc!=null) and ,
from the above condition for (integer i=0)the contact will start with the index/serial no.0[contact0]

and,for(i<mapAcc.get(accid);i++) the contacts which gonna print will be less than/not equal to 2 the number,
you input in no.of location field ,
after that
 if(listContact.size()>0 && listContact!=null)
insert listContact; that means you put the size of contact(2) in the list .

so the contact0 and contact1 created.

if you get your answer,mark this best answer or like.
 
Gonapati Bhavitha 11Gonapati Bhavitha 11
Hi Rvsk,
It is working Try once,

ApexHandler

     public void accrelatedContact(list<account> lstNewAcc)
     {
         list<contact> lstContact = new list<contact>();
       For(Account ac : lstNewAcc)   
       {
           if(ac.NumberofLocations__c >0)
           {
               for(Integer i=1 ; i<=ac.NumberofLocations__c;i++){
               Contact con = new Contact();
               con.AccountId = ac.id;
               con.LastName = ac.Name + i;
                   lstContact.add(con);
               }
               
           }
           
       }
           if(lstContact.size()>0)
           {
               insert lstContact;
           }
         
     }
    

This is Trigger
   trigger AccountTrigger on Account (After insert) {
       AccountHandler achndlr = new AccountHandler();
       if(trigger.isAfter && Trigger.isinsert)
       {
           achndlr.accrelatedContact(Trigger.new);

       }


if you get your answer,mark this best answer or like.
 
 
sanju 5sanju 5
A simple way try this trigger 

trigger NoOfLoctnCon on Account (after insert) {
list<Contact> conlist = new list<Contact>();
for(Account a:trigger.new){
if(a.NumberofLocations__c != null){
Decimal i = a.NumberofLocations__c ;
for(integer j=1; j<=i; j++){
Contact c = new Contact();
c.LastName = a.Name+i;
c.AccountId = a.Id;
conlist.add(c);
}
}
insert conlist;
}
}
Mounika Golla 7Mounika Golla 7
Can any one please help me on this trigger. ..
In account there is No_of_contact__c custom field, when we are giving No 5 in no.Of.contact field automatically create a 5 contact records, when we are change or increasing the number 5 to 7 then automatically 2 contact records has automatically created that means before 5+ 2= Total 7 records. Similarly if we change or decrease the value from 7 to 4 then automatically 4 contact records should be delete and remain 3 records that means 7-4 = 3.

Please anyone help for logic on update and delete operations
Saurav DadwalSaurav Dadwal
// this is working fine but can anyone explain why map was considered here? 

trigger accountTrigger on Account (after insert) {
   List<contact> cons=new List<contact>();
    for(account a : trigger.new){
          for(integer i=0; i<a.Number_of_contacts__c; i++){
              contact cc = new contact();
              cc.accountid=a.id;
              cc.lastname=a.name;
              cc.phone=a.phone;
                   cons.add(cc);
   }
       insert cons;
    }  


}