Help Needed with Transform Map Script to Prevent Updating Target Field with Blank Source Value

pabadrian
Giga Contributor

I am writing a "Run Script" for a Transform Map in ServiceNow, and I need guidance to implement the following logic:

If the source.u_number field is blank (""), the target field (e.g., number) should remain unchanged.

 

If the source.u_number contains a valid value (non-blank), it should update the target field with that value.


Here is the script I’ve written so far:


answer = (function transformEntry(source) {
if (source.u_number) {
return source.u_number;
} else {
return undefined;
}
})(source);


Problem:
This script is not working as intended. When source.u_number is blank, it is still updating the target field with a blank value. I want the target field to remain unchanged in this case.

 

Could someone help me identify what’s wrong with this script and suggest a fix?

 

Thank you!

2 REPLIES 2

Juhi Poddar
Kilo Patron

Hello @pabadrian 

The issue you're encountering is likely due to how the undefined value behaves in ServiceNow's transform map scripts. When you return undefined, the target field might still be set to a blank value because it's interpreted as no value being returned.

Here is the updated script:

 

(function transformRow(source, target, map, log, isUpdate) {
	// Add your code here
    if (source.u_number && source.u_number.trim() !== "") { //check for empty field and it’s not just a string with spaces
        target.setValue('number', source.u_number);
    }
})(source, target, map, log, action==="update");

 

Hope this helps!

 

"If you found my answer helpful, please like and mark it as an "accepted solution". It helps others find the solution more easily and supports the community!"

 

Thank You
Juhi Poddar

Anand Kumar P
Giga Patron
Giga Patron

Hi @pabadrian ,

Instead of undefined use the target number .

function transformEntry(source) {

    if (source.u_number) {

        return source.u_number;

    } else {

        return target.number;

    }

})(source);

 

If my response helped, please mark it as the accepted solution and give a thumbs up👍.
Thanks,
Anand