Ask Search:
Debjani BanerjeeDebjani Banerjee 

Apex test class help needed

I am working on a trigger to stop users other than system admin to delete opportunity line items. Here is my trigger which is working fine in Sandbox. Now I need a test class to deploy it in Prod. Can anyone please help.

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!');

        }

    }
}
Best Answer chosen by Debjani Banerjee
Amit SinghAmit Singh
Like below
@isTest
public class StopDeleteUnitTest {
 public static testMethod void testMethod() {

  Date closeDt = Date.Today(); 
date myDate = date.today(); 
Account a2 = new Account(Name ='icrm testing acc'); 
insert a2; 
opportunity oppr = new opportunity(Name='testing DIE 4/6/2015' , AccountId= a2.Id,StageName = 'Prospecting', CloseDate = closeDt); 
insert opp;

  Id pricebookId = Test.getStandardPricebookId();

  //Create your product
  Product2 prod = new Product2(
   Name = 'Product X',
   ProductCode = 'Pro-X',
   isActive = true
  );
  insert prod;

  //Create your pricebook entry
  PricebookEntry pbEntry = new PricebookEntry(
   Pricebook2Id = pricebookId,
   Product2Id = prod.Id,
   UnitPrice = 100.00,
   IsActive = true
  );
  insert pbEntry;

  //create your opportunity line item.  This assumes you already have an opportunity created, called opp
  OpportunityLineItem oli = new OpportunityLineItem(
   OpportunityId = opp.Id,
   Quantity = 5,
   PricebookEntryId = pbEntry.Id,
   TotalPrice = quantity * pbEntry.UnitPrice
  );
  insert oli;

  Profile p = [SELECT Id FROM Profile WHERE Name = 'Standard User'];
  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) {
   // Delete the OpportunityLine Item
   delete oli;
  }

 }
}

 

All Answers

Amit SinghAmit Singh
Hi Debjani Banerjee,

In your test class, You need to Insert the test Data for the Following Object.

1 - Insert Opportunity
2 - Insert Product2 Record and Associate it to StandatrdPriceBook, You will get the Id of price book using below method. test.getstandardpricebookid()
3 - Insert PriceBookEntry
4 -  Then insert OpportunityLineItem
5 - After inserting the Same Delete the Same Record.

Below is the simple Code for inserting and deleting the OpportunityLine Item.
@isTest
public class StopDeleteUnitTest {
 public static testMethod void() {

  Date closeDt = Date.Today(); 
date myDate = date.today(); 
Account a2 = new Account(Name ='icrm testing acc'); 
insert a2; 
opportunity oppr = new opportunity(Name='testing DIE 4/6/2015' , AccountId= a2.Id,StageName = 'Prospecting', CloseDate = closeDt); 
insert opp;

  Id pricebookId = Test.getStandardPricebookId();

  //Create your product
  Product2 prod = new Product2(
   Name = 'Product X',
   ProductCode = 'Pro-X',
   isActive = true
  );
  insert prod;

  //Create your pricebook entry
  PricebookEntry pbEntry = new PricebookEntry(
   Pricebook2Id = pricebookId,
   Product2Id = prod.Id,
   UnitPrice = 100.00,
   IsActive = true
  );
  insert pbEntry;

  //create your opportunity line item.  This assumes you already have an opportunity created, called opp
  OpportunityLineItem oli = new OpportunityLineItem(
   OpportunityId = opp.Id,
   Quantity = 5,
   PricebookEntryId = pbEntry.Id,
   TotalPrice = quantity * pbEntry.UnitPrice
  );
  insert oli;

  Profile p = [SELECT Id FROM Profile WHERE Name = 'Standard User'];
  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) {
   // Delete the OpportunityLine Item
   delete oli;
  }

 }
}

 
Debjani BanerjeeDebjani Banerjee
Thank you for the help. I am getting an error while saving it. 
"Error: Compile Error: static is not allowed on constructors at line 3 column 27"
Any idea what I am doing wrong.

Thanks,
Debjani.
Amit SinghAmit Singh
Like below
@isTest
public class StopDeleteUnitTest {
 public static testMethod void testMethod() {

  Date closeDt = Date.Today(); 
date myDate = date.today(); 
Account a2 = new Account(Name ='icrm testing acc'); 
insert a2; 
opportunity oppr = new opportunity(Name='testing DIE 4/6/2015' , AccountId= a2.Id,StageName = 'Prospecting', CloseDate = closeDt); 
insert opp;

  Id pricebookId = Test.getStandardPricebookId();

  //Create your product
  Product2 prod = new Product2(
   Name = 'Product X',
   ProductCode = 'Pro-X',
   isActive = true
  );
  insert prod;

  //Create your pricebook entry
  PricebookEntry pbEntry = new PricebookEntry(
   Pricebook2Id = pricebookId,
   Product2Id = prod.Id,
   UnitPrice = 100.00,
   IsActive = true
  );
  insert pbEntry;

  //create your opportunity line item.  This assumes you already have an opportunity created, called opp
  OpportunityLineItem oli = new OpportunityLineItem(
   OpportunityId = opp.Id,
   Quantity = 5,
   PricebookEntryId = pbEntry.Id,
   TotalPrice = quantity * pbEntry.UnitPrice
  );
  insert oli;

  Profile p = [SELECT Id FROM Profile WHERE Name = 'Standard User'];
  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) {
   // Delete the OpportunityLine Item
   delete oli;
  }

 }
}

 
This was selected as the best answer
Debjani BanerjeeDebjani Banerjee
I am getting the error :

Code Coverage Failure
Your code coverage is 0%. You need at least 75% coverage to complete this deployment.

Any idea why.
Thanks again for your help.

Debjani.
Amit SinghAmit Singh
You need to be sure that Your Trigger have at least 75% Percent coverage while deploying
Debjani BanerjeeDebjani Banerjee
Well, I am a very new to APEX and any kind of help will bw highly appreciated. Below is my trigger to stop users from deleting opportunity Line item except Sys Admins.

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!');

        }

    }
}

Do you see a way to increase the code coverage in it.

Thanks,
Debjani.
Debjani BanerjeeDebjani Banerjee
Resolved. Thank you for your help.