How to re-arrange a string variable in Flow Designer
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-25-2023 06:55 AM
Dear All,
- I am handling an inbound email via Flow Designer
- I set a variable called "EndDate" in flow designer
- This variable is a string. I use the variable to parse a date which is in the email body
- The parsed variable has this format: DD/MM/YYYY (for example 19/02/2023)
- I need to use this variable to populate a "Date and Time" field in the sc_req_item table
- Unfortunately, the field takes only dates with this format: YYYY-MM-DD (for example 2023-02-19)
- To overcome it, I created a new variable called "EndDateRearranged"
- I wrote this piece of code to re-arrange the date, but it doesn't work.
// Access the input date Flow variable
var enddate = current.enddate;
// Split the input string by "/"
var dateComponents = enddate.split("/");
// Rearrange the components in the desired order
var enddaterearranged = dateComponents[2] + "-" + dateComponents[1] + "-" + dateComponents[0];
// Set the rearranged date value to an output Flow variable
current.enddaterearranged = enddaterearranged;
Just for clarity, this is how the flow looks like.
In the "Action 2" I set the "EndDate" variable and in "Action 3" I set the EndDateRearranged
Attached is the error trace.
Can you please help?
- Labels:
-
flow designer
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-25-2023 07:27 AM
this is the sample code to convert the format.
Please use it as per your requirement
var gd = new GlideDate();
gd.setDisplayValue(current.enddate, "dd/MM/yyyy");
current.enddaterearranged = gd.getByFormat("yyyy-MM-dd");
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-28-2023 04:28 AM
Apologies for my late reply.
I tried the code in my flow, but it doesn't work. I am not good in coding, so I am probably doing something wrong. However, correct me if I am wrong, with the below line of code, you are telling the flow to work with the current value of the variable "enddate", correct?
gd.setDisplayValue(current.enddate, "dd/MM/yyyy");
Does it also consider the variables which are "Flow Variables"?
Thank you.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-28-2023 06:42 AM
I just enhanced what script you shared.
ensure you pass the correct value for end date in flow script
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-28-2023 06:56 AM
Hi @AEterni,
'current' does not work in flow designer. You need to lookup the output variables of your action.
Start typing fd_data. and continue dotwalking to the right point.
That being said, combining your script and @Ankur Bawiskar's addition you will come up with something like this, excluding the correct paths to your output variables:
var enddate = fd_data. <path> .enddate;
var gd = new GlideDate();
gd.setDisplayValue(enddate, "dd/MM/yyyy");
return gd.getByFormat("yyyy-MM-dd");
Help others to find a correct solution by marking the appropriate response as accepted solution and helpful.