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
rusty_rusty_ 

Creating a trigger that updates a field in the detail of a master detail relationship

I am trying to create a trigger that updates a field in the detail of a master detail relationship.

 

For example...

 

I have a custom object called Risk and I have a junction object called Risk Relationship.

 

Risk Relationship is connected to Risk by a Master Detail relationship and a lookup relationship as well.

 

Risk Relationship is the detail in both relationships.

 

Is it possible to create a trigger so that whenever a Risk is edited, a trigger is fired to create a field update in the Risk Relationship custom object?

 

Is that possible?

qsdunnqsdunn

If Risk is the Master and Risk Relationship is the detail, could you not use a formula field instead?

aalbertaalbert

Yes. Add a Trigger on the Risk object (after update). That will fire anytime you update one or more Risk objects. 

 

pseudo-code:

 

 

trigger updateRiskRelationship on Risk__c (after update){
   Set<Id> riskIdsToQuery = new Set<Id>{};
   for(Risk__c r: Trigger.new){
      if r meets the criteria required to update child record
      riskIdsToQuery.add(r.Id);
   }

   List<RiskRelationship__c> relationships = [select id, field__c, field2__c from RiskRelationship__c where Risk__c IN :riskIdsToQuery];

   for(RiskRelationship__c rr: relationships){
      //do logic to update risk relationship
      rr.field__c = 'updating here';
   }

   update rr;

}

 

 

rusty_rusty_

Here is what I did so far

 

 

trigger updateRiskRelationshipDefinition on rcon__Risk__c (after update){
   Set<Id> riskIdsToQuery = new Set<Id>{};
   for(rcon__Risk__c r: Trigger.new){
      //if r meets the criteria required to update child record
      riskIdsToQuery.add(r.Id);
   }

   List<rcon__Risk_Relationship_Definition__c> relationships = [select id, Risk_Relationship_Workflow_Trigger_Field__c from rcon__Risk_Relationship_Definition__c where rcon__Risk__c IN :riskIdsToQuery];

   for(rcon__Risk_Relationship_Definition__c rr: relationships){
      //do logic to update risk relationship
      rr.Risk_Relationship_Workflow_Trigger_Field__c = rr.Risk_Relationship_Workflow_Trigger_Field__c + 1;
   }

   update rr;

}

 

 

It is giving me this error.

 

Error: Compile Error: Variable does not exist: rcon__Risk_Relationship_Definition__c at line 15 column 11

Also,  what do you mean by the section below? I want it to update the child record all the time.

if r meets the criteria required to update child record
rusty_rusty_

Sorry, I am still new to this. This is my first trigger that I am creating. I am probably going to be taking DEV501 in April to help me out with this.

 

This is the new edited code but it is still not working...

 

 

trigger updateRiskRelationshipDefinition on rcon__Risk__c (after update){
   Set<Id> riskIdsToQuery = new Set<Id>{};
   for(rcon__Risk__c r: Trigger.new){
      if (r.RecordTypeID == '012A0000000TYgA')
      riskIdsToQuery.add(r.Id);
   }

   List<rcon__Risk_Relationship_Definition__c> relationships = [select id, Risk_Relationship_Workflow_Trigger_Field__c from rcon__Risk_Relationship_Definition__c where rcon__Risk__c IN :riskIdsToQuery];

   for(rcon__Risk_Relationship_Definition__c rr: relationships){
      //do logic to update risk relationship
      rr.Risk_Relationship_Workflow_Trigger_Field__c = rr.Risk_Relationship_Workflow_Trigger_Field__c + 1;
   }

   update rr;

}

 

ErrorError: Compile Error: Variable does not exist: rr at line 15 column 11

 

Rajesh ShahRajesh Shah
Make it update relationships instead of rr. rr is a local variable of the for loop and not accesible outside it. In essence in your for loop, you are changing the value of relationships through rr so update relationships should work for you.
rusty_rusty_

This is the new code...

 

 

 

trigger updateRiskRelationshipDefinition on rcon__Risk__c (after update){
   Set<Id> riskIdsToQuery = new Set<Id>{};
   for(rcon__Risk__c r: Trigger.new){
      if (r.RecordTypeID == '012A0000000TYgA')
      riskIdsToQuery.add(r.Id);
   }

   List<rcon__Risk_Relationship_Definition__c> relationships = [select id, Risk_Relationship_Workflow_Trigger_Field__c from rcon__Risk_Relationship_Definition__c where rcon__First_Risk__c IN :riskIdsToQuery];

   for(rcon__Risk_Relationship_Definition__c rr: relationships){
      //do logic to update risk relationship
      rr.Risk_Relationship_Workflow_Trigger_Field__c = rr.Risk_Relationship_Workflow_Trigger_Field__c + 1;
   }

   update relationships;

}

 

it saves it but when I try to test it I get this error.

 

Error:Apex trigger updateRiskRelationshipDefinition caused an unexpected exception, contact your administrator: updateRiskRelationshipDefinition: execution of AfterUpdate caused by: System.NullPointerException: Attempt to de-reference a null object: Trigger.updateRiskRelationshipDefinition: line 12, column 56

 

 

Can anyone help?