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
Satabdi MoharanaSatabdi Moharana 

Trigger to update parent object with highest priority picklist value from the child object

I have 2 custom object, parent__c and child__c. I have a status__c field in child__c which is a picklist field with value a,b,c,d,e. The picklist values have priority like a>b>c>d>e. Need to write a trigger to update parent__c status with highest priority status from child__c.
mukesh guptamukesh gupta
Hi Satabdi,

you can use below code 
 
trigger UpdateStatus on child__c (after insert, after update) { 
  Map<ID, parent__c > parentItems = new Map<ID, parent__c >(); //Making it a map instead of list for easier lookup
  List<Id> listIds = new List<Id>();

  for (child__c childObj : Trigger.new {
    listIds.add(childObj.parent__c);
  }

  parentItems = new Map<Id, parent__c>([SELECT id, amount Name,(SELECT ID, Total_List_Price__c FROM childs__r) FROM parent__c WHERE ID IN :listIds]);

  for (child__c child: Trigger:new){
     parent__c myParentObj = parentItems.get(quote.parent__c);
     myParentOpp.status = child.status;
  }

  update parentOpps.values();
}

I am not getting your priority releated point. if you need help further please let me know.

Please Mark it As Best Answer If it Helps


Regards
Mukesh

 
ANUTEJANUTEJ (Salesforce Developers) 
Hi Satabdi,

You can try the below snippet:
 
trigger Child_trigger on Child__C(before insert,before update)
{
if(triggerisbefore && trigger.isinsert)
{
List<Parent__c> ParentToUpdate = new List<Parent__>();
for(Child__c tempc: trigger.new)
{
if(tempc.status__c=='a' && !ISBLANK(tempc.prent_id__c))
{
Parent p= new Parent__c();
p.id=tempc.parent_id__c;
p.PriVal= 'a';
ParentToUpdate.add(p);
}
}
if(ParentToUpdate.size()>0){update ParentToUpdate;}
}

if(trigger.isbefore && trigger.isupdate)
{

}
}

I have the above snippet for the insert and you need to insert the before update snippet in above code.

Let me know if it helps you and close your query by marking it as solved so that it can help others in the future.  

Thanks.
Suraj Tripathi 47Suraj Tripathi 47

Hi Satabdi,

Please find the solution.

trigger Data on Child__c (After Insert,before insert,before update) {

if(trigger.isAfter && trigger.isInsert){
	 set<id> parentset=new set<id>();
	for(child__c con:trigger.new){
	if(con.status__c=='a'){
	}
		parentset.add(con.parent__cid);
	}
	map<id,parent__c> parentMap=new map<id,parent__c>([select id,Status__c from parent__c where id in: parentset]);
	
  
for(child__c con:trigger.new){
if(con.parent__c==parentMap.get(con.parent__c).id){
     parentMap.get(con.parent__c).Status__c=con.Status__c;
}
}
update parentMap.Values();

}else if(trigger.isBefore && trigger.isUpdate){

 set<id> parentset=new set<id>();
	for(child__c con:trigger.new){
	if(con.status__c=='a'){
	}
		parentset.add(con.parent__c);
	}
	map<id,parent__c> parentMap=new map<id,parent__c>([select id,Status__c from parent__c where id in: parentset]);
	
  
for(child__c con:trigger.new){
if(con.parent__c==parentMap.get(con.parent__c).id){

     parentMap.get(con.parent__c).Status__c=con.Status__c;
}
}
update parentMap.Values();

}
}


I hope it will you.

Please mark it as Best Answer so that other people would take reference from it.

Thank You!

Satabdi MoharanaSatabdi Moharana
Hi Mukesh,

My requirement is, let's say for parent p1 there are 3 childs c1, c2, c3 with statuses as a, c, d respectively, then the status of p1 should be a.
Similarly for parent p2 there are 2 childs c4, c5 with statuses as b, d respectively, then the status for p2 should be b.
the priorities of statuses goes like a>b>c>d>e. Please help me with the code.

Regards,
Satabdi.