+ Start a Discussion
SkeeterSkeeter 

trigger to prevent deleting child records

I'm trying to create a trigger to prevent deletion of a child record if the parent record has a status of approved.  I'm not sure what I'm missing.  on the testentry.add line it is saying invalid field test__c for SObject sheet__c
trigger PreventEntryDeletion on Test__c (before delete) {

Set<ID> testentry = New Set<ID>();
    
    List<sheet__c> l = [Select Id From sheet__c Where status = 'Approved' and Id IN :trigger.oldMap.KeySet()];
    for(sheet__c objp : l)
    {
        testentry.add(objp.test__c);
    }
   
    For(test__c obja : trigger.old){
        
        If(testentry.contains(obja.ID))
            obja.addError('You cannot delete a test entry on an approved sheet.');
    }
        
    }

Ramu_SFDCRamu_SFDC
What is the datatype of Test__c ? is it a relationship(lookup/master detail) field? 
SkeeterSkeeter
Test__c has a lookup to the sheet__c object which is the parent
Ramu_SFDCRamu_SFDC
As per line # 08 it seems that there is no lookup/master-detail field with the field name 'test__c' in the object Sheet__c hence it is throwing the error.

Below is the sample code as per my understanding and hopefully it should work. I am assuming that Sheet__c is a parent object, Test__c is a child object and sheetid__c is a lookup field on Test__c which is related to Sheet__c parent object. 

trigger PreventEntryDeletion on Test__c (before delete) {

	Set<ID> testentry = New Set<ID>();
	for(test__c tst:trigger.old){
		testentry.add(tst.sheetid__c);
	}
    
    	set<id> l = new set<id>[Select Id From sheet__c Where status = 'Approved' and Id IN :testentry];
   	For(test__c obja : trigger.old){        
        		If(l.contains(obja.sheetid__c))
	                obja.addError('You cannot delete a test entry on an approved sheet.');
    	}
        }


SkeeterSkeeter
I'm receiving an unexpected token: '[' on line 8