You need to sign in to do that
Don't have an account?
HTANIRS
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.
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.
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
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;
}
Please use the below code for your requirement. Add the fields for the contact as per your requirement.
Please mark it as best answer, if you find it helpful.
Thanks
Thanks for the Solution. I could able to made working as expected.