Can i change the source field Value using onBefore Transform Script?

RamjiP611320747
Tera Contributor

1. I have a scenario where i have to remove field value which contains "not availabe" to transformed as empty, source script for every field could not be the best scenario.

2. i wanted to change the source field itself from "not available" to "", can i do this using onBefore Transform Script.

function removeNotAvailable() {
        var naLookup = {
            'NA': true,
            'N/A': true,
            'NOT AVAILABLE': true
        };
        var sourceGr = new GlideRecord("u_test_supplier_data");
        sourceGr.get(source.sys_id);
        gs.info("Updated source field: "+ source.sys_id)
 
        var value = (source.u_diversity_status_temporary).trim().toUpperCase();
       
 
        if (naLookup[value]) {
            gs.info("FOUND THE NOT AVAIALBLE: " + value)
            sourceGr.u_diversity_status_temporary = "hello";
            sourceGr.update();  
            gs.info("Updated source field: "+ sourceGr.u_diversity_status_temporary)
        }
    }
 
    removeNotAvailable();
7 REPLIES 7

Ankur Bawiskar
Tera Patron

@RamjiP611320747 

not possible in onbefore script to manipulate source data

why not use onBefore transform script to handle your logic based on what's there in source field value?

💡 If my response helped, please mark it as correct ✅ and close the thread 🔒— this helps future readers find the solution faster! 🙏

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

RamjiP611320747
Tera Contributor

why not use onBefore transform script to handle your logic based on what's there in source field value?


i am already using the onBefore transform script to manipulate data, could you be more descriptive here

yashkamde
Giga Sage

Hello @RamjiP611320747 ,

 

Updating the staging record is not recommendable, but you can change the values in memory during onBefore. source is the row being transformed, and field maps read from it after onBefore runs.

 

The issue might be bcz it queries a second GlideRecord (sourceGr) and updates that, while the field map reads from the original source object.

 

Try this script which loop over all the source fields and blank the matching values directly on source. This needs no per-field source scripts and no update().

(function runTransformScript(source, map, log, target /*undefined onStart*/ ) {

    var NA = { 'NA': true, 'N/A': true, 'NOT AVAILABLE': true };

    // getFields() = global scope, getElements() = scoped
    var fields = (typeof source.getFields === 'function') ? source.getFields() : source.getElements();
    var len = fields.size ? fields.size() : fields.length;

    for (var i = 0; i < len; i++) {
        var el = fields.size ? fields.get(i) : fields[i];
        var name = el.getName();

        if (name.indexOf('u_') !== 0) continue; // only import set columns, skip sys_* fields

        var val = source.getValue(name);
        if (val && NA[String(val).trim().toUpperCase()]) {
            source.setValue(name, '');
        }
    }

})(source, map, log, target);

 

If my response helped mark as helpful and accept the solution.

RamjiP611320747
Tera Contributor

Hi @Yash9  thanks for solution, earlier i have tried the approach to manipulate the source object but still i was failing, i have tried your script as well but again the same result 

RamjiP611320747_0-1790578085756.png