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
sdfasdsdfasd 

how to delete the parent object records using trigger

1. i created one custom object i.e  Broker__c.

2. in Contact object i created one lookup field to Broker__c object(Field Name : Broker__c(lookup))

3.i created trigger for  once inser the records in Broker object. that record convert to Contact object. and once i update the Broker object record then Contact object record also updated based on Broker id.

 

4. Now my requirement is i delete the Broker object record then Contact object record also deleted based on Broker id using trigger.i write code for delete the records.but records is not deleted this is my problem. pls help me......

 

Delete Trigger

================

 

 

if(Trigger.isDelete){
        List<ID> DBList = new List<ID>();
        System.debug('DBList:'+DBList);
        for(Broker__c b:Trigger.old) {
            DBList.add(b.id);
        }
        
       for( List<Contact> DelConList :[select id from contact where Broker__c in :DBList]){
        if(DelConList.size() >0) {
           System.debug('DelConList:'+DelConList);
           delete DelConList;
        }
    }

Best Answer chosen by Admin (Salesforce Developers) 
Navatar_DbSupNavatar_DbSup

Hi,

 

Make sure you are using the before delete event in the trigger on the Broker custom object.

 

Use the below trigger as reference .

 

 

trigger deleterecord on Broker__c (before delete) 
{
    if(Trigger.isDelete){
        List<ID> DBList = new List<ID>();
        System.debug('DBList:'+DBList);
        for(Broker__c b:Trigger.old) {
            DBList.add(b.id);
        }
        
       List<Contact> DelConList = [select id from contact where Broker__c in :DBList];
        if(DelConList.size() >0) 
        {
           System.debug('DelConList:'+DelConList);
           delete DelConList;
        }
    }

}



All Answers

Navatar_DbSupNavatar_DbSup

Hi,

     Tryout this sample code it may help you.

 

if(Trigger.isDelete){
        List<ID> DBList = new List<ID>();
        System.debug('DBList:'+DBList);
        for(Broker__c b:Trigger.old) {
            DBList.add(b.id);
        }
        
       List<Contact> DelConList = [select id from contact where Broker__c in :DBList];
        if(DelConList.size() >0) 
		{
           System.debug('DelConList:'+DelConList);
           delete DelConList;
        }
    }

 Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved. 

 

sdfasdsdfasd

Thank for your post Navatar. But this code not working .in DEBUG showing this error . i.e


   Aggregations: Select id from Contact where Broker__c in : DBList

   Rows : 0

 

 

 insert and udpate properly working but delete only not working .how can solve this problem

Navatar_DbSupNavatar_DbSup

Hi,

 

Make sure you are using the before delete event in the trigger on the Broker custom object.

 

Use the below trigger as reference .

 

 

trigger deleterecord on Broker__c (before delete) 
{
    if(Trigger.isDelete){
        List<ID> DBList = new List<ID>();
        System.debug('DBList:'+DBList);
        for(Broker__c b:Trigger.old) {
            DBList.add(b.id);
        }
        
       List<Contact> DelConList = [select id from contact where Broker__c in :DBList];
        if(DelConList.size() >0) 
        {
           System.debug('DelConList:'+DelConList);
           delete DelConList;
        }
    }

}



This was selected as the best answer
sdfasdsdfasd

Thanks Navatar

Now working code. i take after delete that is the problem.Now properly working. Thanks for your Help Navatar