+ Start a Discussion
Glenn at MvistaGlenn at Mvista 

Test Coverage on a Trigger - need some help

I have a working trigger on a custom object, related between Contacts and Entitlements__c, that changes the value of some contact fields, depending on a field in the Account record and a returned value in a query.  Here it is:

 

 

trigger Contact_Entitlement_Active on Contact_Entitlement__c (after insert, after update) {

	Set<Id> Ent_Ids = new Set<Id>();
        
    for (Contact_Entitlement__c ce:Trigger.new){
       Ent_Ids.add(ce.Contact__c);
       system.debug('ID: ' + ce.Id);
    }   	

	List<Contact_Entitlement__c> activeCon = [select Id from Contact_Entitlement__c where Contact__c in :Ent_Ids and Entitlement__r.Start_Date__c <= today and Entitlement__r.End_Date__c > today and Entitlement__r.Support_Program__r.Support_Type__c <> 'No Support' order by Entitlement__r.End_Date__c];
	List<Contact> userType = [select AccountId, Status__c, User_Type__c, Account.Type from Contact where Id in :Ent_Ids];

			if (!activeCon.isEmpty()) {			
				if (userType[0].Account.Type == 'Partner') {
				userType[0].User_Type__c = 'partner';
				userType[0].Status__c = 'active';
				} else {
				userType[0].User_Type__c = 'customer';
				userType[0].Status__c = 'active';
				}
			} else {
				userType[0].User_Type__c = 'inactive';
			}
			update userType;

}

 I have this test class for it to create the records, and meet the "Insert" trigger condition.

 

 

 

static testMethod void Contact_Entitlement_Active_Update() {
        
	Account testAccount = new Account(Name='Test Partner', Type='Partner');
		insert testAccount;
	Contact testContact = new Contact(LastName='TestPerson',AccountId=testAccount.Id);
		insert testContact;
	Support_Program__c testProgram = new Support_Program__c(Name='Test Program');
		insert testProgram;
	Entitlement__c testEntitlement = new Entitlement__c(Name='Test Entitlement',Support_Program__c=testProgram.Id,End_Date__c=Date.newinstance(2011,10,10),Case_Pack__c=true, Renewal_Amount__c=100, Cases_Remaining__c=100, Total_Cases__c=0);
		insert testEntitlement;
	Program_Contact__c testPContact = new Program_Contact__c(Name='Test Contact',Contact__c=testContact.Id,Support_Program__c=testProgram.Id);
		insert testPContact;
	Contact_Entitlement__c testCEnt = new Contact_Entitlement__c(Contact__c = testContact.Id,Entitlement__c = testEntitlement.Id,Program_Contact__c=testPContact.Id);
		insert testCEnt;

 

I have the Account Type set to Partner, but there is no coverage for this part of the Trigger:

 



if (!activeCon.isEmpty()) {
 if (userType[0].Account.Type == 'Partner') {
    userType[0].User_Type__c = 'partner';
    userType[0].Status__c = 'active';
    } else {
    userType[0].User_Type__c = 'customer';
    userType[0].Status__c = 'active';
  }

 I am stuck on what to do.  Any ideas?

 

Best Answer chosen by Admin (Salesforce Developers) 
sforce2009sforce2009
The problem may be, you have not mentioned start_date in Entitlement object. After this if it still not work, put a debug statement after your first query and check if the result is there.
Entitlement__c testEntitlement = new Entitlement__c(Name='Test Entitlement',Support_Program__c=testProgram.Id,End_Date__c=Date.newinstance(2011,10,10),Case_Pack__c=true, Renewal_Amount__c=100, Cases_Remaining__c=100, Total_Cases__c=0);

All Answers

sforce2009sforce2009
The problem may be, you have not mentioned start_date in Entitlement object. After this if it still not work, put a debug statement after your first query and check if the result is there.
Entitlement__c testEntitlement = new Entitlement__c(Name='Test Entitlement',Support_Program__c=testProgram.Id,End_Date__c=Date.newinstance(2011,10,10),Case_Pack__c=true, Renewal_Amount__c=100, Cases_Remaining__c=100, Total_Cases__c=0);
This was selected as the best answer
Glenn at MvistaGlenn at Mvista

Missing start date was the reason.  Thanks so much!