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

Running transform maps asynchronously

Post a command to the ECC queue for the mid server to initiate a powershell file copy

GlideRecord setValue