2

Converting code to utilize the TimeZone class for easier maintainability. For now, I'm stuck relying on a pseudo-timezone field on contacts (c.Time_Zone__c below) to make the determination and setting a string variable to what I think are correct timeZoneIdStrings. I would like to use that string to instantiate a new TimeZone object and then call the getDisplayName method on the object. Is the timeZoneIdString parameter not actually of type String? If not, is there a way to get the necessary Id?

String tz;
if(c.Time_Zone__c == 'Eastern'){
    tz = 'America/New_York';
} else if(c.Time_Zone__c == 'Central'){
    tz = 'America/Chicago';
} else if(c.Time_Zone__c == 'Mountain'){
    tz = 'America/Denver';
} else if(c.Time_Zone__c == 'Pacific'){
    tz = 'America/Los_Angeles';
}
TimeZone tzone = TimeZone.getTimeZone(tz);
timeZoneDisplay = tzone.getDisplayName();
  • 2
    I am not able to reproduce this error. What API Version are you using? Do you have any variables or other classes named TimeZone? – Adrian Larson May 6 at 20:50
  • Version 44.0. Strangely, it works from the Execute Anonymous Window (I can log a display name), but when I try to deploy from VS Code, or edit from the build screen in SF, I get the same error. I don't see any other class or variable named TimeZone. – Nate Gelle May 6 at 21:02
  • My apologies. There was a variable named timezone that I was missing. Thank you for your help. – Nate Gelle May 6 at 21:28
2

What often will cause this sort of error is shadowing. It can look something like this:

String timezone = 'some value';

//...

Timezone tzone = Timezone.getTimezone('some name');

Or you might have a class by the same name interfere:

public with sharing class Timezone { /*implementation*/ }

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.