0

I need some help with writing an invocable Apex class/method.

// assume a Date object startDate exists already
Timezone tz = Timezone.getTimeZone('Australia/Sydney');
Integer offset = tz.getOffset(startDate);
Time t = Time.newInstance(9, 30, 0, 0);
Datetime dt = newInstanceGmt(startDate, t).addSeconds(-offset/60);

The Apex class needs to:

2
  • 4
    What have you tried so far? What research have you done? Have you gone through the documentation that you've linked to? What about any relevant trailhead modules?
    – Derek F
    May 30, 2019 at 13:06
  • I've done plenty of research, which is how I came across the two articles I linked in my post. I'm not a developer, and unfortunately I wasn't able to set up the Apex class myself. Hence, I decided to post my question here. Given that most people appreciate brevity, and this seems to be a fairly simple question for a Salesforce developer, I didn't list the different things that I tried.
    – M H
    May 30, 2019 at 15:00

1 Answer 1

3

You just need to pass the date to the invocable method as a parameter since it is a primitive data type.

public class calculateStartDateTime {

@InvocableMethod(label='Calculate Date and Time' description='Converts date values to date/time values.')
public static List<Datetime> getStartDates(List<Date> startDates)
    { 
        Timezone tz = Timezone.getTimeZone('Australia/Sydney');
        List<Datetime> dateTimes = new List<Datetime>();
        for (Date startDate : startDates)
        {
            Integer offset = tz.getOffset(startDate);
            Time t = Time.newInstance(9, 30, 0, 0);
            Datetime dt = Datetime.newInstanceGmt(startDate, t).addSeconds(-offset/1000);
            dateTimes.add(dt);
        }
        Return dateTimes;
    }
}

Important notes about invocable methods

Inputs and Outputs

There can be at most one input parameter and its data type must be one of the following:

A list of a primitive data type or a list of lists of a primitive data type – the generic Object type is not supported.

A list of an sObject type or a list of lists of an sObject type – the generic sObject type is not supported.

A list of a user-defined type, containing variables of the supported types and with the InvocableVariable annotation. Create a custom global or public Apex class to implement your data type, and make sure your class contains at least one member variable with the invocable variable annotation.

If the return type is not Null, the data type returned by the method must be one of the following:

A list of a primitive data type or a list of lists of a primitive data type – the generic Object type is not supported.

A list of an sObject type or a list of lists of an sObject type – the generic sObject type is not supported.

A list of a user-defined type, containing variables of the supported types and with the InvocableVariable annotation. Create a custom global or public Apex class to implement your data type, and make sure your class contains at least one member variable with the invocable variable annotation.

Sources: Invocable Methods Salesforce Help

4
  • Thanks so much mate. Your comment was truncated, but I presume you meant to say that 'void' should be replaced with the data type of the value I'm trying to return? I made some changes to the code accordingly (please see this screenshot), however, I am now receiving this error: "Unsupported parameter type Date. Valid invocable parameters must be List types like List<T> where T is a supported type". Any ideas?
    – M H
    May 30, 2019 at 15:17
  • 2
    @MoH As the error says, you need to return a List<Date>; your logic should presume that the incoming values may be a bulk request, and so the output must also support bulk requests.
    – sfdcfox
    May 30, 2019 at 16:22
  • 2
    @MoH that's interesting and seems like a discrepancy in the documentation as it specifies that an invocable method can accept a single primitive data type. But I agree with sfdcfox that just passing the date as a single parameter will resolve your issue May 30, 2019 at 16:28
  • Thank you for your advice! I managed to make these changes, the method works and can now be invoked from a flow.
    – M H
    Jun 4, 2019 at 3:30

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.