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

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

you will have to do this dynamically in the transform map script or field map script where you are mapping

answer = (function transformEntry(source) {

    var incomingId = source.u_id; // your import set field
	
	if(incomingId.length < 4){
	
	if(incomingId.length == 1){
	return '000' + source.u_id;
	}
	if(incomingId.length == 2){
	return '00' + source.u_id;
	}
	if(incomingId.length == 3){
	return '0' + source.u_id;
	}
    }
    else{
    return incomingId;
    }	

})(source);

Regards
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader