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
DekorDekor 

Apex test class for verifying entitlement creation

I currently have a couple of triggers in our system that automatically set up an entitlement against accounts.  These triggers fire based on the field "type" against the account.  I'm trying to create a test class for these triggers as they currently have zero code coverage.

Here is the test class I have created but its failing on line 15 with List has no rows for assignment to SObject message:
@isTest
private class CreateEntitlement{
static testMethod void CreateEntitlement(){
	
     // CORRECT - Create the required test data needed for the test scenario.
     // In this case, I need to create an account with type set to customer
     // So I create that Account in my test method itself. 
     Account testAccount = new Account(name='Test Company Name');
     insert testAccount;
	   
     testAccount.type='Customer';
     update testAccount;

     // Verify that the entitlment was created in the database.
     Entitlement NewEntitlement = [SELECT AccountId FROM Entitlement WHERE Name = 'testAccount'];
     System.assertEquals(testAccount.ID, NewEntitlement.AccountId);

  
}
}



Here is the Apex Trigger that I'm trying to cover with this test. 
 
trigger CreateEntitlement on Account (after insert,before update) {

Map<Id, Entitlement> mapOfAccountIdToEntitlement = new Map<Id, Entitlement>();

for( Entitlement et : [ Select AccountId From Entitlement Where AccountId in: trigger.new] ) {
mapOfAccountIdToEntitlement.put( et.AccountId , et );
}


List<Entitlement> createentitlement = new List <Entitlement> {};

for (Account acc : trigger.new) {

// then in you code where you have &&(entitlement.accountid != account.id))  check frommap instead like
Boolean alreadyhasEntitlement = mapOfAccountIdToEntitlement.containsKey(acc.Id) ;
if( (acc.type == 'customer') && ( !alreadyhasEntitlement)  ) 
{
createentitlement.add(new Entitlement (
Name = acc.name, /* Give a standard name*/
AccountId = acc.Id, /* Link the Entitlement to the account */
SlaProcessId = '55220000000L9ZOAA0', /* Link it to a defined entitlement process */
StartDate = system.Today(),
Type = 'Phone Support',
BusinessHoursId = '01m200000009dGQAAY'
    
));
}
}
try {
insert createentitlement ;
}
catch (Exception Ex)
{
system.debug(Ex);
}
}

 
RAM AnisettiRAM Anisetti
try this one
 
@isTest
private class CreateEntitlement_test{
static testMethod void CreateEntitlement_TestOne(){
	
     // CORRECT - Create the required test data needed for the test scenario.
     // In this case, I need to create an account with type set to customer
     // So I create that Account in my test method itself. 
     
	 Account testAccount = new Account(name='Test Company Name');
     insert testAccount;
	   
     testAccount.type='Customer';
     update testAccount;

     // Verify that the entitlment was created in the database.
     Entitlement NewEntitlement = [SELECT AccountId FROM Entitlement WHERE Name =:testAccount.Name ];
     System.assertEquals(testAccount.ID, NewEntitlement.AccountId);

  
}
}