I have a custom object called Junction__c with lookups to Custom__c, and Case. In the data model, Custom__c is also a child of Case.

There is a validation rule to ensure that any Custom__c record being related to a Junction__c record is actually a child of the related Case record referenced on the Junction__c record.

This is creating an issue in one of my triggers. The trigger is creating a new child Case record, and establishing links to both Custom__c and Case, but is throwing a validation rule error. The issue is because the record has not yet been inserted, and so Case is null.

How do I work around this? Is there a way to insert the case, retrieve it, and then update it, i.e. do two DML operations in a single trigger?

  • Yes, the record doesn't have to be "fully inserted" to get an Id for it. You simply need to insert the case (this will return an Id you can use), then insert the Custom__c (which you'll get another Id you can use), and finally link everything together. However, without specifically seeing your code, we can't really tell where you went wrong. It's probably just the wrong order of operations or something. – sfdcfox Nov 16 '18 at 23:34
  • Shouldn't the validation rule only apply when relating a Custom__c record to a Junction__c record? It sounds like that's not what's happening here. – Shane Steinfeld Nov 17 '18 at 15:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.