Field Map script help for yyyy-MM-dd'T'HH:mm:ss.SSSZ date

Dan Brown2
Kilo Sage

Hi,

I am trying to get the Created Date for users in AD using LDAP and a transform map.

 

I have added the 'whencreated' attribute to the LDAP attributes list and the date is coming in to the import set table as 20221026133235.0Z.

 

I added a Field Map using the source script of:

answer = (function transformEntry(source) {

var gdt = new GlideDateTime(source.u_whencreated);
return gdt.getValue();

})(source);

 

But it didn't work.

 

The following link: https://www.servicenow.com/docs/bundle/yokohama-api-reference/page/app-store/dev_portal/API_referenc...

Which described what are the allowed date/time formats by the platform. It also mentions specifically that "yyyy-MM-dd'T'HH:mm:ss.SSSZ" is not allowed,

 

Support advised that I would need to add additional code to the Field Map Script, but I can't seem to get it to work.

 

Can anyone help please?

Thank you,

Dan

 

1 ACCEPTED SOLUTION

Cheikh Ahmadou
Tera Guru

Hi Dan,

To convert this value into a format that ServiceNow’s GlideDateTime can accept, i would propose to manually reformat it from yyyyMMddHHmmss.0Z to the expected yyyy-MM-dd HH:mm:ss.

example of function:

function transformEntry(source) {
    var raw = source.u_whencreated + ''; // Ensure it's a string
    if (!raw || raw.length < 14) {
        return '';
    }

    // Extract components
    var year = raw.substring(0, 4);
    var month = raw.substring(4, 6);
    var day = raw.substring(6, 8);
    var hour = raw.substring(8, 10);
    var minute = raw.substring(10, 12);
    var second = raw.substring(12, 14);

    // Reformat into a valid datetime string
    var formatted = year + '-' + month + '-' + day + ' ' + hour + ':' + minute + ':' + second;

    // Create a GlideDateTime from the formatted string
    var gdt = new GlideDateTime(formatted);
    return gdt.getValue(); 
}


 

View solution in original post

3 REPLIES 3

Cheikh Ahmadou
Tera Guru

Hi Dan,

To convert this value into a format that ServiceNow’s GlideDateTime can accept, i would propose to manually reformat it from yyyyMMddHHmmss.0Z to the expected yyyy-MM-dd HH:mm:ss.

example of function:

function transformEntry(source) {
    var raw = source.u_whencreated + ''; // Ensure it's a string
    if (!raw || raw.length < 14) {
        return '';
    }

    // Extract components
    var year = raw.substring(0, 4);
    var month = raw.substring(4, 6);
    var day = raw.substring(6, 8);
    var hour = raw.substring(8, 10);
    var minute = raw.substring(10, 12);
    var second = raw.substring(12, 14);

    // Reformat into a valid datetime string
    var formatted = year + '-' + month + '-' + day + ' ' + hour + ':' + minute + ':' + second;

    // Create a GlideDateTime from the formatted string
    var gdt = new GlideDateTime(formatted);
    return gdt.getValue(); 
}


 

many thanks  Cheikh

Cheikh Ahmadou
Tera Guru

You're welcome