1

I am using Apex to retrieve 2 Date fields from the FundingOpportunity object and I want to pass them back to my OmniScript via Integration Procedure. However the issue I'm running into is they are returning as yyyy-MM-dd and I need it in MM-dd-yyyy format instead.

Here is my Apex:

Date startDate;
Date endDate;

List<FO> fundOpp = [SELECT Period_Start__c, Period_End__c FROM FO];
for(FO fo : fundOpp) {
   startDate = fo.Period_Start__c;
   endDate = fo.Period_End__c;
   break;
}
System.debug('startDate: ' + startDate);

Response r = new Response();
r.startDate = startDate;
r.endDate = endDate;
output.put('result', JSON.serialize(r));
System.debug('output: ' + output);

I put the 2 debug statements to see what was going on and the first returns yyyy-MM-dd HH:MM:SS and the second returns {result={"startDate":"yyyy-MM-dd","endDate":"yyyy-MM-dd"}}. I've tried adding .format() but then I get illegal assignment from string to date So I'm pretty stuck, how do I format this properly?

Thanks

1 Answer 1

2

Short answer:

You need to cast it as a DateTime object, otherwise you won't be able to use the format(dateFormatString) method. The TestDate__c field used in the following example is a Date field.

Account acc = [Select Id, TestDate__c FROM Account ORDER BY LastModifiedDate DESC LIMIT 1];
DateTime dateTimeVar = acc.TestDate__c;
String formattedDateStr = dateTimeVar.format('MM-dd-yyyy', 'GMT+00:00');

System.debug('Before formatting: ' + dateTimeVar);
System.debug('After formatting: ' + formattedDateStr);

Previous code will yield the following result:

Before formatting: 2024-04-18 00:00:00
After formatting: 04-18-2024

Long answer:

Apex is returning the Date field value as a DateTime in the format of yyyy-mm-dd hh:mm:ss:

Format returned from Apex

You will need to use Omniscript with Remote Action element and a Step element containing the Date input:

OS overview

Date input format is set the same as the format in your question, i.e. MM-dd-yyyy:

Date Input

Your Omniscript will call our remote action with the following settings:

Remote Action settings

And your date will reference the returned value using the merge syntax:

Value reference syntax

Apex used as a remote action:

global with sharing class RemoteActionTest implements vlocity_ins.VlocityOpenInterface2
   {
            
    global Boolean invokeMethod(String methodName, Map<String,Object> inputMap, Map<String,Object> outMap, Map<String,Object> options) {
    Boolean success = true;
    String errors = 'OK';
    try {
      if (methodName.equals('retrieveDate')) {
        Account acc = [Select Id, TestDate__c FROM Account ORDER BY LastModifiedDate DESC LIMIT 1];
        DateTime dateTimeVar = acc.TestDate__c;
        String formattedDateStr = dateTimeVar.format('MM-dd-yyyy', 'GMT+00:00');
        outMap.put('result', formattedDateStr);
      } else {
        errors = 'Method ' + methodName + ' was not found.';
      }
    } catch (Exception e) {
      System.debug(e.getMessage());
      success = false;
    }
        
    outMap.put('success', success);
    outMap.put('error', errors);

    return success;       
   }    
}

When running the Omniscript, the date input is prepopulated:

Prepopulated result

3
  • hey thanks so much for this! but unfortunately it returns 1 day before? I'm actually getting the values via an integration procedure and then using them in a textblock on the OS as merge fields. Before formatting it pulls the value as 2024-03-01 but when I format it using your code (and also tried using =FORMATDATETIME() function on a SetValues in the IP) it returns 02-29-2024
    – stormy
    Apr 8 at 19:22
  • @stormy You're right. The format method converts the DateTime instance to the specific format based on the user's locale. Your user locale is probably GMT < 0, so that's why it deducted a few hours from our DateTime, making it become the day before. When we casted the Date instance to DateTime, it automatically added the time aspect of the date to 00:00:00, so if we deduct two hours, that makes it 22:00:00 of the previous day. I've edited my code to include the default timezone Salesforce uses internally to store the DateTIme: dateTimeVar.format('MM-dd-yyyy', 'GMT+00:00')
    – DevelBase2
    Apr 8 at 19:53
  • worked like a charm, thank you!
    – stormy
    Apr 8 at 20:09

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .