Script to update existing related records

surajjha
Tera Contributor

Dear leaders ,

Can someone please help me with script for updating related records Description in Risk statement.

below 2 screen shots are GRC risk statement and related list have risks.

We have requirement to update all risk description if risk statement description is changed.

1>

find_real_file.png

 

2>

find_real_file.png

 

Thanks in advance.

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

@surajjha 

you can use these steps

1) Always use after update BR on table "sn_risk_definition" to update records on another table

Condition: Description Changes

Script:

 (function executeRule(current, previous /*null when async*/) {

	// Add your code here
	var gr = new GlideRecord("sn_risk_risk");
	gr.addQuery("statement", current.getUniqueValue());
	gr.query();
	while(gr.next()) {
		gr.setValue('description', current.getValue('description'));
		gr.update();
	}

})(current, previous);

find_real_file.png

Regards
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

13 REPLIES 13

Prasad Pagar
Mega Sage

Hi,

You can write BR on Risk Statement table which should goto Risks table -> Find all records which have Risk statement as current risk statement and then update their desc with current description

Something like

find_real_file.png

Script -

(function executeRule(current, previous /*null when async*/) {

	// Add your code here
	var gr = new GlideRecord('risks'); //Add risk table name here
	gr.addQuery('risk_statement',current.sys_id);//Add correct field name here
	gr.query();
	while(gr.next())
		{
			gr.description = current.description;
			gr.update();
		}

})(current, previous);

find_real_file.png

Make sure you run this BR on Risk Statement table. Also add correct risk table name and risk statement field name

Hope this helps

Thank you
Prasad

Hi @surajjha 

Updated code with exact field names and table. Thank you Ankur for values

Before Update BR as I have mentioned above with below code

(function executeRule(current, previous /*null when async*/) {

	// Add your code here
	var gr = new GlideRecord('sn_risk_risk'); //Add risk table name here
	gr.addQuery('statement',current.sys_id);//Add correct field name here
	gr.query();
	while(gr.next())
		{
			gr.description = current.description;
			gr.update();
		}

})(current, previous);

Thank you
Prasad

Ankur Bawiskar
Tera Patron
Tera Patron

@surajjha 

you can use these steps

1) Always use after update BR on table "sn_risk_definition" to update records on another table

Condition: Description Changes

Script:

 (function executeRule(current, previous /*null when async*/) {

	// Add your code here
	var gr = new GlideRecord("sn_risk_risk");
	gr.addQuery("statement", current.getUniqueValue());
	gr.query();
	while(gr.next()) {
		gr.setValue('description', current.getValue('description'));
		gr.update();
	}

})(current, previous);

find_real_file.png

Regards
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

@surajjha 

You can also use flow designer based approach with no script

Regards
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader