Skip to main content

πŸ” Seeking Advice from Salesforce Wizards πŸ”

 

Hey, Salesforce experts!

I'm currently tackling a tricky logic puzzle in Salesforce, and I could use some guidance.

 

Here's the scenario: I have a requirement to ensure that two lookup fields on a record maintain a one-to-one mapping. To accomplish this, I've implemented a before insert/update trigger to enforce the mapping. Additionally, I've set up a validation rule to prevent changes to the lookup fields once they are populated.

 

Now, according to Salesforce's order of execution, validation rules should run before triggers. However, I'm encountering a situation where the trigger error is firing first, even though I expect the validation rule to take precedence, when i am trying a update a lookup field after it's already populated.

 

Feel free to drop your thoughts in the comments below or reach out to me directly. Your expertise could be the missing piece of the puzzle!

 

#Salesforce Developer  #Automation  #Apex  #Triggers  #Validation Rule  #Validation Rules

1 commento
  1. 14 mag 2024, 22:04

    Hi @Adarsh Agrawal,

     

    Please refer to the documentation below.

     

    Triggers and Order of Execution | Apex Developer Guide | Salesforce Developers

     

    1. Salesforce performs different validation checks depending on the type of request.
      • For requests from a standard UI edit page, Salesforce runs these system validation checks on the record:
        • Compliance with layout-specific rules
        • Required values at the layout level and field-definition level
        • Valid field formats
        • Maximum field length
      • Additionally, if the request is from a user object on a standard UI edit page, Salesforce runs custom validation rules.
      • For requests from multiline item creation such as quote line items and opportunity line items, Salesforce runs custom validation rules.
      • For requests from other sources such as an Apex application or a SOAP API call, Salesforce validates only the foreign keys and restricted picklists. Before executing a trigger, Salesforce verifies that any custom foreign keys don’t refer to the object itself.
    2. Executes record-triggered flows that are configured to run before the record is saved.
    3. Executes all before triggers.
    4. Runs most system validation steps again, such as verifying that all required fields have a non-null value and runs any custom validation rules.

    Please check the fourth point.

     

    Thanks!

0/9000