1

Most Trigger frameworks come with trigger bypass functionality that allow you to selectively bypass triggers or trigger contexts for certain operations.

For example, say you have CustomObject__c with a Master-Detail relationship to Account, and Account has a roll-up summary to SUM all CustomObject__c records. Every time you enter a new CustomObject__c record, it updates the Account and does After Update logic. You may not want Account afterUpdate to fire every time you save a new CustomObject__c, so you leverage your trigger framework to bypass it with something like this:

TriggerHandler.bypass('AccountTriggerHandler');
insert customObjectRecords; // Insert but don't run Account Trigger
TriggerHandler.clearBypass('AccountTriggerHandler');

This works great when working with Visualforce or standard UI (with slightly modified usage).

However, so far I've been unable to get this to work with LWC because LWC executes the Apex bypass method in one transaction, and the lightning-record-edit-form.submit() function in another transaction.

Example:

import byPassMethod from '@salesforce/apex/SalesCallLWCController.byPassAccountUpdate';

byPassMethod({handlerName:'AccountTriggerHandler'})
  .then(result => {
    console.log('Here-->'+result);
    if(result){
      this.template.querySelector('.sales-call-form').submit(formFields);
    }
  })

The above results in the Bypass being enabled successfully, but in a separate transaction from the .submit(formFields).

Question: What are our options for properly leveraging trigger bypasses with LWC, or for that matter, establishing any type of transaction value states in the same transaction as .submit()?

Do we need to instead build an Apex method to handle save and scrap the idea of .submit() altogether? Is that even a viable option for LWC? Or is there any other alternative that I'm not thinking of?

1

Yes, you can go with the apex to save the data. lightning-record-edit-form.submit() is the prebuilt functionality and you don't have that control over it to bypass the trigger. But you can prevent the default behavior of the submit method and move the update logic to an apex class. In this way, you can leverage all other features of lightning-record-edit-form like validations, lightning input fields, etc.

You will need to handle the onsubmit event of the record edit form. In that, you can get all the fields from the form.

handleSubmit(event){
   event.preventDefault();
   const fields = event.detail.fields;
   byPassMethod({handlerName:'AccountTriggerHandler', record: fields})
       .then(result => {
       })
}

apex code

byPassMethod(String handlerName, CustomObject__c record){
    TriggerHandler.bypass('AccountTriggerHandler');
    insert record; // Insert but don't run Account Trigger
    TriggerHandler.clearBypass('AccountTriggerHandler');
}

Other Approach

Sharing another approach just in case you want to think of. You can create one hidden field on the record to mention the list of comma-separated handlers you want to bypass. You can set these bypasses in before trigger based on the field values and clear the field there. If you do in this way, from LWC you will just need to set this field value and this will be flexible if you have many LWC components. But caveat in this approach is you might need some extra logic to reset the bypasses.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.