Transform map - How to create incident but will not populate reference field based on condition
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-28-2023 10:42 PM
Hello!
I have a transform map that creates an incident record on transform. The source table has a field called config_item and populated the cmdb_ci field on the target table.
I want to create the incident but leave the cmdb_ci field blank if the condition does not meet the condition. The condition is 'sys_class_name=cmdb_ci_server^ORsys_class_name=cmdb_ci_win_server^operational_status=1'.
Is this possible?
Please help!
Thank you.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-28-2023 10:57 PM
then you should use field map script and handle it
Also ensure you set name in Referenced value field name
Also choice action as ignore
answer = (function transformEntry(source) {
// Add your code here
var incomingValue = source.u_config_item; // use the source field name here
var gr = new GlideRecord("cmdb_ci");
gr.addQuery("name", incomingValue); // query with name if incoming value is CI name
gr.addEncodedQuery('sys_class_name=cmdb_ci_server^ORsys_class_name=cmdb_ci_win_server^operational_status=1');
gr.query();
if (gr.next()) {
return gr.name;
}
else{
return -1; // don't allow update
}
})(source);
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
12-28-2023 10:59 PM
Hello @ceraulo ,
You can do this by using a Onbefore transform script as below:
(function runTransformScript(source, map, log, target /*undefined onStart*/ ) {
// Get the sys_class_name and operational_status from the source record
var sysClassName = source.u_config_item.sys_class_name.toString();
var operationalStatus = source.u_config_item.operational_status.toString();
// Check the condition
if ((sysClassName == 'cmdb_ci_server' || sysClassName == 'cmdb_ci_win_server') && operationalStatus == '1') {
// If condition is met, copy the config_item to the target record
target.cmdb_ci = source.u_config_item;
} else {
// If condition is not met, leave the cmdb_ci field blank
target.cmdb_ci = '';
}
})(source, map, log, target);
Kindly mark the answer ✔️Correct or Helpful ✔️If it addresses your concern.
Regards,
Siddhesh