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
Scott RussoScott Russo 

Help needed for Apex Trigger unit test code

All,

 

We do not have an Sales Force developer on staff.  We found sample code that does exactly what we need it to do, but we do not know how to get it tested, and implimented into our production environment.  Is there sample test class for the following code?

 

trigger SupportPlanEntitlement on Case (Before Insert, Before Update) {
   /*
   When a case is created, auto-populate with the active support plan
   If the Entitlement Name is not set then, check to see if the Contact on the Case has an active Entitlement
    and select the first one.  If not then check to see if the Account on the Case has an active Entitlement.
   */
   List<Id> contactIds = new List<Id>();
   List<Id> acctIds = new List<Id>();
   for (Case c: Trigger.new){
      if (c.EntitlementId == null && c.ContactId!= null && c.AccountId!= null){
         contactIds.add(c.ContactId);
         acctIds.add(c.AccountId);
      }
   }
   if(contactIds.isEmpty()==false || acctIds.isEmpty()==false){
      /* Added check for active entitlement */
      List <EntitlementContact> entlContacts = [Select e.EntitlementId,e.ContactId,e.Entitlement.AssetId From EntitlementContact e
                                                Where e.ContactId in:contactIds
                                                And e.Entitlement.Type = 'Support Plan' And e.Entitlement.EndDate >= Today And e.Entitlement.StartDate <= Today];
      if(entlContacts.isEmpty()==false){
         for(Case c: Trigger.new){
            if(c.EntitlementId == null && c.ContactId!= null){
               for(EntitlementContact ec:entlContacts){
                  if(ec.ContactId==c.ContactId){
                     c.EntitlementId = ec.EntitlementId;
                     if(c.AssetId==null && ec.Entitlement.AssetId!=null)
                        c.AssetId=ec.Entitlement.AssetId;
                     break;
                  }
               } // end for
            }
         } // end for
      } else{
         List <Entitlement> entls = [Select e.StartDate, e.Id, e.EndDate, e.AccountId, e.AssetId
                                     From Entitlement e
                                     Where e.AccountId in:acctIds And e.Type = 'Support Plan' And e.EndDate >= Today And e.StartDate <= Today];
         if(entls.isEmpty()==false){
            for(Case c: Trigger.new){
               if(c.EntitlementId == null && c.AccountId!= null){
                  for(Entitlement e:entls){
                     if(e.AccountId==c.AccountId){
                        c.EntitlementId = e.Id;
                        if(c.AssetId==null && e.AssetId!=null)
                           c.AssetId=e.AssetId;
                        break;
                     }
                  } // end for
               }
            } // end for
         }
      }
   } // end if(contactIds.isEmpty()==false)
}

 

This is test test code I have so far.  I don't even know if this code is correct.  I read the examples out on the boards, and I can't seem to make the connection between what is explained, and what I need to do accomplish my goal.  Since all I want to do is relate an Entitlement to a case, I'm not sure what needs to be tested.  .

 

I do not seem my trigger listed when I run the test class.  Does that mean my test code doesn't have any referense to the Apex trigger?

I am not a developer, I do not understand what is missing, or what to add to create a valid test.

I had compile errors on a few lines, so I commented them out to see what would happen.  I was able to save and run a test.  This test didn't run on my Apex Trigger.  Frankly I dont understand how Sales Force would know this test was for my Trigger.  Since the code I am using was listed on these board, I was hoping someone created the test code.  This is so frustrating.

 

Can someone provide me with the information I need to get this code to test my Apex trigger.  Does my trigger need to have a class created?

What does my test code need to have 100% code coverage?

 

@isTest

private class SupportPlanEntitlement_test{

    static testmethod void SupportPlanEntitlement_test(){

    Account acc = new Account(); 
    acc.name='test'; 
// insert required fields like this 
    insert acc; 
    Contact con= new Contact(); 
    con.Lastname='test5'; 
//like this insert required fields 
    insert con; 
  
    Asset a = new Asset();
    a.Name = 'AssetNameTest';

    insert a; 
  
    Case cs = new Case(); 
 //   cs.contactIds = con.ContactId;  Compiler error on this line
 //   cs.acctIds=acc.AccontId;        Compiler error on this line
    Entitlement en = new Entitlement();
    en.Name = 'TestEntitlement';
    en.AccountId = acc.Id;
 //   en.AssetId = a;                 Compiler error on this line
    insert en;

    cs.EntitlementId = en.Id;
    insert cs; 

    } 
}

 

