Skip to main content

Dates causing me issues in the I overcame it

Houston, we have a problem...

scenario: we are invoking the cart object to create a service request off the back of an inbound email, as part of a leaver process for our customer.

The leaver date comes in via the email in format:

     Last Working Date: 22/03/2018 (british format: dd/mm/yyyy)

this is then grabbed in the email inbound action code using the following:


And in this case needs to be mapped to both a catalog variable (date/time) and a custom field on the target RITM table (date/time)

Problem: I noticed my JSON was causing the cart.addToCart(item)
to crash, and it was all down to the date...the JSON just did not like it!



1) manipulate the date:

var interim_date;
var day;
var month;
var year;
var l_date=new GlideDateTime();
var sDate=gs.nowDateTime();
if(email.body.last_working_date != undefined) {
interim_date = sDate;
var day = interim_date.substring(0,2);
var month = interim_date.substring(3,5);

var year = interim_date.substring(6,10);

2) copy the date into a GlideDate object that matches the format it is stored on date/time fields in the local instance, and then ultimately map it into a catalog variable of type string via the JSON:

var l_date2=new GlideDateTime(year+'-'+month+'-'+day+'-'+' 00:00:00');//--"2018/02/20 00:00:00";
var l_date2_num=l_date2.getNumericValue().toString(); //--use this as it's JSON-friendly,

3) copy the date into a GlideDate object, and then ultimately into a catalog variable of type string via the JSON - "check out the catalog item from the cart" - programatically, done like this:

var cart = new sn_sc.CartJS();

var catItemID = '678e49fd378a860049ced69543990e0a'; //-- Leaver catalog item
var item =
            'sysparm_id': catItemID.toString(),
            'sysparm_quantity': '1',
            'sysparm_requested_for': userSYSID.toString,
                'u_employee_id': emp_id.toString(),
                'leaver': userSYSID.toString(),
                'requested_for': userSYSID.toString(),
                'email_body': email.body_html.toString(),
                'leaver_date_numeric': l_date2_num
var cartDetails = cart.addToCart(item);
var requestDetails = cart.submitOrder(item);
//var jsonAsString = JSON.stringify(cartDetails);
//gs.log('4: jsonAsString: ' + jsonAsString,'inbEmAct:LeaverProc');

4) in the RITM workflow, map this numeric value back to the variable/field in date format:

'Run script' snippet:

var l_date_numeric_DT=new GlideDateTime(); //--the leaver date pulled in from the email, stored as numeric value in a variable on the request [prevents the JSON from crashing]

var leavingDate= l_date_numeric_DT.getValue(); //--convert it back to a date

Result: on the RITM:


Popular posts from this blog

Get URL Parameter - server side script (portal or classic UI)

Classic UI : var sURL_editparam = gs . action . getGlideURI (). getMap (). get ( ' sysparm_aparameter ' ); if ( sURL_editparam == 'true' ) { gs . addInfoMessage ( 'parameter passed ); } Portal : var sURL_editparam = $sp . getParameter ( " sysparm_aparameter " ); if ( sURL_editparam == 'true' ) { gs . addInfoMessage ( 'parameter passed ); }

ServiceNow - script include: build up the results as a JSON object (array) - example 2

function call var sCurrentGroupMembers = new <script_include> (). getGroupMembers ( group_sysid ); gs . print ( 'sCurrentGroupMembers:' + sCurrentGroupMembers ); var oGroupMembers = JSON . parse ( sCurrentGroupMembers ); gs . print ( JSON . stringify ( oGroupMembers . users )); oNewGroupMember = JSON . parse ( group_members ); var oGroupConfig = getGroupDeltas ( oGroupMembers . users , oNewGroupMember ); gs . print ( JSON . stringify ( oGroupConfig . remove )); gs . print ( JSON . stringify ( oGroupConfig . add )) output : *** Script: sCurrentGroupMembers: {"manager":"3b2649efdb0f8c10cc0652f3f39xxxxx", "email":"", "type":"33a2226edb99c340edfc7cbdae96xxxx", "description":"Created for xyz", "users":[{"user":"3b2649efdb0f8c10cc0652f3f396xxxx"},{"user":"856a8f71db3d73041b4ffc45ae96196a"},{"user":"3752771