- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-29-2025 08:14 AM
Please advise the transform map script, If computer exists in both source and target table, update these fields in CMDB:
Update the field: Computer exists = yes
last login date.
If computer exists only source but does not exist in target (CMDB):
No action
If computer does not exist in source, but exists in CMDB, update this field in CMDB.
Computer exists in AD = no. Thanks.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-29-2025 08:29 PM
Hi @Deepa12 ,
Scenario Overview
- Source Table: Data coming from the source (e.g., Active Directory, discovery tool).
- Target Table: CMDB (e.g., cmdb_ci_computer).
- Logic:
- If the computer exists in both source and target:
- Update Computer exists = Yes.
- Update the Last login date.
- If the computer exists only in the source:
- No action.
- If the computer exists only in the CMDB:
- Update Computer exists in AD = No.
- If the computer exists in both source and target:
try this transformation script
(function executeMapScript(source, target, map, log, isUpdate) {
// Check if the computer exists in the target table (CMDB)
var cmdbRecord = new GlideRecord('cmdb_ci_computer'); // Replace with the correct table name
cmdbRecord.addQuery('name', source.name); // Replace 'name' with the unique identifier field
cmdbRecord.query();
if (cmdbRecord.next()) {
// Computer exists in both source and target (CMDB)
target.u_computer_exists = 'Yes'; // Update the "Computer exists" field
target.u_last_login_date = source.u_last_login_date; // Update "Last login date" from source field
} else {
// Computer does not exist in the target (CMDB), no action
return;
}
// Check if the computer does not exist in the source but exists in CMDB
var sourceExists = false;
var sourceCheck = new GlideRecord('source_table'); // Replace with your source table name
sourceCheck.addQuery('name', cmdbRecord.name); // Match on unique field
sourceCheck.query();
if (!sourceCheck.hasNext()) {
// Computer exists in CMDB but not in source
target.u_computer_exists_in_ad = 'No'; // Update "Computer exists in AD"
}
})();
Source Table Query:
- Ensures you compare records between source and target based on a unique field (e.g., name, serial_number).
Target Table Query:
- Uses a GlideRecord query to determine if the computer exists in the CMDB.
Field Updates:
- Updates fields such as u_computer_exists, u_last_login_date, and u_computer_exists_in_ad only if the required conditions are met.
No Action:
- For cases where the computer exists only in the source, the script exits early with no updates.
Field Names: Update field names (e.g., name, u_computer_exists, u_last_login_date, u_computer_exists_in_ad) as per your table schema.
Table Names:
- Replace cmdb_ci_computer with your CMDB table name.
- Replace source_table with your actual source table name.
Transform Map Execution Timing:
- Use onBefore to prevent unnecessary writes to the target table.
- Use onAfter if updates should only occur after the record is inserted.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-03-2025 11:47 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