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
Shawn ReichnerShawn Reichner 

Help with Test Class

Team,

I have created an Apex Trigger and Class to populate a field based on the values in 2 other fields.  

The trigger works like a charm, however I can not get the test class I created past 51 percent.  

Can anyone save the day by looking over the following Triiger, class, and test class to advise me where I need to beef up the test class to promote to my production environemnt?

Thank you in advance for your help!

Shawn

Trigger Code

trigger TimeZoneextract on SVMXC__Service_Order__c (before insert, before update) {

List<SVMXC__Service_Order__c> Service_Orders = Trigger.new;
    TimeZoneOffsetextraction.TimeZoneextract(Service_Orders);

Class Code

public class TimeZoneOffsetextraction {
    public static void TimeZoneextract(List<SVMXC__Service_Order__c> Service_Orders) {
   
        for (SVMXC__Service_Order__c w:Service_Orders){
        
        IF (w.Account_Time_Zone__c == 'PST' && w.My_Time_Zone__c == 'GMT'){
        w.Time_Zone_Offset__c = '-8';
        }
        IF (w.Account_Time_Zone__c == 'MST' && w.My_Time_Zone__c == 'GMT'){
        w.Time_Zone_Offset__c = '-7';
        }
        IF (w.Account_Time_Zone__c == 'CST' && w.My_Time_Zone__c == 'GMT'){
        w.Time_Zone_Offset__c = '-6';
        }
        IF (w.Account_Time_Zone__c == 'EST' && w.My_Time_Zone__c == 'GMT'){
        w.Time_Zone_Offset__c = '-5';
        }
        IF (w.Account_Time_Zone__c == 'AKST' && w.My_Time_Zone__c == 'GMT'){
        w.Time_Zone_Offset__c = '-10';
        }
        IF (w.Account_Time_Zone__c == 'HST' && w.My_Time_Zone__c == 'GMT'){
        w.Time_Zone_Offset__c = '-10';
        }
        IF (w.Account_Time_Zone__c == 'PST' && w.My_Time_Zone__c == 'AST'){
        w.Time_Zone_Offset__c = '-4';
        }
        IF (w.Account_Time_Zone__c == 'MST' && w.My_Time_Zone__c == 'AST'){
        w.Time_Zone_Offset__c = '-3';
        }
        IF (w.Account_Time_Zone__c == 'CST' && w.My_Time_Zone__c == 'AST'){
        w.Time_Zone_Offset__c = '-2';
        }
        IF (w.Account_Time_Zone__c == 'EST' && w.My_Time_Zone__c == 'AST'){
        w.Time_Zone_Offset__c = '-1';
        }
        IF (w.Account_Time_Zone__c == 'AKST' && w.My_Time_Zone__c == 'AST'){
        w.Time_Zone_Offset__c = '-6';
        }
        IF (w.Account_Time_Zone__c == 'HST' && w.My_Time_Zone__c == 'AST'){
        w.Time_Zone_Offset__c = '-6';
        }
        IF (w.Account_Time_Zone__c == 'PST' && w.My_Time_Zone__c == 'EST'){
        w.Time_Zone_Offset__c = '-3';
        }
        IF (w.Account_Time_Zone__c == 'MST' && w.My_Time_Zone__c == 'EST'){
        w.Time_Zone_Offset__c = '-2';
        }
        IF (w.Account_Time_Zone__c == 'CST' && w.My_Time_Zone__c == 'EST'){
        w.Time_Zone_Offset__c = '-1';
        }
        IF (w.Account_Time_Zone__c == 'AKST' && w.My_Time_Zone__c == 'EST'){
        w.Time_Zone_Offset__c = '-5';
        }
        IF (w.Account_Time_Zone__c == 'HST' && w.My_Time_Zone__c == 'EST'){
        w.Time_Zone_Offset__c = '-5';
        }
        IF (w.Account_Time_Zone__c == 'EST' && w.My_Time_Zone__c == 'EST'){
        w.Time_Zone_Offset__c = '-0';
        }
        IF (w.Account_Time_Zone__c == 'PST' && w.My_Time_Zone__c == 'CST'){
        w.Time_Zone_Offset__c = '-2';
        }
        IF (w.Account_Time_Zone__c == 'MST' && w.My_Time_Zone__c == 'CST'){
        w.Time_Zone_Offset__c = '-1';
        }
        IF (w.Account_Time_Zone__c == 'CST' && w.My_Time_Zone__c == 'CST'){
        w.Time_Zone_Offset__c = '-0';
        }
        IF (w.Account_Time_Zone__c == 'EST' && w.My_Time_Zone__c == 'CST'){
        w.Time_Zone_Offset__c = '+1';
        }
        IF (w.Account_Time_Zone__c == 'AKST' && w.My_Time_Zone__c == 'CST'){
        w.Time_Zone_Offset__c = '-4';
        }
        IF (w.Account_Time_Zone__c == 'HST' && w.My_Time_Zone__c == 'CST'){
        w.Time_Zone_Offset__c = '-4';
        }
        IF (w.Account_Time_Zone__c == 'PST' && w.My_Time_Zone__c == 'MST'){
        w.Time_Zone_Offset__c = '-1';
        }
        IF (w.Account_Time_Zone__c == 'MST' && w.My_Time_Zone__c == 'MST'){
        w.Time_Zone_Offset__c = '-0';
        }
        IF (w.Account_Time_Zone__c == 'CST' && w.My_Time_Zone__c == 'MST'){
        w.Time_Zone_Offset__c = '+1';
        }
        IF (w.Account_Time_Zone__c == 'EST' && w.My_Time_Zone__c == 'MST'){
        w.Time_Zone_Offset__c = '+2';
        }
        IF (w.Account_Time_Zone__c == 'AKST' && w.My_Time_Zone__c == 'MST'){
        w.Time_Zone_Offset__c = '-3';
        }
        IF (w.Account_Time_Zone__c == 'HST' && w.My_Time_Zone__c == 'MST'){
        w.Time_Zone_Offset__c = '-3';
        }
        IF (w.Account_Time_Zone__c == 'PST' && w.My_Time_Zone__c == 'PST'){
        w.Time_Zone_Offset__c = '-0';
        }
        IF (w.Account_Time_Zone__c == 'MST' && w.My_Time_Zone__c == 'PST'){
        w.Time_Zone_Offset__c = '+1';
        }
        IF (w.Account_Time_Zone__c == 'CST' && w.My_Time_Zone__c == 'PST'){
        w.Time_Zone_Offset__c = '+2';
        }
        IF (w.Account_Time_Zone__c == 'EST' && w.My_Time_Zone__c == 'PST'){
        w.Time_Zone_Offset__c = '+3';
        }
        IF (w.Account_Time_Zone__c == 'AKST' && w.My_Time_Zone__c == 'PST'){
        w.Time_Zone_Offset__c = '-2';
        }
        IF (w.Account_Time_Zone__c == 'HST' && w.My_Time_Zone__c == 'PST'){
        w.Time_Zone_Offset__c = '-2';
        }
        IF (w.Account_Time_Zone__c == 'PST' && w.My_Time_Zone__c == 'AHST'){
        w.Time_Zone_Offset__c = '+2';
        }
        IF (w.Account_Time_Zone__c == 'MST' && w.My_Time_Zone__c == 'AHST'){
        w.Time_Zone_Offset__c = '+3';
        }
        IF (w.Account_Time_Zone__c == 'CST' && w.My_Time_Zone__c == 'AHST'){
        w.Time_Zone_Offset__c = '+4';
        }
        IF (w.Account_Time_Zone__c == 'EST' && w.My_Time_Zone__c == 'AHST'){
        w.Time_Zone_Offset__c = '+5';
        }
        IF (w.Account_Time_Zone__c == 'AKST' && w.My_Time_Zone__c == 'AHST'){
        w.Time_Zone_Offset__c = '-0';
        }
        IF (w.Account_Time_Zone__c == 'HST' && w.My_Time_Zone__c == 'AHST'){
        w.Time_Zone_Offset__c = '-0';
        }
        IF (w.Account_Time_Zone__c == 'PST' && w.My_Time_Zone__c == 'CET'){
        w.Time_Zone_Offset__c = '-9';
        }
        IF (w.Account_Time_Zone__c == 'MST' && w.My_Time_Zone__c == 'CET'){
        w.Time_Zone_Offset__c = '-8';
        }
        IF (w.Account_Time_Zone__c == 'CST' && w.My_Time_Zone__c == 'CET'){
        w.Time_Zone_Offset__c = '-7';
        }
        IF (w.Account_Time_Zone__c == 'EST' && w.My_Time_Zone__c == 'CET'){
        w.Time_Zone_Offset__c = '-6';
        }
        IF (w.Account_Time_Zone__c == 'AKST' && w.My_Time_Zone__c == 'CET'){
        w.Time_Zone_Offset__c = '-11';
        }
        IF (w.Account_Time_Zone__c == 'HST' && w.My_Time_Zone__c == 'CET'){
        w.Time_Zone_Offset__c = '-11';
        }
        IF (w.Account_Time_Zone__c == 'PST' && w.My_Time_Zone__c == 'EET'){
        w.Time_Zone_Offset__c = '-10';
        }
        IF (w.Account_Time_Zone__c == 'MST' && w.My_Time_Zone__c == 'EET'){
        w.Time_Zone_Offset__c = '-9';
        }
        IF (w.Account_Time_Zone__c == 'CST' && w.My_Time_Zone__c == 'EET'){
        w.Time_Zone_Offset__c = '-8';
        }
        IF (w.Account_Time_Zone__c == 'EST' && w.My_Time_Zone__c == 'EET'){
        w.Time_Zone_Offset__c = '-7';
        }
        IF (w.Account_Time_Zone__c == 'AKST' && w.My_Time_Zone__c == 'EET'){
        w.Time_Zone_Offset__c = '-12';
        }
        IF (w.Account_Time_Zone__c == 'HST' && w.My_Time_Zone__c == 'EET'){
        w.Time_Zone_Offset__c = '-12';
        }
        IF (w.Account_Time_Zone__c == 'PST' && w.My_Time_Zone__c == 'BT'){
        w.Time_Zone_Offset__c = '-11';
        }
        IF (w.Account_Time_Zone__c == 'MST' && w.My_Time_Zone__c == 'BT'){
        w.Time_Zone_Offset__c = '-10';
        }
        IF (w.Account_Time_Zone__c == 'cST' && w.My_Time_Zone__c == 'BT'){
        w.Time_Zone_Offset__c = '-9';
        }
        IF (w.Account_Time_Zone__c == 'EST' && w.My_Time_Zone__c == 'BT'){
        w.Time_Zone_Offset__c = '-8';
        }
        IF (w.Account_Time_Zone__c == 'AKST' && w.My_Time_Zone__c == 'BT'){
        w.Time_Zone_Offset__c = '-13';
        }
        IF (w.Account_Time_Zone__c == 'HST' && w.My_Time_Zone__c == 'BT'){
        w.Time_Zone_Offset__c = '-13';
        }
        IF (w.Account_Time_Zone__c == 'PST' && w.My_Time_Zone__c == 'CCT'){
        w.Time_Zone_Offset__c = '-16';
        }
        IF (w.Account_Time_Zone__c == 'MST' && w.My_Time_Zone__c == 'CCT'){
        w.Time_Zone_Offset__c = '-15';
        }
        IF (w.Account_Time_Zone__c == 'CST' && w.My_Time_Zone__c == 'CCT'){
        w.Time_Zone_Offset__c = '-14';
        }
        IF (w.Account_Time_Zone__c == 'EST' && w.My_Time_Zone__c == 'CCT'){
        w.Time_Zone_Offset__c = '-13';
        }
        IF (w.Account_Time_Zone__c == 'AKST' && w.My_Time_Zone__c == 'CCT'){
        w.Time_Zone_Offset__c = '-18';
        }
        IF (w.Account_Time_Zone__c == 'HST' && w.My_Time_Zone__c == 'CCT'){
        w.Time_Zone_Offset__c = '-18';
        }
        IF (w.Account_Time_Zone__c == 'PST' && w.My_Time_Zone__c == 'JST'){
        w.Time_Zone_Offset__c = '-17';
        }
        IF (w.Account_Time_Zone__c == 'MST' && w.My_Time_Zone__c == 'JST'){
        w.Time_Zone_Offset__c = '-16';
        }
        IF (w.Account_Time_Zone__c == 'CST' && w.My_Time_Zone__c == 'JST'){
        w.Time_Zone_Offset__c = '-15';
        }
        IF (w.Account_Time_Zone__c == 'EST' && w.My_Time_Zone__c == 'JST'){
        w.Time_Zone_Offset__c = '-14';
        }
        IF (w.Account_Time_Zone__c == 'AKST' && w.My_Time_Zone__c == 'JST'){
        w.Time_Zone_Offset__c = '-19';
        }
        IF (w.Account_Time_Zone__c == 'HST' && w.My_Time_Zone__c == 'JST'){
        w.Time_Zone_Offset__c = '-19';
        }
               }
 
            }        
        }

Test Class

@isTest(SeeAllData = true)
private class TestTimeZoneOffsetextraction {
    //test method
    static testMethod void testService_Order() {
       
      
       SVMXC__Service_Order__c w = New SVMXC__Service_Order__c (
       SVMXC__Case__c = '5004000000bSVJt',
       SVMXC__Company__c = '0014000000OCOao',
       SVMXC__Contact__c = '0034000001CZCxg',
       SVMXC__Order_Status__c = 'Open',
       SVMXC__Order_Type__c = 'Field Service',
       SVMXC__Priority__c = 'P4',
       Escallations__c = 'Tier 1 - AVI-SPL Field Techs',
       Is_Billable_new__c = 'No',
       SVMXC__Purpose_of_Visit__c = 'Testing',
       SVMXC__Billing_Type__c = 'Contract',
       Alert_Message__c = 'Testing',
       Criticality__c = 'T&M',
       Rate_Category_wm__c = 'Metro 6-8pm',
       After_Hours_or_Holiday__c = 'No',
       Day__c = 'Weekday',
       Internal_Comments__c = 'Testing',
       Initial_Room_Availability__c = 'Testing',
       wm_desired_skills__c = 'Testing',
       Technician_wm__c = 'Test Tester',
       Technician_email_wm__c = 'test@test.com',
       SVMXC__City__c = 'Tampa',
       SVMXC__Street__c = '123 Anywhere street',
       SVMXC__Zip__c = '33615',
       SVMXC__Country__c = 'United States',
       SVMXC__State__c = 'WA',
       My_Time_Zone__c = 'MST',
       Time_Zone_Offset__c = '-1',
       SVMXC__Problem_Description__c = 'Testing',
       Dispatch_Detail__c = 'Testing');
       
       insert w;


        SVMXC__Service_Order__c pop1 = [SELECT Id, Account_Time_Zone__c FROM SVMXC__Service_Order__c Where ID =:w.ID];

system.debug(pop1.Account_Time_Zone__c);
       }
       
     
       
    }
Best Answer chosen by Shawn Reichner
Art SmorodinArt Smorodin
What kind of field is "Account_Time_Zone__c"? Make sure that it is not set to read only. If you have it locked down or hidden for your profile. You might need to make it editable or visible. (Setup -> Security Controls -> Field Accessibility)
Also if it "Account_Time_Zone__c" is a formula, this solution might not work since formula fields can not be populated by triggers.

Art.

All Answers

Art SmorodinArt Smorodin
Hi Shawn, 

most of your class is IF conditions, so unless the condition is met the code inside it is ignored. So in order to boost your code coverage simply check for those conditions. The easiest way, probably, would be adding something like 
w.Account_Time_Zone__c = 'AKST'; 
       w.My_Time_Zone__c = 'MST';
	   update w;
        
       w.Account_Time_Zone__c = 'HST'; 
       w.My_Time_Zone__c = 'MST';
	   update w;
        
       w.Account_Time_Zone__c = 'PST'; 
       w.My_Time_Zone__c = 'PST';
	   update w;

into your TestTimeZoneOffsetextraction class after 
insert w;

If all goes well you can actually open up your TimeZoneOffsetextraction class in Developer Console and you will see which conditions got checked. 
User-added image
Hope this helps.

Art.
Shawn ReichnerShawn Reichner
Art, Thank you for your reply. I am now getting this error…. Error: Compile Error: Field is not writeable: SVMXC__Service_Order__c.Account_Time_Zone__c at line 40 column 8 Any other ideas that may help? Shawn [cert_program_rgb] [sf_cert_adm_rgb] [EmailSignatureBanner_Video] Connect with us: Facebook Twitter Linkedin YouTube Blog Pinterest
Art SmorodinArt Smorodin
What kind of field is "Account_Time_Zone__c"? Make sure that it is not set to read only. If you have it locked down or hidden for your profile. You might need to make it editable or visible. (Setup -> Security Controls -> Field Accessibility)
Also if it "Account_Time_Zone__c" is a formula, this solution might not work since formula fields can not be populated by triggers.

Art.
This was selected as the best answer
Shawn ReichnerShawn Reichner
Art,

Thank you again.  The field was indeed a formula field, which I was not aware of the formula restriction but noted now! Thank you! 

That allowed me to create a new field and re-code, and now I am testing great!  It was that formula field. 


Thanks again for your advice,

Shawn