Can i change the source field Value using onBefore Transform Script?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
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();
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
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! 🙏
Ankur
✨ Certified Technical Architect || ✨ 10x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
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
