Workflow IF Statement Errors Out when Checking Opened_At Date

appstorm
Tera Contributor

I am needing to return 'true' if requested item is opened within 4 days of a term start date.  As part of the script, term_code from the u_term table should match the termCode variable previously defined in the workflow.  Everything passes correctly, except the workflow errors out when it gets to this step.

 

 

// Assume termCode is already defined and holds the value you need to check
var termCode; // This should be set previously in the workflow

// Get the opened_at date from the current requested item
var openedAtDate = current.opened_at;

// Check if opened_at is defined
if (!openedAtDate || !termCode) {
    return false; // Exit early if there's no opened_at date or termCode is not set
}

// Create a GlideDateTime object for the opened_at date
var openedAtDT = new GlideDateTime(openedAtDate);

// Query the u_term table
var termGR = new GlideRecord('u_term');
termGR.addQuery('u_term_code', termCode); // Match TERM_CODE
termGR.addQuery('u_start_date', '>=', openedAtDT.getDisplayValue()); // Start date >= opened_at
termGR.addQuery('u_start_date', '<=', openedAtDT.addDays(4).getDisplayValue()); // Start date <= opened_at + 4 days
termGR.query();

// Check if any records are found
if (termGR.hasNext()) {
    return true; // u_start_date is within 4 days and u_term_code matches
} else {
    return false; // No matching u_start_date or u_term_code
}

 

 

I have verified all field names match their corresponding tables.  Any help is greatly appreciated!

1 REPLY 1

Brad Bowman
Kilo Patron
Kilo Patron

Is this running in a (legacy) workflow or flow (designer)?  What error are you seeing when this runs?  Log openedAtDt to confirm the value.  I wouldn't use getDisplayValue() in the addQuery.  Try manually filtering a list view on your u_term table building a simple filter to start - Opened is less than 0 hours before u_start_date - or vice-versa if the field label for u_start_date doesn't appear in the last box, so then you can copy either the simple filter, or once you build it to the exact one you want with other conditions, then you can add that as an encoded query and concatenate in termCode and openedAtDT.