+ Start a Discussion
AnonymouseAnonymouse 

Validation Rule to Prevent Deletion if Lookup Field is Not Null

Hello,

I have a custom object with custom fields. One of the fields is a lookup field to another object. I would like to prevent the deletion of a record of my custom object if the lookup field is not null. How may I go about doing this? A validation rule?

Any help would be greatly appreciated.

Sincerely,
Jackie
Best Answer chosen by Anonymouse
Steven NsubugaSteven Nsubuga
Hi Jackie, you can use a before trigger to prevent record deletion.
trigger preventRecordDeletion on Object__c  (before delete) {
	
    for(Object__c obj : trigger.old){
		if (obj.lookup__c != null) {
			obj.addError('This Record Must Not be deleted');
		}
    }
}

 

All Answers

Steven NsubugaSteven Nsubuga
Hi Jackie, you can use a before trigger to prevent record deletion.
trigger preventRecordDeletion on Object__c  (before delete) {
	
    for(Object__c obj : trigger.old){
		if (obj.lookup__c != null) {
			obj.addError('This Record Must Not be deleted');
		}
    }
}

 
This was selected as the best answer
AnonymouseAnonymouse
Hi Steven,

That worked beautifully. Thank you.

Sincerely,
Jackie