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

URL link in addInfoMessage

var ga=new GlideAjax('gld_HR_ajax'); ga.addParam('sysparm_name', 'checkEmployeeNumber_hrProfile'); ga.addParam('sysparm_hrprofilenumber', g_form.getValue('number')); ga.addParam('sysparm_employeenumber', newValue); ga.getXMLAnswer(function(answer) { if (answer!='undefined' && answer!=''){ var navURL="<a style='text-decoration:underline;color:blue'" + answer + ">" + answer + "</a><img width='3' src='images/s.gif'/>"; var sMsg='The employee number entered already exists on another HR Profile ' + navURL; //alert(sMsg); g_form.showErrorBox('employee_number', 'error - please check'); g_form.addInfoMessage(sMsg); } });

GlideRecord setValue

setValue(String name, Object value) Sets the specified field to the specified value. Normally a script would do a direct assignment, for example,  gr.category = value . However, if in a script the element name is a variable, then  gr.setValue(elementName, value)  can be used. When setting a value, ensure the data type of the field matches the data type of the value you enter. This method cannot be used on journal fields. If the value parameter is null, the record is not updated, and an error is not thrown!/api_doc?v=madrid&id=r_GlideRecord-setValue_String_Object