Best Answer chosen by Scott Russo
Scott RussoScott Russo
This is my updated code
/* 
SupportPlanEntitlement Apex Trigger
When a case is created, auto-populate with the active support plan. 
If the Entitlement Name is not set then, check to see if the Contact on the Case has an active Entitlement and select the first one. 
If not then check to see if the Account on the Case has an active Entitlement. 

+ 04/02/2012 SR@BT Originally created.
+ 09/12/2012 JU@IC Pasted in Sbox.
+ 09/13/2012 JU@IC Improved code.
+ 09/21/2012 JU@IC Changed the API Version from 24.0 to 25.0
+ 03/01/2013 JSR@BT CUST001 Added code for Maintenance Active
+
*/

trigger SupportPlanEntitlement on Case (before insert, before update) {
    Set<Id> acctIds = new Set<Id>();
    for(Case c: Trigger.new){ 
        if (c.AccountId!=null)
            acctIds.add(c.AccountId); 
    }
    
    Map<Id,Id> accSPlanMap = new Map<Id,Id>();  //ACCOUNT TO SUPPORT PLAN ENTITLEMENT MAPPING
    Map<Id,Boolean> accTechAccMgrMap = new Map<Id,Boolean>();   //ACCOUNT TO TAM ENTITLEMENT MAPPING
    Map<Id,Boolean> accTMaintenanceMap = new Map<Id,Boolean>();   //CUST001 ACCOUNT TO Maintenance ENTITLEMENT MAPPING    
    for(Entitlement ent : [SELECT Id, AccountId, Type, Status FROM Entitlement WHERE AccountId IN :acctIds
                            AND (Type = 'Support Plan' OR Type LIKE '%Technical Account Manager'OR Type = 'Maintenance')
                            AND (Status = 'Active'OR Status = 'Inactive') ORDER BY CreatedDate]){
        if(ent.Type == 'Support Plan')
            accSPlanMap.put(ent.AccountId, ent.Id);
        else if (ent.Type == 'Maintenance' && ent.Status == 'Active')//CUST001
            accTMaintenanceMap.put(ent.AccountId, TRUE);//CUST001
            else if ((ent.Type == '24x7 Technical Account Manager' || ent.Type == 'Technical Account Manager') && ent.Status == 'Active')//CUST001 
            accTechAccMgrMap.put(ent.AccountId, TRUE);
    }
    
    for(Case c: Trigger.new){ 
        if (c.AccountId!=null){
            if(accSPlanMap.containskey(c.AccountId))//CUST001
                c.EntitlementId = accSPlanMap.get(c.AccountId);
            if(accTechAccMgrMap.containskey(c.AccountId))
                c.TAM_Active__c = accTechAccMgrMap.get(c.AccountId);
            if(accTMaintenanceMap.containskey(c.AccountId))
                c.Maintenance_Active__c = accTMaintenanceMap.get(c.AccountId);

        }
    }
    
}

This is the Apex Test Execution code
/* 
SupportPlanEntitlementTest Apex Class

+ 09/20/2012 JU@IC Created 100% Test Coverage for the SupportPlanEntitlement Apex Trigger.
+
*/

