Activity Forums Salesforce® Discussions Is the method trigger:old only used for the event "before delete"?

  • shradha jain

    Member
    July 30, 2018 at 1:31 pm

    No, the method trigger:old is not only used for the event “before delete”.It can be used for both update and delete triggers.

  • Parul

    Member
    September 18, 2018 at 7:36 am

    No you can't used the trigger:old for the event “before delete”

     

    Thanks

  • shariq

    Member
    September 21, 2018 at 12:23 am

    Hi,

    Parul, you are wrong, we can use Trigger.old in before delete

    It can also be used for update trigger.

    Hope this helps.

  • Parul

    Member
    September 21, 2018 at 4:34 am

    Hi, Thanks a lot to clear my doubt.

    In Before Insert Trigger.old collection is not able, it's null.
    Trigger.old trigger is available in after update trigger and will hold old copy of current modifying record.
    Trigger.old is available in Delete trigger .

    Trigger.new is not available on before delete, and after delete triggers.

    For future refrence:

    trigger ParentTrigger on Parent__c (before delete) {
    List<Id> parentIds = new List<Id>();
    for(Parent__c p : Trigger.Old)
    parentIds.add(p.Id);

    Map<Id, List<Child__c>> childMap = new Map<Id, List<Child__c>>();
    for (Child__c child : [SELECT id, Parent__c FROM Child__c WHERE Parent__c IN :parentIds]) {
    if(!childMap.containsKey(child.Parent__c))
    childMap.put(child.Parent__c, new List<Child__c>());
    childMap.get(child.Parent__c).add(child);
    }

    for (Parent__c par : Trigger.Old) {
    if (childMap.containsKey(child.Parent__c)) {
    par.adderror('Cannot be deleted');
    }
    }
    }

     

    Thanks Shariq.

     

Log In to reply.

Popular Salesforce Blogs