Activity Forums Salesforce® Discussions How to delete child records in lookup relation (with Salesforce triggers)

  • Sudhir

    Member
    June 21, 2017 at 2:46 pm

    Check out this one.. Click here to get the solution DeleteChildrenFromOppty

    You can optimize it further based on design patterns you use.

  • Parul

    Member
    September 17, 2018 at 11:40 am

    Hi,

    Please find the below code:

    trigger DeleteChildrenFromOppty on Opportunity (before insert, before update, after insert, after update) {

    if(Trigger.isAfter && Trigger.isUpdate){
    set<Id> setOpportunityIds = new set<Id>() ;
    for(Opportunity opp : Trigger.new){
    if((!opp.isWon && opp.isClosed) && !(!Trigger.oldMap.get(opp.Id).isWon && Trigger.oldMap.get(opp.Id).isClosed)){
    setOpportunityIds.add(opp.Id) ;
    }
    }
    if(!setOpportunityIds.isEmpty())
    delete([SELECT Id from Object_A__c WHERE Opportunity__c IN: setOpportunityIds]) ;
    }
    }

    Hope this helps you.

    Thanks.

     

  • Suresh Avula

    Member
    March 26, 2019 at 1:11 am

    trigger deleteuniversity on University__c (before insert,before delete,after delete) {
    set<id> ids =new set<id>();
    if(trigger.isbefore && trigger.isdelete)
    {
    for(University__c u:trigger.old)
    {
    ids.add(u.id);
    }
    system.debug('ma size'+ids);
    Deleteuniversityhelper.emp =[SELECT Id, University__c FROM Employeee__c where University__c in:ids];
    }
    if(trigger.isafter && trigger.isdelete)
    { system.debug('ma size'+Deleteuniversityhelper.emp);
    delete Deleteuniversityhelper.emp;
    }

    }

     

    public class Deleteuniversityhelper {
    public static list<Employeee__c> emp;
    }

Log In to reply.

Popular Salesforce Blogs

Popular Salesforce Videos