@isTest
private class SupportPlanEntitlementTest {
    static testMethod void myUnitTest() {
        Account newAcc = new Account();
        newAcc.Name = 'Test Account';
        newAcc.BillingStreet = 'Test Address';
        newAcc.BillingCity = 'Test City';
        newAcc.BillingState = 'Test State';
        newAcc.BillingPostalCode = 'Test PostalCode';
        insert newAcc;
        
        Entitlement testEnt1 = new Entitlement();
        testEnt1.Name = 'Test Entitlement 1';
        testEnt1.AccountId = newAcc.Id;
        testEnt1.Type = 'Support Plan';
        testEnt1.StartDate = SYSTEM.today();
        testEnt1.EndDate = SYSTEM.today().addMonths(1);
        insert testEnt1;
        
        Entitlement testEnt2 = new Entitlement();
        testEnt2.Name = 'Test Entitlement 2';
        testEnt2.AccountId = newAcc.Id;
        testEnt2.Type = 'Technical Account Manager';
        testEnt2.StartDate = SYSTEM.today();
        testEnt2.EndDate = SYSTEM.today().addMonths(1);
        insert testEnt2;
        
        List<Case> testCases = new List<Case>();
        for(Integer i=0; i<200; i++){
            Case testCase = new Case();
            testCase.AccountId = newAcc.Id;
            testCases.add(testCase);
        }
        insert testCases;
        
        for(Case varCase : [SELECT EntitlementId, TAM_Active__c FROM Case WHERE Id IN :testCases]){
            SYSTEM.AssertEquals(testEnt1.Id,varCase.EntitlementId);
            SYSTEM.AssertEquals(TRUE,varCase.TAM_Active__c);            
        }
    }
}

 

All Answers

Henry AkpalaHenry Akpala

Do you know that your trigger only requires some test coverage?  It doesn't require 100% coverage.  That said, you need to create a Account, Contact, Case, and at least 2 entitlement records(1 with both contact and account id, 1 with only account id) This second entitlement with only account id is used to test the else condition of the trigger.  Also, ensure that the date criteria meets are satisfised on the entitlement "e.Entitlement.EndDate >= Today And e.Entitlement.StartDate <= Today]"

If 

Use cs.contactIds = con.Id

Use cs.acctIds = acc.Id

Use en.AssetId = a.Id

Remember to add the dates for the new entitlement EndDate & StartDate since your query is checking for dates

 

Hope this helps 

Regards

-H

Scott RussoScott Russo

Hi Henry,

 

Thank you for the reply.  Unfortunately I'm not sure how to translate your comments into a proper test script, since I'm not a developer.

I thought this would be easy.

Also, how does the test code find the test trigger?

Henry AkpalaHenry Akpala

You are on the right track. Just update your code to use the Id's instead of the code you wrote. see the sample below...  The reason you are getting compile error is because you are using the the wrong field .

 

 

cs.contactIds = con.Id

   Case cs = new Case(); 
 //   cs.contactIds = con.ContactId;  Compiler error on this line
 //   cs.acctIds=acc.AccontId;        Compiler error on this line

 

 

Henry AkpalaHenry Akpala
Entitlement en = new Entitlement();
    en.Name = 'TestEntitlement';
    en.AccountId = acc.Id;
 //   en.AssetId = a;                 Compiler error on this line
    insert en;

    cs.EntitlementId = en.Id;
    insert cs; 

this is very close to what you want but add values for the date field,

en.StartDate=SOMEVALUE,

en.EndDate=SOMEVALUE

but make sure you create 2 entitlement records...

 

Scott RussoScott Russo

Thank you.  I will attempt to make this work.

2 questions though.

1. How do you create multiple documents in the test code?

2. when I run this code, how does it find the Apex Trigger it's suppose to be testing?

 

Cheers

Scott RussoScott Russo
This is my updated code
/* 
SupportPlanEntitlement Apex Trigger
When a case is created, auto-populate with the active support plan. 
If the Entitlement Name is not set then, check to see if the Contact on the Case has an active Entitlement and select the first one. 
If not then check to see if the Account on the Case has an active Entitlement. 

+ 04/02/2012 SR@BT Originally created.
+ 09/12/2012 JU@IC Pasted in Sbox.
+ 09/13/2012 JU@IC Improved code.
+ 09/21/2012 JU@IC Changed the API Version from 24.0 to 25.0
+ 03/01/2013 JSR@BT CUST001 Added code for Maintenance Active
+
*/

