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
erikdozsaerikdozsa 

Before/after delete trigger in Territory object

Hi Developers,

We would like to track Territory record deletion so I created a trigger which would archive some field values of the Territory record in another custom object. I tried "before delete" and "after delete" as well but even if the archivation is successful (in case of before delete) the following message shows up: "You cannot delete the territory because at least one territory rolls up to it." This is not the case and I am not able to actually delete the territory. If I remove my trigger I can delete the Territory record without issues. My approach works well on other objects but not on Territory.

Do you have any idea what could be wrong with my trigger or how could I avoid this error that I get?
 
trigger xxx_CORE_AFTER_TERR_DELETE on Territory (after delete) 
{
	
	list<xxx_CORE_Deleted_Record__c> deletedRecords = new list<xxx_CORE_Deleted_Record__c>();
	
	for(Territory t : trigger.old)
	{
		xxx_CORE_Deleted_Record__c deletedRecord = new xxx_CORE_Deleted_Record__c();
		
		deletedRecord.xxx_CORE_Country_Code__c = t.xxx_CORE_Country_Code__c;
		deletedRecord.xxx_CORE_Object_Name__c = 'Territory';
		deletedRecord.xxx_CORE_Record_ID__c = t.id;
		deletedRecord.xxx_CORE_Record_Name__c = t.Name;
		
		deletedRecords.add(deletedRecord);
		
	}	
	
	if(deletedRecords.size() <> 0)
		upsert deletedRecords;
	
}

Thanks in advance!




 
Frédéric TrébuchetFrédéric Trébuchet
Hi,

Comment lines 12 and 13.
What's happening?
Do you have a lookup field between Territory and xxx_CORE_Deleted_Record__c objects?
If yes, change it to a regular field.
Well, I think so...

Hope this helps,
Fred
 
erikdozsaerikdozsa

Hi Fred,

Thanks for the feedback. No, there is no lookup relationship here. xxx_CORE_Record_ID__c and xxx_CORE_Record_Name__c are just text fields. Basically I just grab the territory's ID and Name and load it into text fields on xxx_CORE_Deleted_Record__c object.

Anyway by commenting the 2 lines still leads to the same message:  "You cannot delete the territory because at least one territory rolls up to it."

Frédéric TrébuchetFrédéric Trébuchet
So, now we are sure there is no relation between these objects ;)
Any relation from Territory with itself?