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
thirumaghal baskarthirumaghal baskar 

Trigger to create contact automatically on entering the Number(custom Field) in account.On entering 3 in the custom field 3 contact records should be created

Best Answer chosen by thirumaghal baskar
Khan AnasKhan Anas (Salesforce Developers) 
Hi Thirumaghal,

Greetings to you!

Please try the below code, I have tested in my org and it is working fine. Kindly modify the code as per your requirement.
 
trigger MultipleConBasedOnAccNumber on Account (after insert, after update) {
    
    List<Contact> contactFinalListToInsert = New List<Contact>();
    
    if(Trigger.isInsert || Trigger.isUpdate){
        for(Account acc : Trigger.New) {
            if(acc.Number__c != null) {
                List<Contact> fetchingAlreadyExistedRecords = [Select Id FROM Contact WHERE AccountId =:acc.Id];
                
                if(fetchingAlreadyExistedRecords.IsEmpty()) {
                    // We are only creating a records when there is no Contact records exists.
                    for(Integer i=0; i<acc.Number__c; i++) {
                        Contact con = New Contact();
                        
                        con.AccountId = acc.Id;
                        con.LastName = 'Sample Name ' + i;
                        contactFinalListToInsert.add(con);
                    }
                }
                
            }
            
            try{
                if(!contactFinalListToInsert.IsEmpty()){
                    INSERT contactFinalListToInsert;
                }
            }
            catch(Exception e){
                System.debug('The thrown exception for CreatingAutoRecords is:: ' + e.getMessage());
            }
        }
    }
}

I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.

Thanks and Regards,
Khan Anas

All Answers

Khan AnasKhan Anas (Salesforce Developers) 
Hi Thirumaghal,

Greetings to you!

Please try the below code, I have tested in my org and it is working fine. Kindly modify the code as per your requirement.
 
trigger MultipleConBasedOnAccNumber on Account (after insert, after update) {
    
    List<Contact> contactFinalListToInsert = New List<Contact>();
    
    if(Trigger.isInsert || Trigger.isUpdate){
        for(Account acc : Trigger.New) {
            if(acc.Number__c != null) {
                List<Contact> fetchingAlreadyExistedRecords = [Select Id FROM Contact WHERE AccountId =:acc.Id];
                
                if(fetchingAlreadyExistedRecords.IsEmpty()) {
                    // We are only creating a records when there is no Contact records exists.
                    for(Integer i=0; i<acc.Number__c; i++) {
                        Contact con = New Contact();
                        
                        con.AccountId = acc.Id;
                        con.LastName = 'Sample Name ' + i;
                        contactFinalListToInsert.add(con);
                    }
                }
                
            }
            
            try{
                if(!contactFinalListToInsert.IsEmpty()){
                    INSERT contactFinalListToInsert;
                }
            }
            catch(Exception e){
                System.debug('The thrown exception for CreatingAutoRecords is:: ' + e.getMessage());
            }
        }
    }
}

I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.

Thanks and Regards,
Khan Anas
This was selected as the best answer
thirumaghal baskarthirumaghal baskar
It works great!! Thanks a lot!
Khan AnasKhan Anas (Salesforce Developers) 
It's my pleasure. I’m glad I was able to help!

Kindly mark this as solved if it's resolved so that it gets removed from the unanswered queue which results in helping others who are encountering a similar issue. *(After you choose the best answer the question is marked as “resolved”)*

May the (Sales)force be with you :)
Happy Learning!


Regards,
Khan Anas
thirumaghal baskarthirumaghal baskar
Hi Khan Anas,

I have an update with the same question, if i need to update the number(custom field) from 3 to 5. i need to get 2 more contact records created. and not 8. as i dont need the total.