+ Start a Discussion
Dev2IndiaDev2India 

Prevent opportunity deletion through validation ?

HI Experts,

 

Please let me know a solution if we can prevent opportunity deletion through validation also. I beleive we can do through the code. Suppose I have to prevent opportunity deletion based on 2 condtions as in validation below

 

OR(Isblank(Username__c), Isblank(Date__c) ))

 

So can we prevent deletion for one profile users , based on above conditions through Validation ???????

 

I appreciate your help....

 

Thanks

Best Answer chosen by Admin (Salesforce Developers) 
Peter_sfdcPeter_sfdc

Validation rules only fire on record save. Not delete. I'm afraid the only way to prevent a delete of a record under most circumstances is through Apex. 

 

There is one exception to this that I'm aware of: preventing deletion of a child in a Master-Detail relationship (or a standard relationship that supports Roll-up Summary fields). 

 

Create a Roll-Up Summary Field (RSF) in Account that performs a count function for Opportunities. You now have a field that performs an update on the Account record when you either insert or delete an opportunity. Because the delete triggers an update on Account, you can validate the deletion of the Opportunity with a validation rule on the Account object.

 

This is tantalizingly close to what you want to do, unfortunately, I don't think you can create RSF filters using the ISBLANK feature. I think you would also really struggle to detect the specific condition on the child record in the validation rule itself. 

 

So I'm afraid it is Apex for you. This will be done with a trigger on the Opportunity object, on the after delete event. 

All Answers

Peter_sfdcPeter_sfdc

Validation rules only fire on record save. Not delete. I'm afraid the only way to prevent a delete of a record under most circumstances is through Apex. 

 

There is one exception to this that I'm aware of: preventing deletion of a child in a Master-Detail relationship (or a standard relationship that supports Roll-up Summary fields). 

 

Create a Roll-Up Summary Field (RSF) in Account that performs a count function for Opportunities. You now have a field that performs an update on the Account record when you either insert or delete an opportunity. Because the delete triggers an update on Account, you can validate the deletion of the Opportunity with a validation rule on the Account object.

 

This is tantalizingly close to what you want to do, unfortunately, I don't think you can create RSF filters using the ISBLANK feature. I think you would also really struggle to detect the specific condition on the child record in the validation rule itself. 

 

So I'm afraid it is Apex for you. This will be done with a trigger on the Opportunity object, on the after delete event. 

This was selected as the best answer
Dev2IndiaDev2India

@peter

 

Thanks for the nice thoughts. I belive this will be better way to implement. I will try once free. Howeve  I did the same through the help of trigger and that works for me.

 

Thanks alot for your commnets.

 

Regards,