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
aaryansriaaryansri 

using trigger copy child object picklist value to parent object septrated by comma

Hi 


   Opportunity is parent object and Approvel by source is a child object. Under one parent object  multiple  child objects are there. Child object having  "source of child" picklist field values are(CCI,Risk,Revenu,SD).  Parent object also having "source" it is  text field.

 One parent is having multiple childs all the "source of child" values should  update to parent "source"  field seprated by comma. Dupplictes should not be repeat and even we update child  "source of child" that should update in related parent "source" text filed and even if we deleted child record  parent filed should based on that.


  Please help me on the above trigger.


  
Best Answer chosen by aaryansri
Vatsal KothariVatsal Kothari
Hi Ashok,

Refer below working code:
Trigger updateSource on  Order__c (after insert,after update) {
	
	Map<Id,Set<String>> optyMap = new Map<Id,Set<String>>();
	List<opportunity> optyList = new List<opportunity>();
	Set<String> sourceSet = new Set<String>();
	Set<String> sourceChild = new Set<String>();
	Set<Id> optyIds = new Set<Id>();
	
	for(Order__c app : Trigger.new){
		if(app.Opportunity__c != null){
			optyIds.add(app.Opportunity__c);
		}
	}
	
	for(Order__c app : [Select Id,Opportunity__c,source_of_child__c from Order__c where Opportunity__c IN: optyIds]){
		if(optyMap.containsKey(app.Opportunity__c)){
			optyMap.get(app.Opportunity__c).add(app.source_of_child__c);
		}else{
			if(app.source_of_child__c != null){
				sourceChild = new Set<String>();
				sourceChild.add(app.source_of_child__c);
				optyMap.put(app.Opportunity__c,sourceChild);
			}
		}
	}	
	
	for(Opportunity opp : [Select Id,source__c from Opportunity where Id IN: optyMap.keySet()]){
		sourceSet = new Set<String>();
		if(optyMap.containsKey(opp.Id)){
			sourceSet = optyMap.get(opp.Id);
			System.debug('***sourceSet'+sourceSet);
			if(sourceSet.size() > 0){
				String sourceStr = '';
				for(String s:sourceSet) {
					sourceStr += (sourceStr==''?'':',') + s; 
				}
				System.debug('***sourceStr'+sourceStr);
				opp.source__c = sourceStr;
				optyList.add(opp);
			}
		}
	}
	
	if(optyMap.size() > 0){
		update optyList;
	}
}
If this solves your problem, kindly mark it as the best answer.

Thanks,
Vatsal

All Answers

Vatsal KothariVatsal Kothari
Hi Ashok,

you can refer below code:
Trigger updateSource on  Approvel_by_source__c (after insert,after update) {
	
	Map<Id,Set<String>> optyMap = new Map<Id,Set<String>>();
	List<opportunity> optyList = new List<opportunity>();
	Set<String> sourceSet = new Set<String>();
	
	for(Approvel_by_source__c app : Trigger.new){
		if(!optyMap.containsKey(app.Opportunity__c)){			
			map.put(app.Opportunity__c,app.source_of_child);
		}else{
			if(app.Opportunity__c != null && app.source_of_child != ''){
				optyMap.get(app.Opportunity__c).add(app.source_of_child);
			}
		}
	}
	
	for(Opportunity opp : [Select Id,source__c from Opportunity where Id IN: optyMap.keySet()]){
		sourceSet = new Set<String>();
		if(optyMap.containsKey(opp.Id)){
			sourceSet = optyMap.get(opp.Id);
			if(sourceSet.size() > 0){
				String sourceStr = '';
				for(String s:sourceSet) {
					sourceStr += (sourceStr==''?'':',')+s; 
				}
				opp.source__c = sourceStr;
				optyList.add(opp);
			}
		}
	}
	
	if(opptyMap.size() > 0){
		update optyList;
	}
}
If this solves your problem, kindly mark it as the best answer.

