Why does assigning answer via a function call not work in Transform Map field mapping script?

monica190
Tera Expert

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:

  1. Why the second approach doesn't work in the Transform Map script context?

  2. Is there a restriction or difference in how ServiceNow handles return values in mapping scripts for sysid mapping?

4 REPLIES 4

Ankur Bawiskar
Tera Patron
Tera Patron

@monica190 

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?

AnkurBawiskar_0-1747919404754.png

 

If my response helped please mark it correct and close the thread so that it benefits future readers.

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

Hi @Ankur Bawiskar 

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

@monica190 

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.

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

@monica190 

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.

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