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
bikla78bikla78 

Test Method Help

 The trigger below gets the days between the created date and actvity date and puts the results in the day_count__C field. However, my test method below is still at 0%. Any help would be grealy appreciated.

 

trigger Event_Before_Insert on Event (before update)
{

for( Event currentEvent : Trigger.new )
{
datetime CreatedDate =CurrentEvent.CreatedDate;
date ActivityDate =CurrentEvent.ActivityDate;

date createdDateTemp = date.newInstance(CreatedDate.year(),CreatedDate.month(),CreatedDate.day());
Integer numberofDays = createdDateTemp.daysBetween(ActivityDate);

CurrentEvent.Day_Count__c = numberofDays;

}

}

 

 

 

The test method below keeps saying 0 percent.

 

 

 

 

            Event ev48 = new Event(Subject = 'Test', Type= 'Other', Meeting_Status__c = 'Pending',ActivityDate =Date.today(),WhoId = cont.Id );
try {
insert ev48;
}
catch(DmlException e) {
System.debug(e.getMessage());
}
ev48.day_count__c= 0;
update ev48;





}



 

Best Answer chosen by Admin (Salesforce Developers) 
dmsx2oddmsx2od

Short answer this time:

 

Please set your test class and methods to private.  This is the way it is set up in the Apex documentation.

 

Without knowing the regular code, one cannot help with the test code, but it looks like you're either setting up a ton of data on which you'll perform some sort of manipulation (which would be good) or you're testing a ton of before insert triggers, in which case you may wish to (and I may get a ton of flame for this) set them all up, add each Event to a single List<Event>, and then insert that List<Event> (then update it as well):

 

List<Event> events = new List<Event>();

Event a = new Event(...);

events.add(a);

Event b = new Event(...);

events.add(b);

...

test.starttest();

insert events;

update events;

test.stoptest();

 

I'm not going to explain why I use starttest and stoptest - they're both in the same area of the documentation. :)

 

Happy coding,

David

 

All Answers

Anup JadhavAnup Jadhav

Hi bikla,

 

could you surround the update event with try catch block in the test, and see if it prints out any error message?



try {

update ev48;

} catch (Exception ex) {

   System.debug(ex.getMessage());

}

 

 -AJ

 

bikla78bikla78

I updated it but it only says 0 percent not covered. When I open up the details it says the lines below are not covered. This is a tricky one for me since the day_count__c is a calculated field so I can't really hard code it like i did below. I also have issues using the createddate field since it's a read only field. Not sure how to test this update trigger...

 

what do u think?

 

 

 lines not tested: 4, 6 ,7, 9, 10, 12


     Event ev48 = new Event(Subject = 'Test', Type= 'Other', Meeting_Status__c = 'Pending',ActivityDate =Date.today(),WhoId = cont.Id );			
try {
insert ev48;
}
catch(DmlException e)

{
System.debug(e.getMessage());
}
ev48.day_count__c= 0;
try {

update ev48;

} catch (Exception ex) {

System.debug(ex.getMessage());

}


 

Message Edited by bikla78 on 08-03-2009 12:38 PM
DevAngelDevAngel

Hi bikla78,

 

Shouldn't your test modify a modifiable field?  For instance:

 

 

Event ev48 = new Event(Subject = 'Test', Type= 'Other', Meeting_Status__c = 'Pending',ActivityDate =Date.today(),WhoId = cont.Id );

try {
insert ev48;
} catch(DmlException e) {
System.debug(e.getMessage());
}

ev48.ActivityDate = Date.today().addDays(-3);

update ev48;

 

 And, by the way, why are you creating a temporary date for the created date?  Couldn't you also do:

 

 

trigger Event_Before_Insert on Event (before update) {

for( Event currentEvent : Trigger.new ) {
Integer numberofDays = currentEvent.ActivityDate.daysBetween(currentEvent.CreatedDate);

CurrentEvent.Day_Count__c = numberofDays;
}
}

 

 

 

bikla78bikla78

Dave,

 

