Reply
Regular Contributor
Markey1
Posts: 43
0
Accepted Solution

Trigger Help Needed - Update Child field based on update to Parent record

Hi, I'm fairly new to Triggers and am hoping someone can give me a bit of direction.

 

I want a custom field "Record_Mode__c" on Child Object B to be updated to value "Edit" when Parent Object A's custom field "Status__c" is updated to "Open". Basically, any time the parent record's status changes, I want the child record's Record Mode field to be updated accordingly.

 

The Record Mode change will then drive workflow that will flip record types and page layouts. I prefer to use out of the box solutions, but I cannot use cross object workflows as there is no master detail relationship between Parent Object A and Child Object B.

 

Any assistance with code or alternate solutions are much appreciated! 

Trusted Contributor
vishal@force
Posts: 429

Re: Trigger Help Needed - Update Child field based on update to Parent record

As you correctly stated that Workflows will not serve your purpose here, here is a code example for you:

 

I want a custom field "Record_Mode__c" on Child Object B to be updated to value "Edit" when Parent Object A's custom field "Status__c" is updated to "Open". Basically, any time the parent record's status changes, I want the child record's Record Mode field to be updated accordingly.

 

trigger tgrA on A__c (after insert, after update){
		
		// 1
		List<B__c> childRecords = [Select A__c, Record_Mode__c FROM B__c WHERE A__c IN Trigger.newMap.keySet()];
		
		// 2
		for(B__c child :childRecords){
			// 3
			if(trigger.isInsert && child.Status__c == 'Open'){
				child.Record_Mode__c = 'Edit';
			}
			// 4
			else if(trigger.isUpdate && child.Status == 'Open' && trigger.NewMap.get(child.A__c).Status__c != trigger.OldMap.get(child.A__c).Status__c){
				child.Record_Mode__c = 'Edit';
			}
		}
		
		// 5
		if(childRecords.size() > 0)
			update childRecords;
}

 

1. Querying child records from B Object related to A records that are being edited, inserted.
2. Iterating through all the child records and updating them as per the conditions.
3. If it is an insert, we simply check the status and if it meets the condition, update the child
4. If it is an update, we check if the Status value has been changed. If yes, then we update the child
5. Update all the related child records

 

This should help you get started.

Vishal, Certified Developer and Administrator
Kudos a post if it helps, mark it as solution if it solves.
http://exploretheforce.blogspot.in
Regular Contributor
Markey1
Posts: 43
0

Re: Trigger Help Needed - Update Child field based on update to Parent record

Vishal,

 

First, thank you for your reply, your assistance is appreciated!

 

I plugged in the code you provided and am receiving an error "Error: Compile Error: unexpected token: 'Trigger.newMap.keySet' at line 4 column 137".

 

trigger tgrMCCReq on MCC_Request__c (after insert, after update){
        
        // 1
        List<MCC_Relationship__c> childRecords = [Select MCC_Request__c, Record_Mode__c FROM MCC_Relationship__c WHERE MCC_Request__c IN Trigger.newMap.keySet()];
        
        // 2
        for(MCC_Relationship__c child :childRecords){
            // 3
            if(trigger.isInsert && child.Status__c == 'Open'){
                child.Record_Mode__c = 'Edit';
            }
            // 4
            else if(trigger.isUpdate && child.Status == 'Open' && trigger.NewMap.get(child.MCC_Request__c).Status__c != trigger.OldMap.get(child.MCC_Request__c).Status__c){
                child.Record_Mode__c = 'Edit';
            }
        }
        
        // 5
        if(childRecords.size() > 0)
            update childRecords;
}

 

Below is updated parent/child information... maybe I entered information incorrectly?

 

Parent Object: MCC_Request__c

Parent Object Field: Status__c

 

Child Object: MCC_Relationship__c

Child Object Field: Record_Mode__c

 

Logic I'm after: Any time MCC_Request__c.Status__c is changed/updated, if MCC_Request__c.Status__c = "Draft" or "Rejected", the MCC_Relationship__c.Record_Mode__c should be updated to "EDIT", otherwise, the MCC_Relationship__c.Record_Mode__c should be updated to "READ"

 

Hope this makes sense and thanks again for your help!

Trusted Contributor
vishal@force
Posts: 429
0

Re: Trigger Help Needed - Update Child field based on update to Parent record

oops, my bad! Sorry.

 

 

List<MCC_Relationship__c> childRecords = [Select MCC_Request__c, Record_Mode__c FROM MCC_Relationship__c WHERE MCC_Request__c IN :Trigger.newMap.keySet()];

 

I missed out the colon (:) in the SOQL.

 

Let me know if any questions/ issues! 

Vishal, Certified Developer and Administrator
Kudos a post if it helps, mark it as solution if it solves.
http://exploretheforce.blogspot.in
Regular Contributor
cbro
Posts: 83
0

Re: Trigger Help Needed - Update Child field based on update to Parent record

I am having an issue with my trigger.  The problem is probably that the Object B (Asset) is NOT a child record, but a related object via a lookup field.

 

Can anyone help with this code?  

 

It is not compiling b/c of it, I guess:   Save error: No such column 'Contract' on entity 'Asset'. If you are attempting to use a custom field, be sure to append the '__c' after the custom field name. Please reference your WSDL or the describe call for the appropriate names.

 

trigger ContractStatusUpdatesAsset on Contract (after insert, after update) 
{


// 1
List<Asset> childAsset = [Select Contract, Status FROM Asset WHERE Asset IN: Trigger.newMap.keySet()];
        
// 2
    for(Asset child :childAsset)
    {
// 3
    //if(trigger.isInsert && child.Status == 'Active', 'Pending')
    //    {
    //        child.Status = 'Inactive';
    //    }
// 4
    //else 
    if(trigger.isUpdate && child.Status == 'Active', 'Pending' && trigger.NewMap.get(child.Contract).Status != trigger.OldMap.get(child.Contract).Status)
        {
            child.Status = 'Inactive';
        }
    }
        
// 5
    if(childRecords.size() > 0)
            
    {
    //System.debug('Chris has values to insert = '+ newConList.size());
    try
         {
        update childAsset;
         }
         catch (System.Dmlexception e)  
         {
         system.debug (e); 
         }
    }
}
}

 

 


cbro
Certified Salesforce Admin

Connect with me
Linked In: www.linkedin.com/in/chrisbroach/