Help with date input in inbound script to create a CR

Mollie V
Tera Guru

Hello Dev community,

I'm developing an inbound action to create a CR from an email. But I'm having trouble with the planned start and end dates.

The email is in html format but I'm using email.body_text to get the values I need as input to the CR record.  I can extract the planned start and end dates with no problem but when I assign them to the CR planned start and end, they are not the right values.

 

Here is part of the script that I need your help with. The input to GlideDateTime() is exactly how the datetime appears in the email. But when I ran this script, the getDisplayValue() returned is not what I expected.

 

var gd = new GlideDateTime("7/11/2023 6:30 PM");

gs.print('getDisplayValue ' + gd.getDisplayValue());
gs.print('toString ' + gd.toString());
gs.print('getValue ' + gd.getValue());
gs.print('getDisplayValueInternal ' + gd.getDisplayValueInternal());

gs.print(gd.isValid()); //true
gs.print(gd.getErrorMsg()); //reason

 

*** Script: getDisplayValue 10-Jul-2023 19:00:00
*** Script: toString 2023-07-11 00:00:00
*** Script: getValue 2023-07-11 00:00:00
*** Script: getDisplayValueInternal 2023-07-10 19:00:00
*** Script: true
*** Script: null 

 Does anyone have any suggestions on what I'm missing? 

 

Thanks in advance for the help!

3 ACCEPTED SOLUTIONS

Mollie V
Tera Guru

Hi all, thanks again for the ideas. After trying out various methods, I got it working. Here's the final script in case you're interested and hopefully it helps others.

 

var planStart = "07/11/2023 9:30 PM";

var dStart = planStart.trim().split(" ");

gs.info(dStart[0]);

gs.info(dStart[0].replaceAll("/","-"));

var tStart = dStart[1]+' '+ dStart[2];

var newDt = dStart[0].replaceAll("/","-")

var newDt2 = newDt.trim().split("-");

var newDt3 = newDt2[0]+ '-' +newDt2[1]+'-' +newDt2[2];

var refDt = newDt3 + ' ' + tStart; 

gs.info ('Plan Start reformatted ' + refDt);

 

var gd = new GlideDateTime();

gd.setValue(refDt);

gs.print('getValue: '+ gd.getValue());

 

//this is where we check if AM or PM

var pstr = planStart.toString();

var adjustedTime = gd.getValue();

if (pstr.indexOf('PM') > 0) {

  gd.addSeconds(43200); // add 12 hours

  gs.print('getValue after adding seconds: '+ gd.getValue());

  adjustedTime = gd.getValue();

}

  gs.print('adjusted time: '+ adjustedTime);

View solution in original post

Tushar
Kilo Sage
Kilo Sage

Hi @Mollie V 

 

This should help -

var planStart = "07/11/2023 9:30 PM";

// Split date and time
var dStart = planStart.trim().split(" ");
var datePart = dStart[0].replaceAll("/", "-");
var timePart = dStart[1] + " " + dStart[2];

// Combine date and time in the right format
var refDt = datePart + " " + timePart;

// Create a new GlideDateTime object and set its value
var gd = new GlideDateTime();
gd.setValue(refDt);

// If PM, add 12 hours (43200 seconds)
if (planStart.indexOf('PM') > 0) {
  gd.addSeconds(43200); // 12 hours in seconds
}

// Display the final adjusted time
gs.info('Adjusted time: ' + gd.getDisplayValue());

 

 

Please, don't forget to mark my answer as correct if it solves your issue or mark it as helpful if it is relevant for you!

Regards,
Tushar

View solution in original post

Thanks, Tushar, for your help. However, getDisplayValue() doesn't return the correct adjusted time. But using getValue() does. But your script works mostly and appears cleaner.

*** Script: Formatted Date: 07-11-2023 9:30 PM
*** Script: Adjusted time getValue(): 2023-07-11 21:30:00**** correct
*** Script: Adjusted time getDisplayValue(): 11-Jul-2023 16:30:00**** incorrect

 

View solution in original post

7 REPLIES 7

Mollie V
Tera Guru

Hi all, thanks again for the ideas. After trying out various methods, I got it working. Here's the final script in case you're interested and hopefully it helps others.

 

var planStart = "07/11/2023 9:30 PM";

var dStart = planStart.trim().split(" ");

gs.info(dStart[0]);

gs.info(dStart[0].replaceAll("/","-"));

var tStart = dStart[1]+' '+ dStart[2];

var newDt = dStart[0].replaceAll("/","-")

var newDt2 = newDt.trim().split("-");

var newDt3 = newDt2[0]+ '-' +newDt2[1]+'-' +newDt2[2];

var refDt = newDt3 + ' ' + tStart; 

gs.info ('Plan Start reformatted ' + refDt);

 

var gd = new GlideDateTime();

gd.setValue(refDt);

gs.print('getValue: '+ gd.getValue());

 

//this is where we check if AM or PM

var pstr = planStart.toString();

var adjustedTime = gd.getValue();

if (pstr.indexOf('PM') > 0) {

  gd.addSeconds(43200); // add 12 hours

  gs.print('getValue after adding seconds: '+ gd.getValue());

  adjustedTime = gd.getValue();

}

  gs.print('adjusted time: '+ adjustedTime);

Tushar
Kilo Sage
Kilo Sage

Hi @Mollie V 

 

This should help -

var planStart = "07/11/2023 9:30 PM";

// Split date and time
var dStart = planStart.trim().split(" ");
var datePart = dStart[0].replaceAll("/", "-");
var timePart = dStart[1] + " " + dStart[2];

// Combine date and time in the right format
var refDt = datePart + " " + timePart;

// Create a new GlideDateTime object and set its value
var gd = new GlideDateTime();
gd.setValue(refDt);

// If PM, add 12 hours (43200 seconds)
if (planStart.indexOf('PM') > 0) {
  gd.addSeconds(43200); // 12 hours in seconds
}

// Display the final adjusted time
gs.info('Adjusted time: ' + gd.getDisplayValue());

 

 

Please, don't forget to mark my answer as correct if it solves your issue or mark it as helpful if it is relevant for you!

Regards,
Tushar

Thanks, Tushar, for your help. However, getDisplayValue() doesn't return the correct adjusted time. But using getValue() does. But your script works mostly and appears cleaner.

*** Script: Formatted Date: 07-11-2023 9:30 PM
*** Script: Adjusted time getValue(): 2023-07-11 21:30:00**** correct
*** Script: Adjusted time getDisplayValue(): 11-Jul-2023 16:30:00**** incorrect