I updated my test method but I am still getting 0 percent coverage. Also, I have to store the created date in a temporary variable since the createddate is a datetime datatype and the activity date is a date datatytpe so if I try to use a isbetween method, it will generate an apex exception so I have to make sure both are the same. The trigger works fine but I just can't seem to get it to pass test coverage. Any insight would be much appreciated.

 

     Event ev48 = new Event(ownerid = '00560000000lgAbAAI', Subject = 'Test', Type= 'Other', Meeting_Status__c = 'Pending',ActivityDate =Date.today(),WhoId = cont.Id );			
try {
insert ev48;
}
catch(DmlException e)
{
System.debug(e.getMessage());
}
ev48.ActivityDate = Date.today().addDays(-3);

update ev48;

 

trigger Event_Before_Insert on Event (before update) {

for( Event currentEvent : Trigger.new )
{
datetime CreatedDate =CurrentEvent.CreatedDate;
date ActivityDate =CurrentEvent.ActivityDate;

date createdDateTemp = date.newInstance(CreatedDate.year(),CreatedDate.month(),CreatedDate.day());
//Integer numberofDays = createdDateTemp.daysBetween(ActivityDate);
//CurrentEvent.Location = string.valueof(numberofDays);

CurrentEvent.Location = string.valueof(createdDateTemp.daysBetween(ActivityDate));

}
}
Message Edited by bikla78 on 08-03-2009 10:11 PM
dmsx2oddmsx2od

Bikla-

 

You're hitting some things that everyone seems to run into at some point, so you're not alone.  A few points:

 

1. ActivityDate is, in fact, a Date, but ActivityDateTime (like CreatedDate) is a DateTime.  However, since you are counting the number of days between CreatedDate and ActivityDate, you are doing the right thing by converting createdDateTemp as a Date to get your integer. 

 

2. Dave is correct to suggest that you update a writeable field, not the Day_Count__c field.  The adddays(-3) is interesting, but I chose to use addDays(3).  And then I removed it altogether... see below.

 

3. ActivityDateTime is the dominant field; ActivityDate is a calculated date from the DateTime.  So instead of writing to the ActivityDate field (which may be writeable, but which still requires a value in ActivityDateTime) just write to the ActivityDateTime field, and the platform will take care of the rest, populating ActivityDate for you. 

 

4. You can only update a record that has been inserted, and then queried or referred to via its ID, from the database.  Therefore, though you inserted ev48, you updated the version of ev48 that had not been committed to the database, so no update trigger was ever fired.

 

5. If you check the system debug log when running your test code, you'll see that there is an error when inserting ev48: 

Insert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [DurationInMinutes]: [DurationInMinutes]

So although you planned well to use a try/catch, it was caught, but you didn't catch that. (Poor joke; sorry.)

In fact, if you create an event and populate ActivityDate and Duration, the error says that the record is missing a required ActivityDateTime value.  So the THREE required fields for every Event (out of the box) are Subject, ActivityDateTime, and Duration.  (And owner, but that is filled automatically.)

 

Here's my trigger:

 

