I have a requirement to stop the record from being saved when a checkbox is set to true in that record. This record is saved from Lightning component and on click of Save button from the component, the record will be saved.

Below is my trigger

trigger MergeIntegratedLeadCheck on Lead_Custom__c (before insert, before update) {
    if (Trigger.isUpdate) {
        for (Lead_Custom__c lc: Trigger.new)
            if (lc.Integrated_Lead__c == true && lc.Merged_Lead_Integrated_Check__c == true) 
                lc.addError('Lead Cannot be updated'); // prevent update

But this trigger isn't working as expected. Instead of record being stopped from saving, the record is updated.

Can anyone please suggest the changes so that I can stop the record being saved.

  • 2
    The trigger looks alright. The first I would suggest is to check if the trigger is active and then use the debug logs to see if it is firing and then check if the condition on the Lead_Custom__c is correct or not i.e. both fields are true or not. You can add a debug before throwing an error and check logs if the debug is printing (simplest way) or use breakpoints to trace.
    – manjit5190
    Commented Jul 29, 2020 at 15:24
  • 2
    This looks correct to me, though I would point out that this is only applied "on update" rather than "on insert" so perhaps you allow the fields to be saved as true on first creation?
    – Phil W
    Commented Jul 29, 2020 at 15:33
  • True that @PhilW. But I want this trigger to be fired before the record is saved from that Lightning component where the values will be updated. I want to stop that using this trigger.
    – SK_112
    Commented Jul 29, 2020 at 15:48
  • The question is - are you creating the record from the Lightning Component or are you simply updating an existing one? We can't tell since we can't see that part of the system. Note that this logic will be applied regardless of the source of the creation/update.
    – Phil W
    Commented Jul 29, 2020 at 15:55
  • @PhilW,I am updating the existing record from that Component. However, when i checked the log and record, if i use the trigger to stop the record from saved, the field merged_integrated_lead__c is not getting updated to true and when I disable the trigger and use the component, the field is getting updated. That means, instead of stopping the record from saved, this trigger is stopping the field from getting updated.
    – SK_112
    Commented Jul 29, 2020 at 16:01


You must log in to answer this question.

Browse other questions tagged .