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 

Assistance required to write test for Apex Trigger 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)
}

 

Best Answer chosen by Scott Russo
Scott RussoScott Russo
Here is the working code, with modifications we've made to improve it.
/* 
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);

        }
    }
    
}

 

All Answers

Rajesh SriramuluRajesh Sriramulu

Hi

 

try this code

 

 

@isTest

private class SupportPlanEntitlemen_test{

static testmethod void SupportPlanEntitlemen_test(){

Accoumt acc = new Account();

acc.name='test';

//insert other which are mandotory

.

.

 

insert acc;

 

like this insert contact and case and EntitlementContact

in case insert the values according to if conditions

 

}}

 

 

Scott RussoScott Russo

Hi SRS8,

 

I appreciate the code sample.  Thank you very much for the reply.

 

Is this complete?  It looks like there are sections where I need to add test cases. 

That is the problem.  I do not know what to test?  We are not Sales Force developers, and have never seen Apex code before.  We saw this code sample, and it accomplished exactly what we needed, but now we are dead in the water with trying to figure out what we need to do for testing.

 

Where do we add this code, in a new Apex trigger?

A new class?

Rajesh SriramuluRajesh Sriramulu

HI

 

u have to go for new class i.e setup->develop->Apex classes->new     and paste this code but this code is not enough to get the sufficient code coverage but as i said  u follow and write more code in it.

 

Let me know if any queries.

 

Regards,

Rajesh.

Scott RussoScott Russo

Thank you.  I will give it a try.

 

Scott RussoScott Russo

Once I have the test class created, I'm not sure how it tests the Apex Trigger code.

is the code I provided above supposed to be in an Apex trigger, or a class?

 

What should my test class be testing for, Creating an account, contact, entitlement?

Does it need to test all the classes that exist in our environment?

 

Thanks,

Scott

Rajesh SriramuluRajesh Sriramulu

Hi

 

 

 

U just create the test class and after saving it run it, it will show which class/trigger are covered in it.And search ur trigger in that page and see the percentage beside it and click on it, it show which lines are covered  that r in blue in color and the color red in color has not covered.

 

Let me know if any queries.

 

Regards,

Rajesh.

Scott RussoScott Russo

Hi,

 

That's just it, 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 trigger?

I'm drowning here.  Can you give me a sample of what my test code should include based on the apex trigger code I provided above.

I've searched for examples, but since I am not a developer, I do not understand the syntax or logic needed.  I appreciate all your help so far.  Thanks.

Rajesh SriramuluRajesh Sriramulu

Hi

 

Try this code in seperate new class

 

@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;

Id contactIds;

Id acctIds;

 

Asset as = new Asset();

as.//insert required fields

 

insert as;

 

Case cs = new Case();

cs.contactIds=con.ContactId;

cs.acctIds=acc.AccontId;

cs.EntitlementId=here i dont what is EntitlementId u insert it.
insert cs;

 

try to insert fields in the above according to if conditions so that it will cover.

 

}}

 

Let me if any query.

 

Regards,

Rajesh.

 

 

 

 

Scott RussoScott Russo

Thanks,  I thought your example would get me going in the right direction.  I appreciate the help, but since I am not a developer, I do not understand what is missing, or what to add to make 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.  It didn't run on my Apex Trigger.  Frankly I dont understand how Sales Force would know this test was for my Trigger.  This is so frustrating.  I thank you for your time.

 

This is test test code I have so far.  I don't even know if it's doing anything.  I read the examples, and I can't seem to make the connection between what is explained, and what I need to do accomplish.

@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; 

    } 
}

 

Rajesh SriramuluRajesh Sriramulu

Hi

 

 

Can plz send ur gmail id so  i can add to my google+ and can know what r custom fields u having and which r required

 

 

Regards,

Rajesh.

Scott RussoScott Russo
Here is the working code, with modifications we've made to improve it.
/* 
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 was selected as the best answer