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
SalesforceASalesforceA 

Update Parent Text Field with Child "Name" values

I have a Parent object (Orders) with a Child object (States). The Child object is using the standard Name field as the "State" field.

I want to list all the UNIQUE states in a concatenated Text field on the Orders object.

My business allows for the same "State" to be related multiple times to the Parent but I only want to show each State one time in the text field that is populated by the trigger.

I'm a newbie when it comes to Triggers any help would be greatly appreciated!
Best Answer chosen by SalesforceA
logontokartiklogontokartik
Hope the below trigger helps. Make sure you change the object name and field names accordingly

trigger updateOrder on State__c (after insert, after update){
	
	Set<Id> parentIds = new Set<ID>();
	
	// Get all the Parent Ids (Orders ) in the Set
	for(State__c s : Trigger.new){
		parentIds.add(s.Order__c);
	}
	// QUery the Parent orders.
	List<Order__c> orders = new List<Order__c>();
	// Use the Child relations so you get all the related States for the Order Parent Object
	orders = [Select Id, Name, UniqueStates__c, (Select Id, Name from States__c) from Order__c where Id in :parentIds];
	
	// Iterate over each parent and child states
	for(Order__c o : orders){
			if(o.States__c != null && o.States__c.size()>0){
				for(State__c s: o.States__c){
					if(!o.UniqueStates__c.containsIgnoreCase(s.Name)){ // Check if the State is already in the Order State Field. if not add it otherwise skip
						o.UniqueStates__c += s.Name + ';';
					}
				}
			}
	}
	// Update the orders
	update orders;
}


All Answers

logontokartiklogontokartik
Hope the below trigger helps. Make sure you change the object name and field names accordingly

trigger updateOrder on State__c (after insert, after update){
	
	Set<Id> parentIds = new Set<ID>();
	
	// Get all the Parent Ids (Orders ) in the Set
	for(State__c s : Trigger.new){
		parentIds.add(s.Order__c);
	}
	// QUery the Parent orders.
	List<Order__c> orders = new List<Order__c>();
	// Use the Child relations so you get all the related States for the Order Parent Object
	orders = [Select Id, Name, UniqueStates__c, (Select Id, Name from States__c) from Order__c where Id in :parentIds];
	
	// Iterate over each parent and child states
	for(Order__c o : orders){
			if(o.States__c != null && o.States__c.size()>0){
				for(State__c s: o.States__c){
					if(!o.UniqueStates__c.containsIgnoreCase(s.Name)){ // Check if the State is already in the Order State Field. if not add it otherwise skip
						o.UniqueStates__c += s.Name + ';';
					}
				}
			}
	}
	// Update the orders
	update orders;
}


This was selected as the best answer
SalesforceASalesforceA
Thanks so much for your help!

I'm getting an error on line 17 "loop variable must be an SObject or list of State__c"
logontokartiklogontokartik
Sorry it should have been __r for all relation, so try changing all States__c to States__r

Also. You need to make sure you put in the right field name/relationship so when you navigate to State object, click on the parent field (order__c) it shows what the relationship name is (my guess is its States) so its States__r
SalesforceASalesforceA
Okay, I think I got the error figured out... I need to reference the related list as __r instead of __c.


SalesforceASalesforceA
Sweet! It works!

Can I bug you for one more tip? I need this to remove the State if they delete the related item. Sorry, I totally forgot to mention that earlier.


logontokartiklogontokartik
Hmm. Yeah you can try the below. Added before delete and o.UniqueStates__c = ''; should work

trigger updateOrder on State__c (after insert, after update,before delete){
	
	Set<Id> parentIds = new Set<ID>();
	
	// Get all the Parent Ids (Orders ) in the Set
	for(State__c s : Trigger.new){
		parentIds.add(s.Order__c);
	}
	// QUery the Parent orders.
	List<Order__c> orders = new List<Order__c>();
	// Use the Child relations so you get all the related States for the Order Parent Object
	orders = [Select Id, Name, UniqueStates__c, (Select Id, Name from States__r) from Order__c where Id in :parentIds];
	
	// Iterate over each parent and child states
	for(Order__c o : orders){
                        o.UniqueStates__c = '';
			if(o.States__r != null && o.States__r.size()>0){
				for(State__c s: o.States__r){
					if(!o.UniqueStates__c.containsIgnoreCase(s.Name)){ // Check if the State is already in the Order State Field. if not add it otherwise skip
						o.UniqueStates__c += s.Name + ';';
					}
				}
			}
	}
	// Update the orders
	update orders;
}


SalesforceASalesforceA
Hot dog! I got it to work by adding this statement to line 6

for(States__c s : trigger.isDelete ? trigger.old : trigger.new )

THANK YOU! THANK YOU! I've been struggling with this for days now!
Mark FardellaMark Fardella
Thanks guys this helped me solve a longstanding issue I had on multiple objects.  I am not a developer in the least so this was a quick win.  I did have to add "after delete" so the text field on the parent object was updated when a child record was deleted.  If you have written test coverage for this and are willing to share I would appreciate it as well.  Thanks again.
Tanvi KakkarTanvi Kakkar
Thanks Kartik .. This worked for me .. perfectly .. 

Please help me understand the basics of writing triggers --
I have a question for you ... In this we are updatind child to parent ... is that the reason the we need to get the parent ids in set ???  at the begining only ???

if we update parent to child : only one inner query and nested for will work ?? 
Please guide . Reply Awaited 
TJeedTJeed
Thank you so much.It worked for me.However,When I want to delete the child record,error message pops up saying system.exception.
please help :-(
vijay kumar kvijay kumar k
What about in delete case.if i use after delete event name will be deleted from populate field HOW?