Script to change date format from email

Richard Thomas2
Tera Contributor

Hi all,

Is it possible to change the date format from:

8 September 2020

to

08/09/2020 - via a script? would then need to populate a date in a request item...

Thanks for your help

 

Regards

 

Rich

1 ACCEPTED SOLUTION

Hi,

Try like this.

var termination_date = email.body.termination_date; 
termination_date = termination_date.split(" ");
var month = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul","Aug", "Sep", "Oct", "Nov", "Dec"];
//get the month
var mon = month.indexOf(termination_date[1].toString());
mon++;
var gdt1= new GlideDateTime();
gs.info("Date format is "+gdt1);
var dt = termination_date[2]+"-"+mon+"-"+termination_date[0]; //change to your system format here
var gdt = new GlideDateTime(dt);
//Now gdt contains the date in the SN format.

View solution in original post

13 REPLIES 13

Ah okay, understood.

you can read the termination date like this

var termination_date = email.body.termination_date;
var gdt = new GlideDate();
gdt.setDisplayValue(termination_date);
gs.print(gdt);
//now gdt stores the date in the required format
current.termination_date=gdt; //assign to RITM field liek this

Let me know if you already have code to create the RITM in the inbound action.

 

Is the format fixed like dd MMM YYYY? or do you think it can be different

Sep or September?

Thanks for your reply. 

It's always fixed as 3 letters (ie Sep). This is my script for creating a RITM. It works and creates the Request Item but the email says " did not create or update sc_req_item using current" It does work though: see below.

 

createRequest();

function createRequest() {
var cart = new Cart();
// add in cart
var item = cart.addItem('3b16e56adbe0b700a0cbf3571d9619d4');
// set requested for
cart.setVariable(item, 'requester', gs.getUserID());
cart.setVariable(item, 'requested_for', gs.getUserID());
cart.setVariable(item, 'short_description', email.subject.toString());
cart.setVariable(item, 'short_description', email.subject.toString());
cart.setVariable(item, 'email', gs.getUser().getEmail());
cart.setVariable(item, 'department', ('93b25282c0a8000b0b55c8ab34e2f1e6'));
cart.setVariable(item, 'contact_number', (12345));
cart.setVariable(item, 'justification', ('Leaver'));


var rc = cart.placeOrder(); //this launches the catalog item, and creates a request object. rc = the request object

updateRITM(rc.sys_id); //call a function immediately to update the ritm. This must be a nested function, otherwise inbound actions get weird.

//also, we're passing the sys_id of the request so we know what RITM to grab.

}

function updateRITM(req){

var ritm = new GlideRecord('sc_req_item');

ritm.addQuery('request', req); //req is what we passed from the previous function. the sys_id of the request.

ritm.query();

while (ritm.next()){

ritm.requested_for = gs.getUserID(); //my ritm table separately tracks its own customer, since I don't use Request

ritm.u_description_html = email.body_text; //we're still in the inbound action so why not exploit?

ritm.state = '1';

ritm.update();

}

if (current.getTableName() == "sc_req_item") {

var createTsk = new GlideRecord('sc_task');
createTsk.initialize();
createTsk.short_description = email.subject;
createTsk.description = email.body_text;
createTsk.request_item = current.sys_id; // It will add task under your RITM
createTsk.insert();

}

Hi,

Try like this.

var termination_date = email.body.termination_date; 
termination_date = termination_date.split(" ");
var month = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul","Aug", "Sep", "Oct", "Nov", "Dec"];
//get the month
var mon = month.indexOf(termination_date[1].toString());
mon++;
var gdt1= new GlideDateTime();
gs.info("Date format is "+gdt1);
var dt = termination_date[2]+"-"+mon+"-"+termination_date[0]; //change to your system format here
var gdt = new GlideDateTime(dt);
//Now gdt contains the date in the SN format.

Hello Richard,

Do you have any update on this? If the issue is resolved, kindly mark the comment as a correct answer so that the question is moved to the solved list.