How to get the previous value of the field in Business rule?

Dinesh Kumar5
Kilo Contributor

We want to update the value of the current field  depends on the value of the previous field .

 

 

 

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

var v = new GlideRecord('u_meeting_booking_details');
var prev1= previous.getvalue("u_string_1");            // unable to get the value using this syntax
gs.addInfoMessage(prev1);
var prev2= previous.getvalue("u_room_status");     // unable to get the value using this syntax
gs.addInfoMessage(prev2);
v.query();


while(v.next())
{

if(current.u_add_room == "volga"){

if(prev1 == "yes")
{
current.setValue("u_string_1","no");
current.setValue("u_room_status","yes");
}

else
{
current.setValue("u_string_1","yes");
current.setValue("u_room_status","no");

}

}
current.update();

}
})(current, previous);

21 REPLIES 21

I corrected it.

 

Dinesh Nikam
Mega Guru

Hello, 

You have written current.update() in business rule.which is not best practice 

And What is the use of var v = new GlideRecord('u_meeting_booking_details'); That you have written

What is your requirement? 

Which type of business rule you have written..?(before,after,async  etc)

I want to update the value. hence i wrote current.update(). and i m write after business rule.

 

Ah so you are actually not after current.update() though after v.update()?

So is it also look you are after the previous value of the u_meeting_booking_details record? Instead of the previous value of current?

Or on what table is your business rule actually running?

If my answer helped you in any way, please then mark it as helpful.

Kind regards,
Mark

---

LinkedIn
Community article list

 

Kind regards,

 

Mark Roethof

Independent ServiceNow Consultant

10x ServiceNow MVP

---

 

~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

LinkedIn

Hello Dinesh,

If you are making changes on the current table then i would suggest go for Before Business rule.

 

And Try Like This

 

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


var prev1= previous.getvalue("u_string_1");           

var prev2= previous.getvalue("u_room_status"); 




if(current.u_add_room == "volga"){

if(prev1 == "yes")
{
current.setValue("u_string_1","no");
current.setValue("u_room_status","yes");
}

else
{
current.setValue("u_string_1","yes");
current.setValue("u_room_status","no");

}



}
})(current, previous);