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

Mark Roethof
Tera Patron
Tera Patron

Hi there,

If just interested in a previous value, just use something like:

previous.getValue("u_string_1")

There's no need to use "getElement".

Note: previous does not work on async business rules.

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

Dinesh Kumar5
Kilo Contributor

 Mark, we tried with that syntax also but its not working.

Hi Dinesh,

try using below updated code

I have just used previous.u_string_1 and previous.u_room_status

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

var v = new GlideRecord('u_meeting_booking_details');
var prev1 = previous.u_string_1;           
gs.addInfoMessage(prev1);
var prev2 = previous.u_room_status;
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);

Mark Correct if this solves your issue and also mark 👍 Helpful if you find my response worthy based on the impact.
Thanks
Ankur

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

Ankur, Its still not working. Infomessage still does not diplays the previous value. It is still empty.