Welcome to Community Week 2025! Join us to learn, connect, and be recognized as we celebrate the spirit of Community and the power of AI. Get the details  

During import set leading zeros in string field - Transformmap

Peda
Mega Expert

Hi everyone,

I have this Transformmap to map departments into the ootb table cmn_department.

find_real_file.png

I want to  achieve that during import, the target field "id" will filled with leading zeros -> field "string length < 4". I.e. "2" becomes "0002" or "100" becomes "0100".

Thanks for your help.

1 ACCEPTED SOLUTION

P-Rudenko-SN
ServiceNow Employee
ServiceNow Employee

Sorry, now better got your requirement. Try this one:

answer = (function transformEntry(source) {
          
var length = source.u_id.toString().length;
if(length>4) return source.u_id.substring(0,4); //ensure the field is 4-character length
else if(length == 3) return '0' + source.u_id;
else if(length == 2) return '00' + source.u_id;
else if(length == 1) return '000' + source.u_id; 

})(source);

View solution in original post

5 REPLIES 5

P-Rudenko-SN
ServiceNow Employee
ServiceNow Employee

Use a following source script for mapping target 'id' field:

answer = (function transformEntry(source) {

	return '000' + source.u_id; // return the value to be put into the target field

})(source);

 

@Pavlo Rudenko thanks for reply,

so with your solution I need to set the string lenght of the field to 4 to achieve my requirement?

find_real_file.png

Whats happen with your solution when i import a id with 999? I get only one zero with your solution? Thanks for help.

 

P-Rudenko-SN
ServiceNow Employee
ServiceNow Employee

Sorry, now better got your requirement. Try this one:

answer = (function transformEntry(source) {
          
var length = source.u_id.toString().length;
if(length>4) return source.u_id.substring(0,4); //ensure the field is 4-character length
else if(length == 3) return '0' + source.u_id;
else if(length == 2) return '00' + source.u_id;
else if(length == 1) return '000' + source.u_id; 

})(source);

@Peda have you had a chance to check the code? Please let me know if it works for you.