I need to restrict Non admin Profiles or Users from deleting the opportunityline items (lookupfield -- product) on added to the opportunity. I need to allow them to edit the opportunity for any changes unless closed Won. Since opportunitylineitems is child object it doesn't have any standard function or edit, read, create or delete function on profile level.
how to restrict Non Admin profiles from Editing or deleting the opportunity line items at any stage?
2 Answers
You might need to write a validation in the trigger on BeforeDelete event , which throws an error when non Admin users try to delete. Customise the if block based on your requirements
trigger OpportunityLineItemTrigger on OpportunityLineItem(before delete) { for (OpportunityLineItem record : Trigger.old) { if (UserInfo.getProfileName() != 'System Administrator') { Trigger.new[0].addError('You are not allowed to delete this record.'); } } } `` - Add a rollup summary field to Opportunity that is
LineItemCount__c(define asCOUNTofOpportunity Product) - Then add a Validation Rule on
Opportunitythat (ifIsWon = true) sees if current value ofLineItemCount__cis < priorvalue ofLineItemCount__cas this would indicate a deletion.