5

Background

I have a master-detail relationship between two objects, let's call them:

  • parent__c
  • child__c

Once the parent__c record is in a certain status__c we block any changes using a before update trigger.

But this same before update trigger on the parent__c object is blocking child__c records from being added.

Questions

  1. Is this an expected behaviour?
  2. What is the usual pattern to enable this situation and workaround this behaviour?
Robs
  • 9,336
  • 20
  • 106
  • 215

1 Answers1

2

Is this an expected behaviour?

Yes, this can happen because of Process Builder updates, triggers, and rollup summary fields. See Triggers and Order of Operation.

What is the usual pattern to enable this situation and workaround this behaviour?

The usual solution is to write triggers to set static variables to allow conditional bypasses. The specific solution often depends on the specific problem, however.

sfdcfox
  • 489,769
  • 21
  • 458
  • 806
  • Thanks sfdcfox. What the usual solution for allowing rollup summaries? – Robs Mar 21 '19 at 20:28
  • 1
    @Robs You'd probably want to check to see if that particular field was being updated, and if so, skip the error message. Each solution is likely going to be problem-dependent. – sfdcfox Mar 21 '19 at 20:31
  • How do I check to see if that particular field was being updated when it's the addition of detail record that's causing the trigger to fire? – Robs Mar 21 '19 at 20:33
  • 1
    @Robs In the parent record, you'd do something like for(parent__c record: trigger.new) { if(Trigger.oldmap.get(record.Id).RollupField__c != record.RollupField__c) { checkForErrors(record); } } This is again just pseudocode, but should get you started, I think. – sfdcfox Mar 21 '19 at 20:39
  • Of course! sorry, my brain just wasn't working! – Robs Mar 21 '19 at 20:40
  • 1
    @Robs No worries, we all have our moments. – sfdcfox Mar 21 '19 at 20:52