field map script to convert date

BALAJI K R
Tera Expert

Hi,

 

We have a transform map to create records in a table. we are receiving dates as (2022-10-20T10:30:00Z) and country as (Q896 - AT&T GNS - Hungary, QF97 - AT&T GNS - India PVT. LTD, QF97 - AT&T GNS - India PVT. LTD, QUKP - AT&T GNS United Kingdom B.V. (Branch), QF53 - AT&T Global Services Canada Co. ,QF59 - AT&T GNS - France).

 

How to write a field map script to convert these dates to system format and to parse and fetch the country only.

1 REPLY 1

Ratnakar7
Mega Sage
Mega Sage

Hi @BALAJI K R ,

 

You can use JavaScript in the field mapping script. Here's a sample script that demonstrates how to achieve this:

(function transformEntry(source) {
    // Convert date to system format
    var originalDate = source.u_original_date; // Replace 'u_original_date' with the actual source field name
    var systemDate = new GlideDateTime(originalDate);
    var systemDateFormat = systemDate.getDisplayValue(); // Adjust date format as per your requirement

    // Parse and fetch country
    var originalCountry = source.u_original_country; // Replace 'u_original_country' with the actual source field name
    var country = originalCountry.split('-')[1].trim(); // Assuming country name is after the hyphen and trim any leading or trailing spaces

    // Set values in target fields
    source.u_system_date = systemDateFormat; // Replace 'u_system_date' with the actual target field name for system date
    source.u_country = country; // Replace 'u_country' with the actual target field name for country

})(source);

Ensure to replace 'u_original_date' and 'u_original_country' with the actual names of the fields containing the original date and country respectively. Similarly, replace 'u_system_date' and 'u_country' with the names of the target fields where you want to store the converted date and extracted country respectively.

This script assumes that the date field is of type GlideDateTime in the ServiceNow instance. Adjust the date format according to your requirements. Additionally, ensure that the country names are consistently formatted in the source field for proper parsing.

 

Thanks,

Ratnakar