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
Julie CurryJulie Curry 

trigger to prevent deletion for opportunity products

Hi,

I'm an admin and I'd like to create something to prevent non-SysAdmin users from deleting opportunity products.  The only option I'm seeing for this is creating a trigger...which I've never done.  I copied and pasted the below from something and am trying to swap things out, but I'm not sure what to plug in.  What should "Account acc" be for opportunity product?  How do I prevent this from being applied to admins?  Do I only need the trigger to get this to work?



trigger <PreventOpportunityProductDeletion> on OpportunityLineItem (<events>) {
     for(Account acc : trigger.old){
         acc.adderror('Account Cannot be deleted');
}
}



Thanks in advance for your help!

Arvind_SinghArvind_Singh
Hello Julie,

Please try this one. This trigger will not allow anyone to delete OpportunityLineItem except Admin profile. 
 
trigger StopDelete on OpportunityLineItem (before delete) 
{
    String profileName=[SELECT id,Name FROM Profile WHERE Id =:UserInfo.getProfileId()].Name ;  
    for (OpportunityLineItem a : Trigger.old)
    {      
        If(profileName!='System Administrator')
        {
            a.addError('You cannot delete this record!');
        }
    }
    
}

Hope this helps.

Thanks
Arvind Singh
Arvind_SinghArvind_Singh
I think another way to restrict user based on profile. Go to Profile and uncheck delete option like below screenshot

StopDelete from Profile