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
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.
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.
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!
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.
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.
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
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.
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
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
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.