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
HTANIRSHTANIRS 

apex trigger to insert record when checkbox is clicked

Hi Friends,

I need assistance in working on Trigger. I have a requirement, where I need to insert contact when Checkbox is clicked in account.

Eg: I have 3 checkboxes Electronics, Fashion, Furnitures. If I select 2 checkboxes Electronics and Fashion and save the Account. Then, I need to insert 2 contacts with field selected as Electronics and field selected as Fashion in another record.

I need assistance in this requirement.

Thanks.
Best Answer chosen by HTANIRS
Danish HodaDanish Hoda
Hi there, 
You can achieve it like below:

Trigger accountTrigger on Account(after update){
Map<Id, List<String>> mapObj = new Map<Id, List<String>>();
List<Contact> contList = new List<Contact>();

for(Account accObj : trigger.New){
List<String> strList = new List<String>();
if(accObj.check_Elect__c == true){
strList.add('Electronics');
}if(accObj.check_Fash__c == true){
strList.add('Fashion');
}if(accObj.check_Furn__c == true){
strList.add('Furnitures');
}
if(!strList.isEmpty()){
mapObj.put(accObj.Id, strList);
}
}
if(!mapObj.isEmpty()){
Contact contObj;
for(Account accObj : trigger.New){
for(String str : mapObj.get(accObj.Id)){
contObj= new Contact();
contObj.Field__c = str;        // I am assuming Field__c is a picklist field with the values - Electronics,Fashion,Furnitures
contList.add(contObj);
}
}
}
if(!contList.isEmpty())    insert contList;
}

All Answers

Danish HodaDanish Hoda
Hi there, 
You can achieve it like below:

Trigger accountTrigger on Account(after update){
Map<Id, List<String>> mapObj = new Map<Id, List<String>>();
List<Contact> contList = new List<Contact>();

for(Account accObj : trigger.New){
List<String> strList = new List<String>();
if(accObj.check_Elect__c == true){
strList.add('Electronics');
}if(accObj.check_Fash__c == true){
strList.add('Fashion');
}if(accObj.check_Furn__c == true){
strList.add('Furnitures');
}
if(!strList.isEmpty()){
mapObj.put(accObj.Id, strList);
}
}
if(!mapObj.isEmpty()){
Contact contObj;
for(Account accObj : trigger.New){
for(String str : mapObj.get(accObj.Id)){
contObj= new Contact();
contObj.Field__c = str;        // I am assuming Field__c is a picklist field with the values - Electronics,Fashion,Furnitures
contList.add(contObj);
}
}
}
if(!contList.isEmpty())    insert contList;
}
This was selected as the best answer
sachinarorasfsachinarorasf
Hi HTANIRS,

Please use the below code for your requirement. Add the fields for the contact as per your requirement.
trigger AccountTrigger on Account (after insert) {
   
    if(Trigger.isInsert && Trigger.isafter){
        CreateContactHandler.insertNewContact(trigger.new);
    }
}


public class CreateContactHandler {
    public static void insertNewContact(List<Account> accList){
        try{
            List<Contact> conList= new List<Contact>();
            for(Account accObj: accList){
               if(accObj.Electronics__c){
                    Contact conObj= new Contact();
                    conObj.LastName= 'Electronics';
                    conObj.AccountId=accObj.id;
                    conList.add(conObj);
                }
				if(accObj.Fashion__c){
                    Contact conObj= new Contact();
                    conObj.LastName= 'Fashion';
                    conObj.AccountId=accObj.id;
                    conList.add(conObj);
                }
				if(accObj.Furnitures__c){
                    Contact conObj= new Contact();
                    conObj.LastName= 'Fashion';
                    conObj.AccountId=accObj.id;
                    conList.add(conObj);
                }
            }
            if(conList.size()>0){
                insert conList;
            }
        }
        catch(Exception e){
            System.debug('exception is '+e.getMessage());
        }
    }
    
}

Please mark it as best answer, if you find it helpful.

Thanks

 
HTANIRSHTANIRS
Hi Danish,

Thanks for the Solution. I could able to made working as expected.