Skip to main content
Hello All,

I created a custom Apex class with invocable method to be able to perform some timezone conversion calculations at a certain point in a Visual Flow.  It works great, but I need to create a test class to get my test coverage above 75% to be able to create a managed package with it.

This is the class with the invokable method:

 

global class TimeZoneConverter

{

@InvocableMethod(label='Convert Timezone DateTime' description='Converts a date time timezone string to a DateTime value')

public static List<Datetime> UTCdateValue(List<TimezoneConvertRequest> ConvertMe)

{

Timezone tz = Timezone.getTimeZone(ConvertMe[0].tzRequest); //now I have an official Timezone datatype variable tz

Datetime dt = Datetime.parse(ConvertMe[0].dtRequest.format()); //now I have a UTC Datetime string

Integer offsetMS = tz.getOffset(dt); //now I have the Milliseconds difference between the timezone selected and the UTC datetime string

Integer offsetHrs = (((offsetMS / 1000)/60)/60)*-1; //now I have the offset in hours

Datetime adjdt = dt.addHours(offsetHrs); //should be the adjusted value.

List<Datetime> returnMe = new List<Datetime>();

returnMe.add(adjdt);

return returnMe;

}

global class TimezoneConvertRequest {

@InvocableVariable(required=true)

public String tzRequest;

@InvocableVariable(required=true)

public Datetime dtRequest;

}

}

and this is as far as I've been able to get with creating the test class:

 

@isTest

class TimeZoneConverterTest {

@isTest static void testTimezoneConvertRequest() {

Test.startTest();

DateTime BroadcastItemDateTimeTest = Datetime.parse('2016-01-01 13:00:00');

string Timezone_Selected = 'US/Eastern';

List<TimeZoneConverter.TimezoneConvertRequest> testingThis = new List<TimeZoneConverter.TimezoneConvertRequest>();

TimeZoneConverter.TimezoneConvertRequest ConvertMeTest = new TimeZoneConverter.TimezoneConvertRequest();

ConvertMeTest.tzRequest = Timezone_Selected;

ConvertMeTest.dtRequest = BroadcastItemDateTimeTest;

testingThis.add(ConvertMeTest);

TimeZoneConverter ConvertTest = new TimeZoneConverter();

ConvertTest.UTCdateValue(TestingThis);

DateTime testResponse = ConvertTest[0];

Test.stopTest();

System.assertEquals(Datetime.parse('2016-01-01 13:00:00'), testResponse);

}

}

I'm getting an error of "Static methods cannot be invoked through an Object instance: UTCdateValue(List)" on line 13 - the one that reads:

 

ConvertTest.UTCdateValue(TestingThis);

I've gone around and around on this one and can't find a resource that spells it out quite clearly enough for me... Can someone help me get my code coverage up to snuff?

Thanks!

Adam
4 个回答
  1. 2020年5月28日 00:58

    Adam Coppin 13

    Thank you! I don't consider myself a developer, and I've been trying to create a test class for hours, thanks to this publication I was able to do it in minutes!
0/9000