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

@surajjha 

Remember to Save and Activate the Flow for your testing

Flow:

find_real_file.png

find_real_file.png

find_real_file.png

find_real_file.png

find_real_file.png

Regards
Ankur

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

Hi Ankur,

Thank you for values. I was not having GRC plugin activated on PDI so given close values and added comment asking user to update BR with correct field name and table name.

I have updated it now.

Can you please tell me difference between

gr.description = current.description;

 

AND

gr.setValue('description', current.getValue('description'));

Apart from this line rest you provided same solution as mine. Really interested to understand and learn the difference.

Thank you
Prasad

Hi,

It is best practice and recommended to update related records/tables in an after update BR and not before update BR.

Technical Best Practices

Also please check below link on the detailed explanation on setValue() method. Ideally wherever possible setValue() and getValue() should be used. But people tend to use current.field more often.

Avoiding Pass-By-Reference Using getValue() & setValue()

Regards
Ankur

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

Good to know but not finding where it says current.field is not good or bad practice.

Hi,

Sharing some additional link; this is for getValue() but something similar for setValue()

Is GlideRecord getValue the King of the String

Regards
Ankur

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