While loading data using import set , latest date is not inserting properly in the start date.

Abhilasha G T
Tera Contributor

Hi Team,

 

we are loading a data to  a  a field  name is Contract number  it is a string  field and also  a  a field  name , is Start date t is a date /time field in the ServiceNow target table.

AbhilashaGT_2-1715688098885.png

Data source fields like below CONTRACT_NUMBER and CONTRACT_START_DATE

 

As you noticed the below  same contact number we have different  start dates,

Requirement is, for all the same contact number latest start date is need to update.

AbhilashaGT_0-1715687924443.png

 

we are mapping the CONTRACT_START_DATE to  Start Date in ServiceNow target table.

 

written one onBefore transform script like below,  to fetch the latest date and update in the Start Date field in ServiceNow target table.

 

 function largestElement(arr) {
        var largestNum = arr[0];
        for (var i = 1; i < arr.length; i++) {
            if (arr[i] > largestNum) {
                largestNum = arr[i];
            }
        }
        target.starts=largestNum;
    }

    largestElement(source.u_contract_start_date);

 

 

But this script is not working as expected, values are inserting as it is in the excel sheet.

 

 

 please guide me to correct this, or how to trouble shoot this?

 

 

Regards,

Abhilasha G T

3 REPLIES 3

Sohail Khilji
Kilo Patron
Kilo Patron

Its not clear for me to undersand ! can you explain a bit more @Abhilasha G T 


☑️ Please mark responses as HELPFUL or ACCEPT SOLUTION to assist future users in finding the right solution....

LinkedIn - Lets Connect

Hi Sohail,

 

As you noticed in the below , we have  contact number  for that we have different  start dates,

Requirement is, for all the same contact number latest start date is need to update.

AbhilashaGT_0-1715751892407.png

Regards,

Abhilasha G T

 

Deepak Shaerma
Kilo Sage

Hi @Abhilasha G T 

i think in your script source.u_contract_start_date should be an array if you’re trying to pass it to your largestElement function. Also target.starts should be set to target.start_date (it is my perspection).

 

(function transformEntry(source, target, map, log, isUpdate) {
    function largestElement(arr) {
        var largestNum = arr[0];
        for (var i = 1; i < arr.length; i++) {
            if (arr[i] > largestNum) {
                largestNum = arr[i];
            }
        }
        return largestNum;
    }

    var startDates = source.u_contract_start_date.split(','); //id dates are comma seprated
    var latestDate = largestElement(startDates);
    
    target.u_start_date = latestDate; // Target field is set to the latest date
})(source, target, map, log, isUpdate);