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
SriramR14SriramR14 

who to write trigger for this? can anyone help me?

Create a field on Account Object Number of contacts  Write a trigger to create the number of contacts which are equal to the number which we will enter in the Number of Contacts field on the account object 

Eg:     if we enter No.of Contacts = 4, we need to create 4 contacts,     then if we are changing it from 4-5, then it should have only 5 contacts
Best Answer chosen by SriramR14
Ajay K DubediAjay K Dubedi
Hi sriram,
Please Try the below code and let me know if this works for you. If still need modifications do let me know.

Trigger :
trigger CreateAccountContact on Account (after insert,before update,before insert,after update) {
    if(trigger.isInsert && trigger.isAfter){
        NoOfContactWithAccountField.contactWithAccountField(trigger.new);
    }
    if(trigger.isUpdate && trigger.isAfter){
        NoOfContactWithAccountField.contactWithAccountField(trigger.new);
    }
}
Class :
public class NoOfContactWithAccountField {
    public static void contactWithAccountField(List<Account> accList){
        try{
            List<Contact> conList=new List<Contact>();
            Set<Id> Account_Id = new Set<Id> ();
            for(Account acc:accList){
                Account_Id.add(acc.Id);
                if(acc.Number_of_contacts__c != null){
                    Decimal noOfContact = acc.Number_of_contacts__c;
                    for(Integer i=0 ;i < noOfContact ;i++){
                        Contact conObj=new Contact();
                        conObj.LastName='Test'+i;
                        conObj.AccountId=acc.Id;
                        conList.add(conObj);
                    }
                }
            }
            List<Contact> get_related_con = [SELECT Id FROM Contact WHERE AccountId IN : Account_Id]; 
            if(get_related_con.size() > 0){
                delete get_related_con;
            }  
            if(conList.size() > 0) {
                Insert conList;
            }
        }
        catch (Exception ex) {
            system.debug(ex);
        }
    }    
}

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks,
Ajay Dubedi

All Answers

Deepali KulshresthaDeepali Kulshrestha
Hi Sriram,

Try the following code it may be helpful for you:
Trigger:
trigger CreateAccountContact on Account (after insert,before update) {
    if(trigger.isInsert && trigger.isAfter){
        CreateAccountContactClass.methodInsert(trigger.new);
    }
}
Trigger-Handler:
public class CreateAccountContactClass {
    public static void methodInsert(List<Account> accList){
        try{
            List<Contact> conList=new List<Contact>();
            for(Account acc:accList){
                if(acc.Number_of_contacts__c!=null){
                    Decimal noOfContact=acc.Number_of_contacts__c;
                    for(Integer i=0;i<noOfContact;i++){
                        Contact con=new Contact();
                        con.LastName='ContactLast';
                        con.AccountId=acc.Id;
                        conList.add(con);
                    }
                }
            }
            if(conList.size()>0){
                Insert conList;
            }
        }
        catch (Exception ex) {
                system.debug('Error->' + ex.getMessage() + '--LineNo->' + ex.getLineNumber());
            }
    }
    
}
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Deepali Kulshrestha
Ajay K DubediAjay K Dubedi
Hi sriram,
Please Try the below code and let me know if this works for you. If still need modifications do let me know.

Trigger :
trigger CreateAccountContact on Account (after insert,before update,before insert,after update) {
    if(trigger.isInsert && trigger.isAfter){
        NoOfContactWithAccountField.contactWithAccountField(trigger.new);
    }
    if(trigger.isUpdate && trigger.isAfter){
        NoOfContactWithAccountField.contactWithAccountField(trigger.new);
    }
}
Class :
public class NoOfContactWithAccountField {
    public static void contactWithAccountField(List<Account> accList){
        try{
            List<Contact> conList=new List<Contact>();
            Set<Id> Account_Id = new Set<Id> ();
            for(Account acc:accList){
                Account_Id.add(acc.Id);
                if(acc.Number_of_contacts__c != null){
                    Decimal noOfContact = acc.Number_of_contacts__c;
                    for(Integer i=0 ;i < noOfContact ;i++){
                        Contact conObj=new Contact();
                        conObj.LastName='Test'+i;
                        conObj.AccountId=acc.Id;
                        conList.add(conObj);
                    }
                }
            }
            List<Contact> get_related_con = [SELECT Id FROM Contact WHERE AccountId IN : Account_Id]; 
            if(get_related_con.size() > 0){
                delete get_related_con;
            }  
            if(conList.size() > 0) {
                Insert conList;
            }
        }
        catch (Exception ex) {
            system.debug(ex);
        }
    }    
}

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks,
Ajay Dubedi
This was selected as the best answer
SriramR14SriramR14
Thank you so much Deepali Kulshrestha, Ajay K Dubedi.,
The coding is working perfectly.,
Thank You So Much for your help.,

Yours,
Sriram.R
SriramR14SriramR14
Hi,
the coding working properly., but..,

its in account object we having 3 records., 

& each account record updating time, its creating contacts records

& 1st record for we updating the no.of.contact field box as 2 in account object, so its 2 records creating in contacts record.,

& 3 account records for we updating in the no.of.contact field as 2, so its 6 records totally of in contacts record.,

& if again we update the 1st account record means then its deleting its contacts 2 records & updating new 2 records.,

its nor deleting all records in contact field., which other 2 account records created.,

.....
you have solution for it?

Yours,
Sriram.R