Why does assigning answer via a function call not work in Transform Map field mapping script?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-22-2025 06:05 AM
Hi all,
I'm working with a Transform Map where sys_id is set as the coalesce field. I have a field mapping script enabled with source scripting turned on. I'm trying to assign a value to the target field based on a GlideRecord lookup.
When I use the following code, it works perfectly:
var modelNumber = source.u_model_id;
var assetGR = new GlideRecord('alm_hardware');
assetGR.addEncodedQuery('po_numberSTARTSWITH' + source.u_po_number + '^model.model_numberSTARTSWITH' + modelNumber + '^install_status=2');
assetGR.query();
if (assetGR.next()) {
answer = assetGR.sys_id;
} else {
answer = -1;
}
However, when I try to use,like this, it doesn’t seem to work:
answer = (function transformEntry(source) {
var modelNumber = source.u_model_id;
var assetGR = new GlideRecord('alm_hardware');
assetGR.addEncodedQuery('po_numberSTARTSWITH' + source.u_po_number + '^model.model_numberSTARTSWITH' + modelNumber + '^install_status=2');
assetGR.query();
if (assetGR.next()) {
return assetGR.sys_id;
} else {
return -1;
}
})(source);
Could someone help me understand:
Why the second approach doesn't work in the Transform Map script context?
Is there a restriction or difference in how ServiceNow handles return values in mapping scripts for sysid mapping?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-22-2025 06:10 AM
the 2nd syntax should work as that's the default syntax which comes when you enable script in field map
Did you try the 2nd approach for some other normal field which is not marked as Coalesce?
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-22-2025 06:52 AM
It's working fine for other fields, but not behaving as expected for the sys_id. The expected behavior is that if a record with the given sys_id is found, it should be updated, otherwise, a new record should be created
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-22-2025 08:07 AM
Seems it's not supported then and for coalesce field which is on sysId it requires normal syntax
Try raising a case with ServiceNow to confirm it once.
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-22-2025 11:46 PM
Thank you for marking my response as helpful.
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader