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
TiborTibor 

pls help with trigger 0% coverage

Hi,

 

we have 2 fields - SRQ_AGREED_TIME and SRQ_AGREED_TIME_OLD

for some purposes we need to save old value to SRQ_AGREED_TIME_OLD when SRQ_AGREED_TIME is changed.

 

 

i wrote trigger:

 

trigger SRq_agreed_time_save_old_value on Case (before update) {

  Case[] item_old = Trigger.old;

  Case[] item_new = Trigger.new;

 

  for ( Case item : item_new)

  {

    if (item_old[0].SRq_agreed_time__c != null)

    {

      item.SRq_agreed_time_old__c = item_old[0].SRq_agreed_time__c;

    }

  }

}

 

 

and test method: 

 

@isTest

private class Srq_agreed_time_tests {

 

  static testMethod void beforeUpdateCase() {

  test.starttest();

  Case[] cases = [Select id, SRq_agreed_time__c from Case where Case.Status != 'Closed'];

 

  for(Case item : cases)

  {

    item.SRq_agreed_time__c = DateTime.now();

    }

 

      test.stoptest();

    }

}

 

 

I don't know If I don't understand how to write test methods but I read article about writing more times.

Please help me or tell me som secrets :) how to edit it and successfuly deploy trigger to server??

 

thank you

 

Best Answer chosen by Admin (Salesforce Developers) 
GoodGrooveGoodGroove

Tibor,

 

 

you didn't force the trigger to fire, because you didn't update the cases in your test class.

I have modified your trigger a little bit, to ensure that the old value is stored, every time SRq_agreed_time__c changes.

Test Class works with your condition  (SRq_agreed_time__c != null ) to

 

 

Trigger:

 

 

trigger SRq_agreed_time_save_old_value on Case (before update) {

Case[] items_old = Trigger.old;

Case[] items_new = Trigger.new;



// Loop through the records

for( integer i = 0; i < items_new.size(); i++){



Case item_new = items_new.get(i);


Case item_old = items_old.get(i);



system.debug('@@@@@@@@@ New Item : ' + ' Time new: ' + item_new.SRq_agreed_time__c + ' Time old: ' + item_new.SRq_agreed_time_old__c);


system.debug('@@@@@@@@@ Old Item : ' + ' Time new: ' + item_old.SRq_agreed_time__c + ' Time old: ' + item_old.SRq_agreed_time_old__c);





// Check if there have been any changes, ....

if(item_new.SRq_agreed_time__c != item_old.SRq_agreed_time__c){

// ... and set the new 'old' if required

item_new.SRq_agreed_time_old__c = item_old.SRq_agreed_time__c;




system.debug('@@@@@@@@@ Updating SRq_agreed_time_old__c from: ' + item_new.SRq_agreed_time_old__c + ' to: ' + item_old.SRq_agreed_time__c);



}
}

}

 

 

 

 

Test Class: 

  

 

@isTest


private class SRq_agreed_time_save_old_value__TEST {




static testMethod void test(){


// Better set up some Test Records


Case[] cases = [Select id, SRq_agreed_time__c, SRq_agreed_time_old__c from Case where Case.Status != 'Closed' LIMIT 2];



List<Case> UpdateList = new List<Case>();




// Loop through the cases and set the Agreed Time to NOW



for(Case c : cases){

c.SRq_agreed_time__c = DateTime.now();



UpdateList.add(c); // Add Case to Update List

}

database.update(UpdateList); // Update the Records and force the trigger to fire



}


}

 

 

 

Rgds

Sebastian 

Message Edited by GoodGroove on 09-29-2009 06:57 PM

All Answers

McAdminMcAdmin

Is this the article you read?   (Running Apex Unit Tests)

 

 

 

You can run the unit tests for your Apex scripts using the Salesforce user interface. You can run unit tests for a specific class or you can run all the unit tests in your organization. Unit test methods take no arguments, commit no data to the database, send no emails, and are flagged with the testMethod keyword in the method definition.

To run the unit tests for a specific class, click Setup | Develop | Apex Classes, click the name of the class, then click Run Test. If your class calls another class or causes a trigger to execute, those Apex scripts are included in the total amount used for calculating the percentage of code covered.

To run all the unit tests in your organization, click Setup | Develop | Apex Classes, then click Run All Tests.

