Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

how to update field value in server script?

Priti18
Tera Expert

hi members,

I have one br on incident table where i have custom fields in which am trying to update one of them below is the eg:

var fieldname1 = "hello world";

current.variables.fieldname2 = fieldname1;

current.update()

but the above code its not doing anything but i can see in logs if i try to print above value

 

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

Few things to inform/check

1) Avoid current.update() in Business rule as it would lead to recursion and is not recommended

2) Also what you want to update? Is it a variable on the incident form or a field

Please use after insert/update BR and sample script below

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

    // Add your code here

var fieldname1 = "hello world";

var rec = new GlideRecord('incident');

rec.get(current.sys_id);

// if you want to update some variable; give proper variable name
rec.variables.fieldname2 = fieldname1; 

rec.update();

})(current, previous);

Regards
Ankur

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

View solution in original post

2 REPLIES 2

Onkar Pandav
Tera Guru

Hi,

It is not best practice to write "current.update()" method in Business rule (Server code).

You can glide a table then update.

For example:

var gr=new GlideRecord(<tablename>);  //Creating object to the table(just like creating a link to the table)

gr.addquery("<fieldname>","<fieldvalue>"); // the condition to pick only selected rows

gr.query(); //Execute the command

while(gr.next()) //this will go through each of the records which are retrieved by the query

{

gr.<fieldname>="<value>"; // you can update any field value of each of the record here

gr.update(); // this will update the field value of the particular record.

}

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

Few things to inform/check

1) Avoid current.update() in Business rule as it would lead to recursion and is not recommended

2) Also what you want to update? Is it a variable on the incident form or a field

Please use after insert/update BR and sample script below

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

    // Add your code here

var fieldname1 = "hello world";

var rec = new GlideRecord('incident');

rec.get(current.sys_id);

// if you want to update some variable; give proper variable name
rec.variables.fieldname2 = fieldname1; 

rec.update();

})(current, previous);

Regards
Ankur

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