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
Haris Sheikh 1Haris Sheikh 1 

Prevent deletion of opportunity product depending on custom value in a field in OpportunityLineItem

Hi,

I have a scenario to prevent deletion of opportunity products from opportunity if a custom field in OpportunityLineItem table has a specific value.

How can I implement it using using validation rules on Opportunity?

Thanks!
Arthur ImirzianArthur Imirzian
Hello,
You can't use Validation Rule to prevent deletion.
You should use a Before Delete Trigger instead.
By adding "addError" on a field, you will abort deletion.

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_triggers_context_variables.htm#highlighter_797812
Amit Chaudhary 8Amit Chaudhary 8
Please try below Trigger:-
trigger OLITrigger on OpportunityLineItem( before delete)
{
	For(OpportunityLineItem oli : Trigger.old)
	{
		if(oli.Status__C =='Submitted') // Please compare your values here
		{
			oli.addError('you can not delete this OLI');
		}
	}

}

Please mark this as solution if this will help you

Thanks
Amit Chaudhary
Haris Sheikh 1Haris Sheikh 1
Thanks a lot for your reply! 

I think the trigger will be needed when deleting a product from its detail page but I want to stop deletion from related list in opportunity page. I am currently doing it through validation rule with this formula: 
RUS_Count_of_Products__c < PRIORVALUE(RUS_Count_of_Products__c)
but now I am trying to change this formula to somehow check for a specific value in a custom field in product.
Amit Chaudhary 8Amit Chaudhary 8
Hi Haris,

Trigger will work from both place from detail page as well as from releted list. Please try above trigger and let me know this will help you or not