- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-03-2017 11:03 AM
Hi All,
I am trying to populate the department and location values on CIs dynamically based on Assigned to field on the CI. While creating the CI if the CI is assigned to an user the department and location field will be populated based on the Assigned to user.
Once the CI is saved and if the user's department and location is changed on the User record, i need to update the CIs to have the updated department and location from the User record.
I have created a scheduled job which is working as required once the department and location field are empty on the CI. But this is not working if the department and location are already filled in to the CI.
How can I modify the script that even though the department and location are filled in. it has to scan the assigned to user records and update the department and location field on CI?
Here is my script :
var ci = new GlideRecord('cmdb_ci');
ci.addQuery('department', '');
ci.addQuery('location', '');
ci.query();
while(ci.next()) {
ci.department = ci.assigned_to.department;
ci.location = ci.assigned_to.location;
ci.update();
}
Thanks,
SD
Solved! Go to Solution.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-03-2017 11:17 AM
If you want to make it scan all records, take out the two addQuery() lines. That's what's filtering it to say "Just look for records where department and location are empty."
var ci = new GlideRecord('cmdb_ci');
// ci.addQuery('department', '');
// ci.addQuery('location', '');
ci.query();
while(ci.next()) {
ci.department = ci.assigned_to.department;
ci.location = ci.assigned_to.location;
ci.update();
}

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-03-2017 11:17 AM
If you want to make it scan all records, take out the two addQuery() lines. That's what's filtering it to say "Just look for records where department and location are empty."
var ci = new GlideRecord('cmdb_ci');
// ci.addQuery('department', '');
// ci.addQuery('location', '');
ci.query();
while(ci.next()) {
ci.department = ci.assigned_to.department;
ci.location = ci.assigned_to.location;
ci.update();
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-03-2017 11:27 AM
Thanks chuck.
That worked perfectly.