+ Start a Discussion
Julie CurryJulie Curry 

test class for prevent opportunity product delete trigger

I am an admin.  I don't code but worked with the community and a salesforce support rep to create the below trigger for preventing opportunity products from being deleted.  Now I need a test class to deploy it and I'm not sure how to create it.  Can anyone help?

trigger PreventOpportunityProductDeletion on OpportunityLineItem (before delete) {
    if(userinfo.getProfileId() != '00e60000001504CAAQ')
        for(OpportunityLineItem oppli: trigger.old){
            oppli.adderror('Opportunity Product can only be deleted by a System Administrator.');
Raj VakatiRaj Vakati
Change your triggeer as below .. 

remove hardcoded profile Id
trigger PreventOpportunityProductDeletion on OpportunityLineItem (before delete) {
Profile  p = [select Id, Name from Profile where Name = 'System Administrator'  Limit 1 ] 
    if(userinfo.getProfileId() != p.Id)
        for(OpportunityLineItem oppli: trigger.old){
            oppli.adderror('Opportunity Product can only be deleted by a System Administrator.');

Test CLass
private class PreventOpportunityProdTest {
    public static testMethod void testRunAs() {
        // Setup test data
        // This code runs as the system user
        Profile p = [SELECT Id FROM Profile WHERE Name='Standard User']; 
        User u = new User(Alias = 'standt', Email='stanstandardusesdasdasdrdarduser@testorg.com', 
            EmailEncodingKey='UTF-8', LastName='Testing', LanguageLocaleKey='en_US', 
            LocaleSidKey='en_US', ProfileId = p.Id, 
            TimeZoneSidKey='America/Los_Angeles', UserName='standardustandardusesdasdasdrsesdasdasdr@testorg.com');

        System.runAs(u) {
           Account a = new Account();
        a.Name = 'Test';
        a.Industry = 'Retail';
        insert a;
        Opportunity o = new Opportunity();
        o.name = 'Test';
        o.AccountId = a.Id;
        o.StageName = 'Closed Won';
        o.CloseDate = date.today();
        o.Type = 'New Customers';
        insert o;
		 Pricebook2 pb22 = new Pricebook2(Name='testDIE');
 insert pb22;

Product2 pro2 = new Product2(Name='BXCD',  isActive=true);
insert pro2;

PricebookEntry pbe2 =new PricebookEntry(unitprice=0.01,Product2Id=pro2.Id,Pricebook2Id=Test.getStandardPricebookId(),
                                         isActive=true,UseStandardPrice = false);
 insert pbe2;

 OpportunityLineItem OPplineitem2 = new OpportunityLineItem (Quantity=2, OpportunityId=o.Id,UnitPrice=0.01,PriceBookEntryId=pbe2.Id );
 insert OPplineitem2;
		delete OPplineitem2 ;

Ryan Dempsey 12Ryan Dempsey 12
Thank you both for already posting about this. I have one question.

When I validate prior to deploying into production, is the error that I'm getting "System.DmlException: Delete failed. First exception on row 0 with id 00k1g0000081vIZAAY; first error: FIELD_CUSTOM_VALIDATION_EXCEPTION, Only a system administrator can delete line items: []" expected??

I'm new to deploying triggers and classes and have not run into this error before. It's almost as if the validation error is a good thing as it seems that it's a result of the error that the trigger is generating when the test class tries to delete the opp line item that it has inserted. I don't want to deploy this as is becuase of this error but maybe it's ok??