Copy fields value from one record to another on the same table

Nico12
Mega Sage

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,

6 REPLIES 6

BALAJI40
Mega Sage

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();
}

While changing manager fields create after BR and add condition

find_real_file.png

find_real_file.png

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();
}

 

Upender Kumar
Mega Sage

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

find_real_file.png

 

find_real_file.png

find_real_file.png

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

}

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,