• Adam Coppin 13
  • NEWBIE
  • 15 Points
  • Member since 2016

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 2
    Replies
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
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
Hi,

trying to complete the challenge, I have the following error:
The "Customer Community User" for the contact "Edna Frank" is not created properly.

More info, hope this helps finding the problem:
The contact exist, in Admin > Users> User license is "Customer Portal Manager Standard", when I try to change it to "Customer community" i'm getting the error "cannot upgrade from or downgrade to LPU"
The contact is assigned to the account "Customers", there is a case associated to it, with Contact Name = Edna Frank
I created the Sharing Set Settings and configured the access (User:Account = Case:Account, Read/Write). The community is active.
Oddly enough, I cannot log in as Edna Frank.
If I'm trying to create a new user, I receive the following error: "Your request cannot be processed at this time. The site administrator has been alerted.
The account associated with this portal has no owner". The created account is "Customers" and I double checked, i'm the owner of this account.
I've received a message "Customers registration accountId parameter value is not valid."

Any idea or clues on how to solve this ?