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
sreekanth reddysreekanth reddy 

Test Class

Hi,

This is my trigger and how to write test class for this trigger.

trigger OppProductDelete on OpportunityLineItem (before delete){
Id sysAdminId = [Select Id, Name from Profile where id = '00e90000001qXNbAAM' LIMIT 1].Id;
for (OpportunityLineItem oppProd : trigger.old){
if(UserInfo.getProfileId() ==sysAdminId)
oppProd.addError('This Opp Product may not be deleted');}
}

thanks.
 
Surendra nuneSurendra nune
1. create a user in test method with sys admin profile.
2. Prepare test data(opp line items ).
3. delete them.
@isTest
                    
private class TestRunAs {
   public static testMethod void testRunAs() {
      // Setup test data
      // This code runs as the system user
      Profile p = [SELECT Id FROM Profile WHERE id = '00e90000001qXNbAAM']; 
      User u = new User(Alias = 'standt', Email='standarduser@testorg.com', 
      EmailEncodingKey='UTF-8', LastName='Testing', LanguageLocaleKey='en_US', 
      LocaleSidKey='en_US', ProfileId = p.Id, 
      TimeZoneSidKey='America/Los_Angeles', UserName='standarduser@testorg.com');

      System.runAs(u) {
         testdataOpplineitems(); // method that prepares oppline items
         // Assert the count of lineitems by querying.
        // delete the line items
        //Assert the count of line items.  
      }
   }
}

 
sreekanth reddysreekanth reddy
Hi surender thanks for responce.
i am new to development, can you please suggest how to get the count of line items and delete line items.

Thanks.
Surendra nuneSurendra nune
Please find the below link on how to get count using aggregate queries
https://developer.salesforce.com/forums/ForumsMain?id=906F0000000BCbW.

Please mark it as best answer if it solves your problem.
sreekanth reddysreekanth reddy
hi thanks,

i am using above test class but i am getting 0% code coverage.

pls send me the total test class for this trigger.

trigger OppProductDelete on OpportunityLineItem (before delete){
Id sysAdminId = [Select Id, Name from Profile where id = '00e90000001qXNbAAM' LIMIT 1].Id;
for (OpportunityLineItem oppProd : trigger.old){
if(UserInfo.getProfileId() ==sysAdminId)
oppProd.addError('This Opp Product may not be deleted');}
}

thanks.