trigger SupportPlanEntitlement on Case (before insert, before update) {
    Set<Id> acctIds = new Set<Id>();
    for(Case c: Trigger.new){ 
        if (c.AccountId!=null)
            acctIds.add(c.AccountId); 
    }
    
    Map<Id,Id> accSPlanMap = new Map<Id,Id>();  //ACCOUNT TO SUPPORT PLAN ENTITLEMENT MAPPING
    Map<Id,Boolean> accTechAccMgrMap = new Map<Id,Boolean>();   //ACCOUNT TO TAM ENTITLEMENT MAPPING
    Map<Id,Boolean> accTMaintenanceMap = new Map<Id,Boolean>();   //CUST001 ACCOUNT TO Maintenance ENTITLEMENT MAPPING    
    for(Entitlement ent : [SELECT Id, AccountId, Type, Status FROM Entitlement WHERE AccountId IN :acctIds
                            AND (Type = 'Support Plan' OR Type LIKE '%Technical Account Manager'OR Type = 'Maintenance')
                            AND (Status = 'Active'OR Status = 'Inactive') ORDER BY CreatedDate]){
        if(ent.Type == 'Support Plan')
            accSPlanMap.put(ent.AccountId, ent.Id);
        else if (ent.Type == 'Maintenance' && ent.Status == 'Active')//CUST001
            accTMaintenanceMap.put(ent.AccountId, TRUE);//CUST001
            else if ((ent.Type == '24x7 Technical Account Manager' || ent.Type == 'Technical Account Manager') && ent.Status == 'Active')//CUST001 
            accTechAccMgrMap.put(ent.AccountId, TRUE);
    }
    
    for(Case c: Trigger.new){ 
        if (c.AccountId!=null){
            if(accSPlanMap.containskey(c.AccountId))//CUST001
                c.EntitlementId = accSPlanMap.get(c.AccountId);
            if(accTechAccMgrMap.containskey(c.AccountId))
                c.TAM_Active__c = accTechAccMgrMap.get(c.AccountId);
            if(accTMaintenanceMap.containskey(c.AccountId))
                c.Maintenance_Active__c = accTMaintenanceMap.get(c.AccountId);

        }
    }
    
}

This is the Apex Test Execution code
/* 
SupportPlanEntitlementTest Apex Class

+ 09/20/2012 JU@IC Created 100% Test Coverage for the SupportPlanEntitlement Apex Trigger.
+
*/

@isTest
private class SupportPlanEntitlementTest {
    static testMethod void myUnitTest() {
        Account newAcc = new Account();
        newAcc.Name = 'Test Account';
        newAcc.BillingStreet = 'Test Address';
        newAcc.BillingCity = 'Test City';
        newAcc.BillingState = 'Test State';
        newAcc.BillingPostalCode = 'Test PostalCode';
        insert newAcc;
        
        Entitlement testEnt1 = new Entitlement();
        testEnt1.Name = 'Test Entitlement 1';
        testEnt1.AccountId = newAcc.Id;
        testEnt1.Type = 'Support Plan';
        testEnt1.StartDate = SYSTEM.today();
        testEnt1.EndDate = SYSTEM.today().addMonths(1);
        insert testEnt1;
        
        Entitlement testEnt2 = new Entitlement();
        testEnt2.Name = 'Test Entitlement 2';
        testEnt2.AccountId = newAcc.Id;
        testEnt2.Type = 'Technical Account Manager';
        testEnt2.StartDate = SYSTEM.today();
        testEnt2.EndDate = SYSTEM.today().addMonths(1);
        insert testEnt2;
        
        List<Case> testCases = new List<Case>();
        for(Integer i=0; i<200; i++){
            Case testCase = new Case();
            testCase.AccountId = newAcc.Id;
            testCases.add(testCase);
        }
        insert testCases;
        
        for(Case varCase : [SELECT EntitlementId, TAM_Active__c FROM Case WHERE Id IN :testCases]){
            SYSTEM.AssertEquals(testEnt1.Id,varCase.EntitlementId);
            SYSTEM.AssertEquals(TRUE,varCase.TAM_Active__c);            
        }
    }
}

 
This was selected as the best answer