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
tmbarrytmbarry 

Problem Creating a test class.

I am trying to create a test class for two trigger I have created.  This first trigger is:

 

trigger Update_ProviderTracking_01 on Task (after update)

{for (Task t:System.Trigger.new)
{if (t.Subject == 'Mail Provider Intro Letter' && t.IsClosed==True) {        

Provider_Facility__c PF = new Provider_Facility__c(ID=t.WhatId, Provider_Intro_Letter_Mailed__c = True);       

update PF;  }  }

 

The test class is:

 

@isTestclass MyTestClass001 {    static testMethod void myUnitTest() {        // TO DO: implement unit test       DateTime dT = System.now();Date myDate = date.newinstance(dT.year(), dT.month(), dT.day());
Task o = new Task();o.Subject = 'Mail Provider Intro Letter'; //set the required fieldso.WhatId=[select id from Provider_Facility__c limit 1].id;o.Status='Not Interested - Other';//o.priority='Normal';//o.ActivityDate=myDate;//o.assign='Todd Barry';insert o;
o.Status='Complete';update o;

{if (o.Subject == 'Mail Provider Intro Letter'&& o.IsClosed==True) {         Provider_Facility__c PF = new Provider_Facility__c(ID=o.WhatId, Provider_Intro_Letter_Mailed__c = True);        update PF;  }}
}}

 

and I get 100% coverage

 

My Second trigger is exactly the same as the first, execpt I am testing for different Task subject:

 

trigger Update_ProviderTracking_02 on Task (after update)

{for (Task t:System.Trigger.new)
{if (t.Subject == 'Follow Up Call to Intro Letter' && t.IsClosed==True) {

       Provider_Facility__c PF = new Provider_Facility__c(ID=t.WhatId, Intro_Letter_Follow_Up_Call_Made__c=True);       

update PF;  }  }

 

and the test class is 

 

@isTestclass MyTestClass002 {    static testMethod void myUnitTest() {        // TO DO: implement unit test       

 

Task o = new

Task();o.Subject = 'Follow Up Call to Intro Letter';

o.WhatId=[select id from Provider_Facility__c limit 1].id;

o.Status='Not Interested - Rates';//o.priority='Normal';

insert o;

 

o.Status='Complete';

update o;

{if (o.Subject == 'Follow Up Call to Intro Letter' && o.IsClosed==True) {         Provider_Facility__c PF = new Provider_Facility__c(ID=o.WhatId, Intro_Letter_Follow_Up_Call_Made__c=True);        update PF;  }}
}}

 

but this time I only get 50% coverage and it highlights the last line Provider_Facility__c PF = new Provider_Facility__c(ID=o.WhatId, Intro_Letter_Follow_Up_Call_Made__c=True);        update PF;   in red.

 

Any ideas why one get 100% and the other only 50%?

Best Answer chosen by Admin (Salesforce Developers) 
jhurstjhurst

It worked for me.  The only difference I had to do was around the status field.  You have the criteria:

 

if (t.Subject == 'Follow Up Call to Intro Letter' && t.IsClosed==True) {
    Provider_Facility__c PF = new Provider_Facility__c(ID=t.WhatId, Intro_Letter_Follow_Up_Call_Made__c=True);        
    update PF;  
}  

 In order for the code to not be entered, the subject either does not equal "Follow Up Call to Intro Letter" or IsClosed equals False.  IsClosed is set based on the Status field.  By default the "Closed" status is "Completed" not "Complete".  Unless you have changed the Statuses in setup to have Complete as a valid closed staus, that is likely the problem.

 

In that case, I cannot tell you why the first trigger is completely covered.  We would have to look to be sure what is happening.


If you are still having issues, please open up a support ticket and we can take a look.

 

Hope this helps.

Jay

All Answers

jhurstjhurst

It worked for me.  The only difference I had to do was around the status field.  You have the criteria:

 

if (t.Subject == 'Follow Up Call to Intro Letter' && t.IsClosed==True) {
    Provider_Facility__c PF = new Provider_Facility__c(ID=t.WhatId, Intro_Letter_Follow_Up_Call_Made__c=True);        
    update PF;  
}  

 In order for the code to not be entered, the subject either does not equal "Follow Up Call to Intro Letter" or IsClosed equals False.  IsClosed is set based on the Status field.  By default the "Closed" status is "Completed" not "Complete".  Unless you have changed the Statuses in setup to have Complete as a valid closed staus, that is likely the problem.

 

In that case, I cannot tell you why the first trigger is completely covered.  We would have to look to be sure what is happening.


If you are still having issues, please open up a support ticket and we can take a look.

 

Hope this helps.

Jay

This was selected as the best answer
tmbarrytmbarry

Jay,

 

Thanks for your help.  It's amazing how another set of eyes can make all the difference in the world.  I kept changing everything, except for adding the "d" to "complete".  

 

One more question if you'll permit.  Is there any way to comibined the two test classes into one?  Or does each trigger have to have it's own test class?  I have ten triggers to test for all the same with the only difference being the Task subject.

 

Thanks again,

 

Todd B.

jhurstjhurst

Glad it worked.

 

You can defintiely combine the tests into a single test class.  All yout would do is create a new test method for each trigger you wanted to test:

 

@isTest
class MyTestClass {    
    static testMethod void myUnitTest001() {        
        // TO DO: implement unit test       
    }
    static testMethod void myUnitTest002() {        
        // TO DO: implement unit test       
    }
    static testMethod void myUnitTest003() {        
        // TO DO: implement unit test       
    }
}

Hope this helps.

Jay 

tmbarrytmbarry

Thanks Jay.

tmbarrytmbarry

Hey Jay,

 

I have another for you if you are up for it.  I am trying to create a new trigger:





trigger CreateContract on Provider_Facility__c (after update) {
for (Provider_Facility__c p : Trigger.new)
DateTime dT = System.now();
Date myDate = date.newinstance(dT.year(), dT.month(), dT.day());

{if (p.Provider_status__c =='Contract Negotiation'){
    Provider_Contracting__c PC= new Provider_Contracting__c(Provider_Name__c=p.Id, Effective_date__c =myDate);
    insert p; } 	
}
}

 and I am getting an error messge that says:  Save Error:  Viable does not exist: p.  But when i type my code and type p. all the fields foe the object show up.  So how can p not exist?

 

Thanks again.

jhurstjhurst

Well...from a quick look, it seems there are a couple of issues:

 

1. You do not have and actions contained in your for loop.  You have to have { and } in conjunction with your for loop to actually do anything.  right now, the code loops through the Trigger.new, assigns the record to "p" and then instntly moves to the next in the list.  Since "p" is only defined in the for loop context, it does nopt exist when you try and insert p.

 

2. It does not make sense for you to do an insert p at the end of the code.  P is a record that aready exists (it was acted on by the trigger), so even if you do #1 above, you will get an error saying that you cannot set the ID of p.  Likely, you want to insert the Provider_Contracting__c record you created.

 

The final code (from my assumptions above) should be:

 

trigger CreateContract on Provider_Facility__c (after update) {
   for (Provider_Facility__c p : Trigger.new) {
      DateTime dT = System.now();
      Date myDate = date.newinstance(dT.year(), dT.month(), dT.day());
      if (p.Provider_status__c =='Contract Negotiation'){
         Provider_Contracting__c PC= new(Provider_Contracting__c(Provider_Name__c=p.Id, Effective_date__c =myDate);
         insert PC;
      } 	
   }
}

 

Hope this helps.

Jay

tmbarrytmbarry

Jay,

 

YOU ARE AWESOME!!!!

 

As you can probably tell I am a little over my head here with all this code writing!

 

One last question, if I wanted to hard code a date 

Effective_date__c =myDate

 What is the proper syntax for entering a date is it  Effective_date__c = #01/01/2012# or something else? 

 

Your new Biggest fan,

 

Todd B. 

jhurstjhurst

Todd,

 

The Date functions are listed at http://www.salesforce.com/us/developer/docs/apexcode/index_Left.htm#StartTopic=Content/apex_methods_system_date.htm?SearchType=Stem.  Datetimes are listed at http://www.salesforce.com/us/developer/docs/apexcode/index_Left.htm#StartTopic=Content/apex_methods_system_datetime.htm?SearchType=Stem.

 

If you just want a static Date, use the format YYYY-MM-DD, DateTime uses YYYY-MM-DD HH:MM:SS (note the space between the day and the hour).  So you could set Jan 1, 2011 using:

 

c.Date_Field__c = date.valueOf('2011-01-01');
c.DateTime_Field__c = datetime.valueOf('2011-01-01 00:00:00');

 Hope this helps.

Jay