0

We have an old Salesforce org from which we are migrating entitlements and data to a new org. There is also a trigger for adding the entitlement by default to the case. It had 90% code coverage. But when I try to replicate it in our current sandbox to move it to production, it gives the above error.

Here are the trigger and the test class. Any help would be greatly appreciated.

 List <Entitlement> entlsAnglo = [Select e.Id
                                     From Entitlement e
                                     Where e.Name = 'Anglo - Platinum'];

  if(entlsAnglo.isEmpty()== false){
    for (Entitlement curAngloEnt : entlsAnglo){

        if (c.EntitlementId == curAngloEnt.id){

             BusinessHours AngloBusinessHours = [select Id from BusinessHours where Name = 'Brisbane Business Hours' limit 1];

             c.BusinessHoursId = AngloBusinessHours.id;

        }        
     }

  }

Test Class:

static testMethod void testtrigger2(){

  Account acc=New Account(Name='Test');
  insert acc;

    Entitlement entl = [select id from Entitlement  Where Name = 'Anglo - Platinum'limit 1];


  Contact oContact = new Contact(FirstName='Test',LastName='test2',Email='[email protected]',Accountid=acc.id,Phone='1111111111',MobilePhone='11111111111');
  insert oContact;   

  DateTime DT=DateTime.Now();

  RecordType RT=[Select id from RecordType where Name='Software Support Case' and SObjectType='Case' limit 1];

  Case c=new Case(RecordTypeID=RT.ID, Subject='test', Description='Test',Reported_At__c=DT,Responded_At__c=DT,Priority='Medium',
  ContactID=oContact .id, Status='Assigned',Origin='Web',Product_Family__c='Operations',Case_Type__c='Workshop repair/RMA',
  SlaStartDate=DT,Accountid=acc.id
  );

  insert c;


  }
5
  • 1
    Possible duplicate of Set Business Hours in Apex Test Code
    – Mark Pond
    Commented Aug 9, 2018 at 17:43
  • BusinessHours in a unit test relies on the Business Hours records that are configured in your org. They are not isolated like other data for unit tests. Create Business Hours records in your new orgs prior to migrating this test code.
    – Mark Pond
    Commented Aug 9, 2018 at 17:44
  • All the business hours, enititlements etc were already created and they exist. I checked. The log after test run said the error was on Where e.Name = 'Anglo - Platinum']; Commented Aug 9, 2018 at 17:47
  • I also have to mention that there are multiple entitlements on that name - 3 to be exact. Each associated to a different account. Commented Aug 9, 2018 at 17:48
  • Hmm, I disagree with the duplicate vote given OP's comments. Voting to keep open.
    – Derek F
    Commented Aug 10, 2018 at 12:49

1 Answer 1

2

Here are the steps to solve the issues.

  1. In the test class, make @seeAllData = false, so that it will not look for existing org's data.

  2. Since, this query select Id from BusinessHours where Name = 'Brisbane Business Hours' limit 1 looks for BusinessHour's name, so it must exist before running this testclass.

Otherwise, create logic as follows in the main class.

if(Test.isRunningTest()){
   String BusinessHourId = [SELECT Id FROM BusinessHours WHERE IsDefault = true];
}
else
{
   String BusinessHourId = [select Id from BusinessHours where Name = 'Brisbane Business Hours' limit 1];

}
  1. You need to create Entitlement record, rather than retrieving from existing org. Because of this the error is occurring.

  2. Create set of test records using @testSetup methods so that it can be used in other test methods in the same class.

  3. Your test class should have necessary System.assert() statements to unit scenarios and outcome.

Refer Testing Best Practices

2
  • Hi Shantanu, Thanks for your answer. As I mentioned above, All the entitlements, Business Hours and other data have been recreated in the new org and exist Commented Aug 9, 2018 at 19:33
  • Read the best practices and think about implementing those correctly. I have already mentioned point wise. Hope it makes sense. Commented Aug 9, 2018 at 19:36

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .