• Ryan Dempsey 12
  • NEWBIE
  • 0 Points
  • Member since 2019

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 2
    Replies
I am an admin without delevoping skills and am getting the following error "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: []" when running the test or when trying to validate a change set with the following trigger and test class.  I poached most of this from the community. I'm wondering if the error has something to do with the profile permissions for the running user? Those with the enrollment profile do not have permissions to create pricebooks. When I replace "Enrollment" with "System Administrator" in the system user profile assignment of the test class, I no longer get those errors and I get my code coverage etc but that's not the test I want to run. I want System admins to be able to delete Opp lines and want to prevent everyone else from deleting them. I'm not even sure if that is what was generating the initial error, but IF it is, would anyone have any ideas on how to handle this in my test. Would I need to insert a system admin user as well and create the pricebook as that system admin and allow the rest of the test to be executed as user with the enrollment profile? 


This is the Trigger. 
trigger preventOppLineDelete 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('Only a system administrator can delete line items');
        }
    }
    
}

This is the Test Class
@isTest
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='Enrollment']; 
        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='enrollment@testorg.com');

        System.runAs(u) {
           Account a = new Account();
        a.Name = 'Test';
        a.RecordTypeId ='0125A000001IgHoQAK';
     
        
        insert a;
        
        
        Opportunity o = new Opportunity();
        o.name = 'Test';
        o.AccountId = a.Id;
        o.StageName = 'Initial App Received';
        o.CloseDate = date.today();
      
        
        insert o;
    
     Pricebook2 pb22 = new Pricebook2(Name='Test Price Book');
 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.02,PriceBookEntryId=pbe2.Id );
 insert OPplineitem2;
 
    
    delete OPplineitem2 ;
        }
    }
}
I am an admin without delevoping skills and am getting the following error "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: []" when running the test or when trying to validate a change set with the following trigger and test class.  I poached most of this from the community. I'm wondering if the error has something to do with the profile permissions for the running user? Those with the enrollment profile do not have permissions to create pricebooks. When I replace "Enrollment" with "System Administrator" in the system user profile assignment of the test class, I no longer get those errors and I get my code coverage etc but that's not the test I want to run. I want System admins to be able to delete Opp lines and want to prevent everyone else from deleting them. I'm not even sure if that is what was generating the initial error, but IF it is, would anyone have any ideas on how to handle this in my test. Would I need to insert a system admin user as well and create the pricebook as that system admin and allow the rest of the test to be executed as user with the enrollment profile? 


This is the Trigger. 
trigger preventOppLineDelete 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('Only a system administrator can delete line items');
        }
    }
    
}

This is the Test Class
@isTest
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='Enrollment']; 
        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='enrollment@testorg.com');

        System.runAs(u) {
           Account a = new Account();
        a.Name = 'Test';
        a.RecordTypeId ='0125A000001IgHoQAK';
     
        
        insert a;
        
        
        Opportunity o = new Opportunity();
        o.name = 'Test';
        o.AccountId = a.Id;
        o.StageName = 'Initial App Received';
        o.CloseDate = date.today();
      
        
        insert o;
    
     Pricebook2 pb22 = new Pricebook2(Name='Test Price Book');
 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.02,PriceBookEntryId=pbe2.Id );
 insert OPplineitem2;
 
    
    delete OPplineitem2 ;
        }
    }
}
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.');
        }
    }
}