How to translate the date (parsed from the email body) into acceptable format

AysenurU
Tera Contributor

Hello Community

 

I am trying to create an automated standard change record using inbound email content via Flow Designer. 

 

1- I set my trigger as Inbound email and also set the relevant conditions.

2- For the action part, I selected Create Record (Create Change Request Record)

3- I filled in the fields (type, category, description, assignment group, close notes etc.)

 

Even though filling out those fields was pretty straight forward, I have an issue with "actual start date " field. I wanted to take it from email, not to pick a date using the calendar icon. Therefore I created a custom action in flow designer and called it "Parse Date from email". I will also share the script which seems to be working fine. 

 

(function execute(inputs, outputs) {
const emailContent = inputs.emailBody;
const dateRegex = /Started at (.* UTC)/;
const dateMatch = emailContent.match(dateRegex);

if (dateMatch && dateMatch[1]) {
  const actualStartDate = dateMatch[1];
  outputs.actual_start_date = actualStartDate;

} else {
  throw new Error("Actual Start Date not found in email content.");
}
})(inputs, outputs);
 
However, my problem is the date format. In the email, the date format is like "Started at Thu, 02 Nov 2023 08:26:53 UTC". When I look at the actual start date format in change record, it is like this 17-05-2021 15:25:27.  Therefore, this field cannot be populated in the change record that is to be created automatically.
 
How should I fix this? 🙄
 
TIA😇
2 ACCEPTED SOLUTIONS

AnveshKumar M
Tera Sage
Tera Sage

Hi @AysenurU 

 

You can convert this to GlideDateTime object, try the following script in your script step. Also change the output variable type to Date/Time (both script step and action outputs).

 

 

 

(function execute(inputs, outputs) {
const emailContent = inputs.emailBody;
const dateRegex = /Started at (.* UTC)/;
const dateMatch = emailContent.match(dateRegex);

if (dateMatch && dateMatch[1]) {
  const actualStartDate = dateMatch[1];
  var simpleDateFormat = 'E, dd MMM yyyy HH:mm:ss z';
  var gdt = new GlideDateTime();
  gdt.setDisplayValue(actualStartDate,simpleDateFormat);
  
  outputs.actual_start_date = gdt;

} else {
  throw new Error("Actual Start Date not found in email content.");
}
})(inputs, outputs);

 

 

  

AnveshKumarM_0-1699362426852.png

 

 

Please mark my answer helpful and accept as solution if it helped 👍✔️

Thanks,
Anvesh

View solution in original post

@AysenurU Sure, create another output variable at script step and outputs as planned_end_date of Date/Time type and try the below code in same action, 

 

(function execute(inputs, outputs) {
const emailContent = inputs.emailBody;
const dateRegex = /Started at (.* UTC)/;
const dateMatch = emailContent.match(dateRegex);

if (dateMatch && dateMatch[1]) {
  const actualStartDate = dateMatch[1];
  var simpleDateFormat = 'E, dd MMM yyyy HH:mm:ss z';
  var startGdt = new GlideDateTime();
  startGdt.setDisplayValue(actualStartDate,simpleDateFormat);

  var endGdt = new GlideDateTime();
  endGdt.setDisplayValue(actualStartDate,simpleDateFormat);
  endGdt.addDaysUTC(3);
  
  outputs.actual_start_date = startGdt;
  outputs.planned_end_date = endGdt;

} else {
  throw new Error("Actual Start Date not found in email content.");
}
})(inputs, outputs);

 

AnveshKumarM_0-1699409786977.png

Please mark my answer helpful and accept as solution if it helped 👍✔️

Thanks,
Anvesh

View solution in original post

12 REPLIES 12

Hi @AnveshKumar M 

 

It is me again. 😌 Could I ask you one more question regarding my script? Adding 3 more days to the actual start date was pretty straightforward. I thought I would follow a similar way while setting the hour of actual start date to a specific hour, but I constantly failed.  As you may remember, this is the code. 

 

(function execute(inputs, outputs) {
  const emailContent = inputs.emailBody;
  const dateRegex = /Started at (.* UTC)/;
  const dateMatch = emailContent.match(dateRegex);

  if (dateMatch && dateMatch[1]) {
    const actualStartDate = dateMatch[1];
    var simpleDateFormat = 'E, dd MMM yyyy HH:mm:ss z';
    var startGdt = new GlideDateTime();
    startGdt.setDisplayValue(actualStartDate, simpleDateFormat);

    var endGdt = new GlideDateTime();
    endGdt.setDisplayValue(actualStartDate, simpleDateFormat);
    endGdt.addDaysUTC(3);
    
    outputs.actual_start_date = startGdt;
    outputs.planned_end_date = endGdt;
    
  } else {
    throw new Error("Actual Start Date not found in email content.");
  }
})(inputs, outputs); endGdt;
 
As I wanted the hour to be 23:00 specifically, first I added    endGdt.setHours(23);   just below this code endGdt.addDaysUTC(3);   However, it did not work. Then I created a new variable which can be seen below. I was pretty sure that it would work, but I was mistaken. 
 
var endHourGdt = new GlideDateTime();
endHourGdt.setDisplayValue(actualStartDate, simpleDateFormat);
endHourGdt.setHours(23);
 
outputs.planned_end_hour = endHourGdt;
 
Then I tried various variations with no success. So I would like to knock your door again to ask for help. 🙏

Hello @AysenurU 

Sure, Happy to help 😀. Try this code in your script action.

 

(function execute(inputs, outputs) {
const emailContent = inputs.emailBody;
const dateRegex = /Started at (.* UTC)/;
const dateMatch = emailContent.match(dateRegex);

if (dateMatch && dateMatch[1]) {
  const actualStartDate = dateMatch[1];
  var simpleDateFormat = 'E, dd MMM yyyy HH:mm:ss z';
  var startGdt = new GlideDateTime();
  startGdt.setDisplayValue(actualStartDate,simpleDateFormat);

  var endGdt = new GlideDateTime();
  endGdt.setDisplayValue(actualStartDate,simpleDateFormat);
  endGdt.addDaysUTC(3);
  endGdt.setDisplayValue(endGdt.getDate() + " 23:00:00");
  
  outputs.actual_start_date = startGdt;
  outputs.planned_end_date = endGdt;

} else {
  throw new Error("Actual Start Date not found in email content.");
}
})(inputs, outputs);

AnveshKumarM_0-1699761160297.png

 

 

Please mark my answer helpful and accept it as a solution, if it helped 👍

Thanks,
Anvesh

Hi @AnveshKumar M  

 

Thank you very much again. Much appreciated 🙏😇