function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Sandy singhSandy singh 

on click of delete button from junction object how to delete record from objects which linked

Hi all,

 

I have one junction object EmpDepartment which link with Employee and Department object and on click of stranded delete button it will delete record from all three object (EmpDepartment, Employee ,Department ).

 

 

Can any one help me on this requirement.

 

Thanks in advance,

Sandy

 

 



Best Answer chosen by Admin (Salesforce Developers) 
jhurstjhurst

Sandy,

 

Rather than relying on a button to hold the logic to delete the related objects, I would suggest using a trigger on the junction object itself:

 

trigger cascadeJunctionDelete on EmpDepartment__c (after delete) {
    List<Employee__c> empList = new List<Employee__c>();
    List<Department__c> depList = new List<Department__c>();
    for(EmpDepartment__c ed : Trigger.old) {
        empList.add(new Employee__c(id=ed.Employee__c));
        depList.add(new Department__c(id=ed.Department__c));    
    }
    if(empList.size() > 0) {
        delete empList;
    }
    if(depList.size() > 0) {
        delete depList;
    }
}

 Hope this helps.

Jay

All Answers

jhurstjhurst

Sandy,

 

Rather than relying on a button to hold the logic to delete the related objects, I would suggest using a trigger on the junction object itself:

 

trigger cascadeJunctionDelete on EmpDepartment__c (after delete) {
    List<Employee__c> empList = new List<Employee__c>();
    List<Department__c> depList = new List<Department__c>();
    for(EmpDepartment__c ed : Trigger.old) {
        empList.add(new Employee__c(id=ed.Employee__c));
        depList.add(new Department__c(id=ed.Department__c));    
    }
    if(empList.size() > 0) {
        delete empList;
    }
    if(depList.size() > 0) {
        delete depList;
    }
}

 Hope this helps.

Jay

This was selected as the best answer
Sandy singhSandy singh

Thanks Jay,

 

  It's solve my requirement.