The result page for running unit tests contains the following sections. Each section can be expanded or collapsed.
  • A summary section that details the number of tests run, the number of failures, and the percentage of Apex scripts that are covered by unit testsImportant
    • You must have at least 75% of your Apex scripts covered by unit tests to deploy your scripts to production environments. In addition, all triggers should have some test coverage.
    • Salesforce recommends that you have 100% of your scripts covered by unit tests, where possible.
    • Calls to System.debug are not counted as part of Apexcode coverage in unit tests.
  • Test failures, if any
  • A code coverage section

    This section lists all the classes and triggers in your organization and the percentage of lines of code in each class and trigger that are covered by tests. If you click on the coverage percent number, a page displays, highlighting all the lines of code for that class or trigger that are covered by tests in blue, as well as highlighting all the lines of code that are not covered by tests in red. It also lists how many times a particular line in the class or trigger was executed by the test.

  • Test coverage warnings, if any
  • The debug log

    The debug log is automatically set to the PROFILE log level. You cannot change the log level. The PROFILE log level includes log messages generated by calls to the System.debug method, every DML statement or inline SOQL or SOSL query, and the entrance and exit of every user-defined method. In addition, the end of the debug log contains overall profiling information for the portions of the request that used the most resources, in terms of SOQL and SOSL statements, DML operations, and Apex method invocations. These three sections list the locations in the code that consumed the most time, in descending order of total cumulative time, along with the number of times they were executed.

For more information on the debug log, see Debug Log Details. 

TiborTibor
nice, ... i read it.. but i still don't know how to write test method for my trigger.
aalbertaalbert

Here is an introductory article that might help get started. 

ThomasTTThomasTT
Trigger is not triggered if you don't  update the database.

Didn't you forget one line? like

 

update cases;

 

?

Program (even something in the majestic SFDC) is very stupid so that it can't "guess" how you want to test it.

 

 

And, you assume that there are cases with status != 'Closed', but you never know.

I storongly recommend that you create the test Case data in the test method by insert, and update the value and do update.

 

ThomasTT

 

 

Message Edited by ThomasTT on 09-29-2009 12:18 PM
GoodGrooveGoodGroove

Tibor,

 

 

you didn't force the trigger to fire, because you didn't update the cases in your test class.

I have modified your trigger a little bit, to ensure that the old value is stored, every time SRq_agreed_time__c changes.

Test Class works with your condition  (SRq_agreed_time__c != null ) to

 

 

Trigger:

 

 

trigger SRq_agreed_time_save_old_value on Case (before update) {

Case[] items_old = Trigger.old;

Case[] items_new = Trigger.new;



// Loop through the records

for( integer i = 0; i < items_new.size(); i++){



Case item_new = items_new.get(i);


Case item_old = items_old.get(i);



system.debug('@@@@@@@@@ New Item : ' + ' Time new: ' + item_new.SRq_agreed_time__c + ' Time old: ' + item_new.SRq_agreed_time_old__c);


system.debug('@@@@@@@@@ Old Item : ' + ' Time new: ' + item_old.SRq_agreed_time__c + ' Time old: ' + item_old.SRq_agreed_time_old__c);





// Check if there have been any changes, ....

if(item_new.SRq_agreed_time__c != item_old.SRq_agreed_time__c){

// ... and set the new 'old' if required

item_new.SRq_agreed_time_old__c = item_old.SRq_agreed_time__c;




system.debug('@@@@@@@@@ Updating SRq_agreed_time_old__c from: ' + item_new.SRq_agreed_time_old__c + ' to: ' + item_old.SRq_agreed_time__c);



}
}

}

 

 

 

 

Test Class: 

  

 

@isTest


private class SRq_agreed_time_save_old_value__TEST {




static testMethod void test(){


// Better set up some Test Records


Case[] cases = [Select id, SRq_agreed_time__c, SRq_agreed_time_old__c from Case where Case.Status != 'Closed' LIMIT 2];



List<Case> UpdateList = new List<Case>();




// Loop through the cases and set the Agreed Time to NOW



for(Case c : cases){

c.SRq_agreed_time__c = DateTime.now();



UpdateList.add(c); // Add Case to Update List

}

database.update(UpdateList); // Update the Records and force the trigger to fire



}


}

 

 

 

Rgds

Sebastian 

Message Edited by GoodGroove on 09-29-2009 06:57 PM
This was selected as the best answer