Welcome to Community Week 2025! Join us to learn, connect, and be recognized as we celebrate the spirit of Community and the power of AI. Get the details  

How to re-arrange a string variable in Flow Designer

AEterni
Mega Guru

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

 

AEterni_0-1692971469454.png

Attached is the error trace.

 

Can you please help?

6 REPLIES 6

Ankur Bawiskar
Tera Patron
Tera Patron

@AEterni 

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.

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

@Ankur Bawiskar 

 

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.

@AEterni 

I just enhanced what script you shared.

ensure you pass the correct value for end date in flow script

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

Peter Bodelier
Giga Sage

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.