Skip to main content

Dates causing me issues in the JSON...how 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:

sDate=email.body.last_working_date;

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!

OUCH!!



Solution:

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) {
    sDate=email.body.last_working_date;
}
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
cart.get();
var item =
        {
            'sysparm_id': catItemID.toString(),
            'sysparm_quantity': '1',
            'sysparm_requested_for': userSYSID.toString,
            'variables':{
                '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]
l_date_numeric_DT.setNumericValue(current.variables.leaver_date_numeric);

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

Result: on the RITM:



Comments

Popular posts from this blog

ServiceNow check for null or nil or empty (or not)

Haven't tested these all recently within global/local scopes, so feel free to have a play! option 1 use an encoded query embedded in the GlideRecord , e.g.  var grProf = new GlideRecord ( 'x_cls_clear_skye_i_profile' ); grProf . addQuery ( 'status=1^ owner=NULL ' ); grProf . query (); even better use the glideRecord  addNotNullQuery or addNullQuery option 2 JSUtil.nil / notNil (this might be the most powerful. See this link ) example: if ( current . operation () == 'insert' && JSUtil . notNil ( current . parent ) && ! current . work_effort . nil ())  option 3 there might be times when you need to get inside the GlideRecord and perform the check there, for example if the code goes down 2 optional routes depending on null / not null can use gs.nil : var grAppr = new GlideRecord ( 'sysapproval_approver' ); var grUser = new GlideRecord ( 'sys_user' ); if ( grUser . get ( 'sys_id' , current . approver )){...

Code a pause/wait - gs.sleep or gs.wait alternative, pause script for specified seconds (timer)

Code a pause/wait - gs.sleep / gs.wait alternative, pause script for specified seconds (timer)  e.g. 10 seconds: do_sleep ( 10000 ); function do_sleep ( milliseconds ) { var start = new Date (). getTime (); for ( var i = 0 ; i < 1e7 ; i ++) { if (( new Date (). getTime () - start ) > milliseconds ){ gs . print ( 'waking up!' ); break ; } } }