Here was the solution I implemented:
In the SysRecordTemplate class I changed this code:
if(common.TableId == tablenum(EventRuleData))
{
excludeValidateField.add(fieldId2Ext(fieldnum(EventRuleData,RuleId),1));
}
to this:
switch(common.TableId)
{
case tablenum(EventRuleData):
excludeValidateField.add(fieldid2ext(fieldnum(eventRuleData,RuleId),1));
break;
case tablenum(InventTableModule):
excludeValidateField.add(fieldid2ext(fieldnum(InventTableModule,ItemId),1));
break;
case tablenum(InventItemLocation):
excludeValidateField.add(fieldid2ext(fieldnum(InventItemLocation,ItemId),1));
break;
}
The above code will prevent the issue from happening in the future (now when the item that the template was built on was deleted, it will not corrupt the template). There was still the issue of cleaning up the corrupted templates. Here is the code I used to do that:
// This job will delete the empty templates from the SysRecordTemplateTable
// Just set the string variable ‘toDelete’ to the display name of the template you need deleted
// and run this job
static void DeleteEmptyItemTemplates(Args _args)
{
SysRecordTemplateTable templateTable;
int j,i,d,x;
container template;
str toDelete = “TEST”
;
ttsbegin;
select forupdate data from templateTable where templateTable.table == TableNum(InventTable);
for(j = 1; j <= conlen(templateTable.Data); j++)
{
template = conPeek(templateTable.Data,j);
i = confind(template,toDelete);
if(i)
{
templateTable.Data = conDel(templateTable.Data,j,1);
templateTable.update();
info(strfmt(“Deleted container %1 - %2”,j,toDelete));
break;
}
}
ttscommit;
}