Thanks,
Vatsal
aaryansriaaryansri
Hi Vatsal



   Thanks  for the responce. When trying the above code in line no 9 getting error of "Incompatible value type String for MAP<Id,SET<String>> at line 9 column 13". Can you please rectify that error and revert back what changes to be done.
Vatsal KothariVatsal Kothari
Try below updated Code:
Trigger updateSource on  Approvel_by_source__c (after insert,after update) {
	
	Map<Id,Set<String>> optyMap = new Map<Id,Set<String>>();
	List<opportunity> optyList = new List<opportunity>();
	Set<String> sourceSet = new Set<String>();
	
	for(Approvel_by_source__c app : Trigger.new){
		if(!optyMap.containsKey(app.Opportunity__c)){
			Set<String> sourceChild = new Set<String>();
			sourceChild.add(app.source_of_child);
			optyMap.put(app.Opportunity__c,sourceChild);
		}else{
			if(app.Opportunity__c != null && app.source_of_child != ''){
				optyMap.get(app.Opportunity__c).add(app.source_of_child);
			}
		}
	}
	
	for(Opportunity opp : [Select Id,source__c from Opportunity where Id IN: optyMap.keySet()]){
		sourceSet = new Set<String>();
		if(optyMap.containsKey(opp.Id)){
			sourceSet = optyMap.get(opp.Id);
			if(sourceSet.size() > 0){
				String sourceStr = '';
				for(String s:sourceSet) {
					sourceStr += (sourceStr==''?'':',') + s; 
				}
				opp.source__c = sourceStr;
				optyList.add(opp);
			}
		}
	}
	
	if(opptyMap.size() > 0){
		update optyList;
	}
}


aaryansriaaryansri
Hi  Vatsal

   I tried above code it is not storing multiple vaues in parent field .when creating 2 nd child record it overwriting the 1st  value in parent field .
Vatsal KothariVatsal Kothari
Hi Ashok,

Refer below working code:
Trigger updateSource on  Order__c (after insert,after update) {
	
	Map<Id,Set<String>> optyMap = new Map<Id,Set<String>>();
	List<opportunity> optyList = new List<opportunity>();
	Set<String> sourceSet = new Set<String>();
	Set<String> sourceChild = new Set<String>();
	Set<Id> optyIds = new Set<Id>();
	
	for(Order__c app : Trigger.new){
		if(app.Opportunity__c != null){
			optyIds.add(app.Opportunity__c);
		}
	}
	
	for(Order__c app : [Select Id,Opportunity__c,source_of_child__c from Order__c where Opportunity__c IN: optyIds]){
		if(optyMap.containsKey(app.Opportunity__c)){
			optyMap.get(app.Opportunity__c).add(app.source_of_child__c);
		}else{
			if(app.source_of_child__c != null){
				sourceChild = new Set<String>();
				sourceChild.add(app.source_of_child__c);
				optyMap.put(app.Opportunity__c,sourceChild);
			}
		}
	}	
	
	for(Opportunity opp : [Select Id,source__c from Opportunity where Id IN: optyMap.keySet()]){
		sourceSet = new Set<String>();
		if(optyMap.containsKey(opp.Id)){
			sourceSet = optyMap.get(opp.Id);
			System.debug('***sourceSet'+sourceSet);
			if(sourceSet.size() > 0){
				String sourceStr = '';
				for(String s:sourceSet) {
					sourceStr += (sourceStr==''?'':',') + s; 
				}
				System.debug('***sourceStr'+sourceStr);
				opp.source__c = sourceStr;
				optyList.add(opp);
			}
		}
	}
	
	if(optyMap.size() > 0){
		update optyList;
	}
}
If this solves your problem, kindly mark it as the best answer.

Thanks,
Vatsal
This was selected as the best answer
aaryansriaaryansri
Hi vastal

    Thanks for help code is working as required.

Thanks
Ashok