Copy fields value from one record to another on the same table
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-10-2021 04:53 AM
Hi,
On the User form, there is a manager reference field that reference another user.
When creating (or updating) a user (from service catalog item or directly from the users list) and populate the manager field, i need 2 things :
- Copy some fields values from the manager form to the user form. If the manager change, the fields value change too.
- If fields values are changed on the manager form, it should update all the fields values on the users who have this manager.
I thought about business rules but i don't know how to do this.
Regards,
- Labels:
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-10-2021 04:58 AM
Hi,
try this in after business rule and run for insert and update.
Set the when run condition as field name changes or like all your fields.
var gr = new GlideRecord("sys_user");
gr.addQuery("manager", current.getValue('sys_id'));
gr.query();
while (gr.next()) {
gr.setValue('field_name', current.getValue('field_name'));
// set the remaining values
gr.update();
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-10-2021 05:42 AM
While changing manager fields create after BR and add condition
var grU = new GlideRecord("sys_user");
grU.addQuery("manager", current.getUniqueValue());
grU.addActiveQuery();
grU.query();
while (grU.next()) {
grU.setValue('field1', current.getValue('field1'));
grU.setValue('field2', current.getValue('field2'));
grU.setValue('field3', current.getValue('field3'));
grU.update();
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-10-2021 05:25 AM
Copy some fields values from the manager form to the user form
Option 1. Create before BR on sys_user table to update field while saving record
Option 2: While changing on the form
Create onChange client script on manager field
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
var user=g_form.getReference('manager',setFields);
function setFields(user){
g_form.setValue('field1',user.field1);
g_form.setValue('field12',user.field2);
}
//Type appropriate comment here, and begin script below
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-10-2021 06:33 AM
Thanks, it worked!
One last question,
if i want to apply your script to a catalog client script, how i should proceed ?
It should apply on an catalog item that will be used to create new users on servicenow.
When submitted, it should check for the manager and create the new user with some of the manager's informations fields.
Regards,