trigger Event_Before_Update on Event (before update) { for( Event currentEvent : Trigger.new ) { datetime CreatedDate =CurrentEvent.CreatedDate; date ActivityDate =CurrentEvent.ActivityDate; date createdDateTemp = date.newInstance(CreatedDate.year(),CreatedDate.month(),CreatedDate.day()); //Integer numberofDays = createdDateTemp.daysBetween(ActivityDate); //CurrentEvent.Location = string.valueof(numberofDays); CurrentEvent.Location = string.valueof(createdDateTemp.daysBetween(ActivityDate)); } }

 

And the test code (with 100% coverage):


@isTest private class TestEventBeforeUpdate { static testMethod void myUnitTest() { Event ev48 = new Event(Subject = 'Test', ActivityDateTime = datetime.now(), DurationInMinutes = 60 ); //Event ev48 = new Event(Subject = 'Test', ActivityDate = date.today() ); try { insert ev48; } catch(DmlException e) { System.debug(e.getMessage()); } test.starttest(); ID newid = ev48.id; Event ds48 = [SELECT id FROM Event WHERE id = :ev48.id LIMIT 1]; //ds48.ActivityDate = Date.today().addDays(3); update ds48; test.stoptest(); } }


(And notice that I commented out the field value change, as all the platform needs is an update call; the values can stay the same.  Interesting, yes?)

 

 

Hope that helps!

 

David Schach

X-Squared On Demand

http://www.x2od.com

bikla78bikla78

David,

 

This is probably the best and most detailed thread I have ever experienced ont he forum- thank you so much

 

Your points are dead on it and probably helps that i "catch" the errors in my debug log =)

 

The main thing I learned out of this is that I need to find out what the required fields are to insert a test record.  Also, when updating a record, I need to update the ID since test record does not commit to the db but instead  peforms a rollback.

 

After all this though, I am still receiving 0% test coverage. I actually changed your test method and made it part of the class instead of it's own private method . However, I am still getting 0%. Here is my entire test class - sorry it;s long but one my classes that uses a complex worflow needed all these tests to reach 75%.  Notice your code block at the end.

 

public class TestAll {

public static testMethod void testEventFlow() {
system.debug('in test method');
Account acc = new Account(Name = 'TestAccount', Type = 'Prospect', Industry = 'Banking', Physical_Street__c = 'Test Street',Physical_City__c = 'Los Angeles', Physical_State__c = 'CA', Physical_Zip_Postal_Code__c = '90017', Physical_Country__c = 'USA', Phone = '(888) 999-1234' );
try {
insert acc;
}
catch(DmlException e) {
System.debug(e.getMessage());
}
Contact cont = new Contact(LastName = 'Kim', AccountId = acc.Id, Contact_Type__c = 'Prospect', LeadSource = 'Other', Direct_Mail_Status__c = 'Do Not Send' );
try {
insert cont;
}
catch(DmlException e) {
System.debug(e.getMessage());
}
System.assertEquals(1, [select count() from Account where Name = 'TestAccount']);
SFDC_Candidate__c cand = new SFDC_Candidate__c(Contact__c = cont.Id, Company__c = acc.Id, Credentials__c = 'MBA', MBA_Alumni__c = 'Columbia', Source__c = 'Legacy', Sourcing_Channel__c = 'Legacy', Sourced_By__c = 'Legacy' );
try {
insert cand;
}
catch(DmlException e) {
System.debug(e.getMessage());
}
//System.assertEquals(1, [select count() from SFDC_Candidate__c where Contact__c = :cont.Id]);
Event ev = new Event(Event_Type__c = '1a - Resume Pending', Subject = 'Other', DurationInMinutes = 5, ActivityDateTime = Datetime.now(), WhatId = cand.Id);
try {
insert ev;
}
catch(DmlException e) {
System.debug(e.getMessage());
}
Event ev2 = new Event(Event_Type__c = '1 - Qualified Profile', Subject = 'Other', DurationInMinutes = 5, ActivityDateTime = Datetime.now(), WhatId = cand.Id);
try {
insert ev2;
}
catch(DmlException e) {
System.debug(e.getMessage());
}
Event ev3 = new Event(Event_Type__c = '2 - Qualified Resume', Subject = 'Other', DurationInMinutes = 5, ActivityDateTime = Datetime.now(), WhatId = cand.Id);
try {
insert ev3;
}
catch(DmlException e) {
System.debug(e.getMessage());
}
Event ev4 = new Event(Event_Type__c = '2a - Connection Pending', Subject = 'Other', DurationInMinutes = 5, ActivityDateTime = Datetime.now(), WhatId = cand.Id);
try {
insert ev4;
}
catch(DmlException e) {
System.debug(e.getMessage());
}
Event ev5 = new Event(Event_Type__c = '2b - Connection Made', Subject = 'Other', DurationInMinutes = 5, ActivityDateTime = Datetime.now(), WhatId = cand.Id);
try {
insert ev5;
}
catch(DmlException e) {
System.debug(e.getMessage());
}
Event ev6 = new Event(Event_Type__c = '3 - Phone Pre-Screen', Subject = 'Other', DurationInMinutes = 5, ActivityDateTime = Datetime.now(), WhatId = cand.Id);
try {
insert ev6;
}
catch(DmlException e) {
System.debug(e.getMessage());
}
Event ev7 = new Event(Event_Type__c = '3a - Pending', Subject = 'Other', DurationInMinutes = 5, ActivityDateTime = Datetime.now(), WhatId = cand.Id);
try {
insert ev7;
}
catch(DmlException e) {
System.debug(e.getMessage());
}
Event ev8 = new Event(Event_Type__c = '3b - DLC Rejected', Subject = 'Other', DurationInMinutes = 5, ActivityDateTime = Datetime.now(), WhatId = cand.Id);
try {
insert ev8;
}
catch(DmlException e) {
System.debug(e.getMessage());
}
Event ev9 = new Event(Event_Type__c = '3c - Candidate Withdrew', Subject = 'Other', DurationInMinutes = 5, ActivityDateTime = Datetime.now(), WhatId = cand.Id);
try {
insert ev9;
}
catch(DmlException e) {
System.debug(e.getMessage());
}
Event ev10 = new Event(Event_Type__c = '3d - Qualified Future', Subject = 'Other', DurationInMinutes = 5, ActivityDateTime = Datetime.now(), WhatId = cand.Id);
try {
insert ev10;
}
catch(DmlException e) {
System.debug(e.getMessage());
}
Event ev11 = new Event(Event_Type__c = '3e - Interested/Future', Subject = 'Other', DurationInMinutes = 5, ActivityDateTime = Datetime.now(), WhatId = cand.Id);
try {
insert ev11;
}
catch(DmlException e) {
System.debug(e.getMessage());
}
Event ev12 = new Event(Event_Type__c = '3f - Interested/Ready', Subject = 'Other', DurationInMinutes = 5, ActivityDateTime = Datetime.now(), WhatId = cand.Id);
try {
insert ev12;
}
catch(DmlException e) {
System.debug(e.getMessage());
}
Event ev14 = new Event(Event_Type__c = '4 - 1st Interview', Subject = 'Other', DurationInMinutes = 5, ActivityDateTime = Datetime.now(), WhatId = cand.Id);
try {
insert ev14;
}
catch(DmlException e) {
System.debug(e.getMessage());
}
Event ev15 = new Event(Event_Type__c = '4a - Pending', Subject = 'Other', DurationInMinutes = 5, ActivityDateTime = Datetime.now(), WhatId = cand.Id);
try {
insert ev15;
}
catch(DmlException e) {
System.debug(e.getMessage());
}
Event ev16 = new Event(Event_Type__c = '4b - DLC Rejected', Subject = 'Other', DurationInMinutes = 5, ActivityDateTime = Datetime.now(), WhatId = cand.Id);
try {
insert ev16;
}
catch(DmlException e) {
System.debug(e.getMessage());
}
Event ev17 = new Event(Event_Type__c = '4c - Candidate Withdrew', Subject = 'Other', DurationInMinutes = 5, ActivityDateTime = Datetime.now(), WhatId = cand.Id);
try {
insert ev17;
}
catch(DmlException e) {
System.debug(e.getMessage());
}
Event ev18 = new Event(Event_Type__c = '4d - Interested/ Future', Subject = 'Other', DurationInMinutes = 5, ActivityDateTime = Datetime.now(), WhatId = cand.Id);
try {
insert ev18;
}
catch(DmlException e) {
System.debug(e.getMessage());
}
Event ev19 = new Event(Event_Type__c = '5 - CSD Interview', Subject = 'Other', DurationInMinutes = 5, ActivityDateTime = Datetime.now(), WhatId = cand.Id);
try {
insert ev19;
}
catch(DmlException e) {
System.debug(e.getMessage());
}
Event ev20 = new Event(Event_Type__c = '5a - Pending', Subject = 'Other', DurationInMinutes = 5, ActivityDateTime = Datetime.now(), WhatId = cand.Id);
try {
insert ev20;
}
catch(DmlException e) {
System.debug(e.getMessage());
}
Event ev21 = new Event(Event_Type__c = '5b - DLC Rejected', Subject = 'Other', DurationInMinutes = 5, ActivityDateTime = Datetime.now(), WhatId = cand.Id);
try {
insert ev21;
}
catch(DmlException e) {
System.debug(e.getMessage());
}
Event ev22 = new Event(Event_Type__c = '5c - Candidate Withdrew', Subject = 'Other', DurationInMinutes = 5, ActivityDateTime = Datetime.now(), WhatId = cand.Id);
try {
insert ev22;
}
catch(DmlException e) {
System.debug(e.getMessage());
}
Event ev23 = new Event(Event_Type__c = '5d - Interested/Future', Subject = 'Other', DurationInMinutes = 5, ActivityDateTime = Datetime.now(), WhatId = cand.Id);
try {
insert ev23;
}
catch(DmlException e) {
System.debug(e.getMessage());
}
Event ev24 = new Event(Event_Type__c = '5e - Interested/Ready', Subject = 'Other', DurationInMinutes = 5, ActivityDateTime = Datetime.now(), WhatId = cand.Id);
try {
insert ev24;
}
catch(DmlException e) {
System.debug(e.getMessage());
}
Event ev25 = new Event(Event_Type__c = '6 - Consultant Interview', Subject = 'Other', DurationInMinutes = 5, ActivityDateTime = Datetime.now(), WhatId = cand.Id);
try {
insert ev25;
}
catch(DmlException e) {
System.debug(e.getMessage());
}


Event ev48 = new Event(Subject = 'Test', ActivityDateTime = datetime.now(), DurationInMinutes = 60 );
//Event ev48 = new Event(Subject = 'Test', ActivityDate = date.today() );
try {
insert ev48;
}
catch(DmlException e)
{
System.debug(e.getMessage());
}

test.starttest();

ID newid = ev48.id;
Event ds48 = [SELECT id FROM Event WHERE id = :ev48.id LIMIT 1];
//ds48.ActivityDate = Date.today().addDays(3);
update ds48;
test.stoptest();
}
}

 

trigger Event_Before_Insert on Event (before update) {

for( Event currentEvent : Trigger.new )
{
datetime CreatedDate =CurrentEvent.CreatedDate;
date ActivityDate =CurrentEvent.ActivityDate;

date createdDateTemp = date.newInstance(CreatedDate.year(),CreatedDate.month(),CreatedDate.day());
//Integer numberofDays = createdDateTemp.daysBetween(ActivityDate);
//CurrentEvent.Location = string.valueof(numberofDays);

CurrentEvent.Location = string.valueof(createdDateTemp.daysBetween(ActivityDate));

}
}

 

 

 

Message Edited by bikla78 on 08-03-2009 10:23 PM
dmsx2oddmsx2od

Short answer this time:

 

Please set your test class and methods to private.  This is the way it is set up in the Apex documentation.

 

Without knowing the regular code, one cannot help with the test code, but it looks like you're either setting up a ton of data on which you'll perform some sort of manipulation (which would be good) or you're testing a ton of before insert triggers, in which case you may wish to (and I may get a ton of flame for this) set them all up, add each Event to a single List<Event>, and then insert that List<Event> (then update it as well):

 

List<Event> events = new List<Event>();

Event a = new Event(...);

events.add(a);

Event b = new Event(...);

events.add(b);

...

test.starttest();

insert events;

update events;

test.stoptest();

 

I'm not going to explain why I use starttest and stoptest - they're both in the same area of the documentation. :)

 

Happy coding,

David

 

This was selected as the best answer
bikla78bikla78

David,

 

Good news.  First of all, I changed the test class back to private. I deployed that first into production with success. Then, I deployed the trigger and it tested with success. So it worked! Thank you so much and it's been a good learning experience. thank you thank you!

 

have a great night

DevAngelDevAngel

One point of clarification on the insert call.  According to the documentation here the Id of any inserted sObject will be set on successful insert.

 

:smileywink:

 

Cheers