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
Kiran Kumar 225Kiran Kumar 225 

After update

Hi,
I have two objects A and B, A is the parent to B, both objacts have status fields, when ever Aobject Status is changed from "Inprogress" to "Complete"then all child B object status changed to "Complete".
Please send me the sample trigger.
Best Answer chosen by Kiran Kumar 225
Tolga SunarTolga Sunar
You don't need a trigger. A process you're going to set up in Process Builder should meet your requirement. In your process,
  • Specified Object: Object A & when record is created and edited
  • Criteria: Formula criteria = ISPICKVAL(PRIORVALUE(StatusOfA),"In Progress") && ISPICKVAL(StatusOfA, "Completed")
  • Immediate action: Update records, "Select a Record related to Object A", choose object B from list
    • StatusOfB = "Completed"
When an Object A record's status changes from in progress to Completed, all its child Object B records' statuses will also change to Completed.

All Answers

Tolga SunarTolga Sunar
You don't need a trigger. A process you're going to set up in Process Builder should meet your requirement. In your process,
  • Specified Object: Object A & when record is created and edited
  • Criteria: Formula criteria = ISPICKVAL(PRIORVALUE(StatusOfA),"In Progress") && ISPICKVAL(StatusOfA, "Completed")
  • Immediate action: Update records, "Select a Record related to Object A", choose object B from list
    • StatusOfB = "Completed"
When an Object A record's status changes from in progress to Completed, all its child Object B records' statuses will also change to Completed.
This was selected as the best answer
mrizmriz
I assume that objectB__c has a lookup field to objectA__c

Trigger:
trigger SampleTrigger on objectA__c (before Insert, after Insert,
                                      before Update, after Update,
                                      before Delete, after Delete{
// this section executes after objectA records have been updated	
if(Trigger.isAfter && Trigger.isUpdate){
		List<ObjectB__c> updateList = new List<objectB__c>();
		
		List<Id> objA_Ids = new List<Id>();
		for(ObjectA__c a:Trigger.new)
			if(a.status__c=='Completed')
				objA_Ids.add(a.id);
		
		//assumed that objectA_ID__c is the custom lookup field in objectB__c
		updateList=[SELECT id,status__c from objectB__c WHERE objectA_ID__c IN: objA_Ids];
		
		for(ObjectB__c b:updateList)
			b.status__c='Completed';
		
		update updateList;
	}
	// if you have too many things to do in trigger, then write a separate class and call its instance from trigger and perform all operations from apex class								  
									  
}


Like this answer, if it helped you understand the solution.
Mark this Best Answer, if it solved your problem.