+ Start a Discussion
sfdc2705sfdc2705 

Trigger Help

Hi,

I need help with writing a trigger for deleting the associated records for example A,B,C records when a 'Case' record is deleted.
Appreciate any code help..

Thanks
John PipkinJohn Pipkin
What's the relationship between object?

Assuming it is lookups, the framework could look like this:
trigger deleteStuff on Case (before delete){

	//get all record IDS you want to delete and store in set
	Set<ID> ObjectAIds = new Set<ID>();
	for(Case c :Trigger.Old){
		ObjectAIds.add(c.ObjectALookup);
	}

	//query the record ids and assign to map
	Map<ID,ObjectA> objAMap = new Map<ID,ObjectA>();

	if(!ObjectAIds.isEmpty()){
		objAMap = new Map<ID,ObjectA>([Select Id from ObjectA where Id in :ObjectAIds]);
	}

	//use the map get method to add the records into a list
	List<ObjectA> deleteRecords = new List<ObjectA>();

	for(Case c :Trigger.old){
		deleteRecords.add(objAMap.get(c.ObjectALookup));
	}
	//delete the records
    if(!deleteRecords.isEmpty())
	    delete deleteRecords;
}

This won't be extremely helpful unless we know how all of the objects are related.
 
sfdc2705sfdc2705
Thanks a lot John..

There is 'Lookup' relationship between 'Case'  and the associated records of 'A,B,C'..

Appreciate your help..

Thanks
John PipkinJohn Pipkin
Cool. That framework should work then. You'll just need to add in the other objects in the same way. 

Happy coding!