2

Basically, I've written a trigger that will update a custom field on an User object when the value of a standard field changes. The trigger works when it is "triggered" via actions made by me on the salesforce.com UI and the custom field is updated correctly.

Now, I'm trying to write my first unit test(on force.com). My issue is that in my testMethod I will "update" a user object, but the trigger never runs. At least not as far as I can tell because my custom field's value remains unchanged.

Here is the code for my unit test, if anything else is needed let me know:

@isTest
public class updateUserTimezoneTest{
    static testMethod void testUpdateUserTimezone(){
        User testUser;

        Profile p = [select id from profile where name='Standard User'];
        testUser = new User(alias = 'standt', email='standarduser@testorg.com',
            emailencodingkey='UTF-8', lastname='Testing', languagelocalekey='en_US',
            localesidkey='en_US', profileid = p.Id,
            timezonesidkey='America/Los_Angeles', Time_Zone__c = 'PST', username='standarduser@testorg.com');
        insert testUser;
        System.debug('Created and inserted testUser');

        test.startTest();
        /*
            test all Time_Zone__c and TimeZoneSidKey mappings are 
            maintained on user's TimeZoneSidKey update.
            the mapping:

            TimeZoneSidKey - Time_Zone__c  
            "America/Chicago" – "CST"
            "America/Indianapolis" – "EST"
            "America/New_York" – "EST"
            "Pacific/Honolulu" – "HST"
            "America/Denver" – "MST"
            "America/Phoenix" - "MST/Phoenix"
            "America/Los_Angeles" – "PST"
            *Any other Time Zone – "None"
         */

        testUser.TimeZoneSidKey = 'America/Chicago';
        update testUser;
        testUser = [SELECT Time_Zone__c, TimeZoneSidKey FROM User WHERE Id = :testUser.Id];
        System.assertEquals('America/Chicago', testUser.TimeZoneSidKey);
        System.assertEquals('CST', testUser.Time_Zone__c);


        testUser.TimeZoneSidKey = 'America/Indianapolis';
        update testUser;
        testUser = [SELECT Time_Zone__c, TimeZoneSidKey FROM User WHERE Id = :testUser.Id];
        System.assertEquals('EST', testUser.Time_Zone__c);
        System.assertEquals('America/Indianapolis', testUser.TimeZoneSidKey);

        testUser.TimeZoneSidKey = 'America/New_York';
        update testUser;
        testUser = [SELECT Time_Zone__c, TimeZoneSidKey FROM User WHERE Id = :testUser.Id];
        System.assertEquals('EST', testUser.Time_Zone__c);
        System.assertEquals('America/New_York', testUser.TimeZoneSidKey);

        testUser.TimeZoneSidKey = 'Pacific/Honolulu';
        update testUser;
        testUser = [SELECT Time_Zone__c, TimeZoneSidKey FROM User WHERE Id = :testUser.Id];
        System.assertEquals('HST', testUser.Time_Zone__c);
        System.assertEquals('Pacific/Honolulu', testUser.TimeZoneSidKey);

        testUser.TimeZoneSidKey = 'America/Denver';
        update testUser;
        testUser = [SELECT Time_Zone__c, TimeZoneSidKey FROM User WHERE Id = :testUser.Id];
        System.assertEquals('MST', testUser.Time_Zone__c);
        System.assertEquals('America/Denver', testUser.TimeZoneSidKey);

        testUser.TimeZoneSidKey = 'America/Phoenix';
        update testUser;
        testUser = [SELECT Time_Zone__c, TimeZoneSidKey FROM User WHERE Id = :testUser.Id];
        System.assertEquals('MST/Phoenix', testUser.Time_Zone__c);
        System.assertEquals('America/Phoenix', testUser.TimeZoneSidKey);

        testUser.TimeZoneSidKey = 'America/Los_Angeles';
        update testUser;
        testUser = [SELECT Time_Zone__c, TimeZoneSidKey FROM User WHERE Id = :testUser.Id];
        System.assertEquals('PST', testUser.Time_Zone__c);
        System.assertEquals('America/Los_Angeles', testUser.TimeZoneSidKey);
        test.stopTest();
    }
}

Update, Here is the code for the trigger:

    trigger updateUserTimezone on User (before update) {

    for (User u : Trigger.new) {
        // found the user who triggered this event.
        if( UserInfo.getUserId() == u.Id ) {

            // When a user changes their locale setting, update their timzone
            User userInfo = [SELECT Time_Zone__c, TimeZoneSidKey FROM User WHERE Id = :u.Id];

            /*
                the mapping:

                TimeZoneSidKey - Time_Zone__c  
                "America/Chicago" – "CST"
                "America/Indianapolis" – "EST"
                "America/New_York" – "EST"
                "Pacific/Honolulu" – "HST"
                "America/Denver" – "MST"
                "America/Phoenix" - "MST/Phoenix"
                "America/Los_Angeles" – "PST"
                *Any other Time Zone – "None"

            */
            if( u.TimeZoneSidKey == 'America/Chicago' ){
                u.Time_Zone__c = 'CST';
            }
            else if( u.TimeZoneSidKey == 'America/Indianapolis' ) {
                u.Time_Zone__c = 'EST';
            }
            else if( u.TimeZoneSidKey == 'America/New_York' ) {
                u.Time_Zone__c = 'EST';
            }
            else if( u.TimeZoneSidKey == 'Pacific/Honolulu' ) {
                u.Time_Zone__c = 'HST';
            }
            else if( u.TimeZoneSidKey == 'America/Denver' ) {
                u.Time_Zone__c = 'MST';
            }
            else if( u.TimeZoneSidKey == 'America/Phoenix' ) {
                u.Time_Zone__c = 'MST/Phoenix';
            }
            else if( u.TimeZoneSidKey == 'America/Los_Angeles' ) {
                u.Time_Zone__c = 'PST';
            }
            else{
                u.Time_Zone__c = 'None';
            }
        }
    }
}
  • Posting the code for the trigger in question might help determine the problem. – JCD Jul 1 '13 at 16:39
  • @JCD I updated it with the trigger code. – EthanLWillis Jul 1 '13 at 16:45
5

Looking at the code, it looks like the time zone will be updated only when the user record gets updated by that user himself. And this is not happening in your test class. You have to use System.runAs(u) to accomplish this (refer this).

Try adding this before you update the user record-

....
test.startTest();
System.runAs(testUser){
     testUser.TimeZoneSidKey = 'America/Chicago';
     update testUser;
}
....

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.