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
ViswaViswa 

creating record based on picklist value

(Master detail relation b/w Account and  Hobbies object)
In Account, a muli picklist called 'Hobbies'. Whenever I selected a few values from the picklist and saved the Account record.....I need to create records in Hobbie's obj with those picklist values.
Best Answer chosen by Viswa
VinayVinay (Salesforce Developers) 
Hi Viswanath,

Try below snippet.
 
Trigger Handler
================

trigger CreateChildTrigger on Account  (after insert, after update) {
	CreateChildHandler.createRecord(Trigger.old, Trigger.new);
}

Controller
===========

public class CreateChildHandler {
	
	public static void createRecord(List<Account > lstOld, List<Account > lstNew) {
		List<Hobbies__c> lstNewCL = new List<Hobbies__c>();
		
		for(Integer index = 0; index < lstNew.size(); index++) {
			Hobbies__c objCL = new Hobbies__c();
			String oldPicklistValues = '';
			String newPicklistValues = '';
			
			if(Trigger.isUpdate && (
			  (lstOld[index].Country__c != lstNew[index].Country__c) 
			||(lstOld[index].State__c != lstNew[index].State__c)
			|| ...
			|| ...)) { //Add all 15 fields here with OR cndition like added for "Country" and "State"
				objCL.Account  = lstNew[index].Id;
				
				if(lstOld[index].Country__c != lstNew[index].Country__c) {
					oldPicklistValues = oldPicklistValues + lstOld[index].Country__c + ',';
					newPicklistValues = newPicklistValues + lstNew[index].Country__c + ',';
				}
				if(lstOld[index].State__c != lstNew[index].State__c) {
					oldPicklistValues = oldPicklistValues + lstOld[index].State__c + ',';
					newPicklistValues = newPicklistValues + lstNew[index].State__c + ',';
				}
				.....
				..... //Add if condition for all remaining pikclist here
				.....
				
				//Finished if, then
				objCL.Old_value = oldPicklistValues;
				objCL.New_value = newPicklistValues;
				objCL.action = '';
				//Add Other required fields 
				
				lstNewCL.add(objCL);
				
			}
			else if(Trigger.isInsert) {
				objCL.Account  = lstNew[index].Id;
				newPicklistValues = lstNew[index].Country__c + ',' + lstNew[index].State__c + ',' + <............. Add all remaining fields here>;
				objCL.Old_value = ''; //There is no old value.
				objCL.New_value = newPicklistValues;
				objCL.action = '';
				//Add Other required fields 
				
				lstNewCL.add(objCL);
			}
		}
		
		//Insert record
		if(!lstNewCL.isEmpty()) {
			insert lstNewCL;
		}
	}
	
}

Kindly update logic as per your requirement.

Hope above information was helpful.

Please mark as Best Answer so that it can help others in the future.

Thanks,
Vinay Kumar