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

Could you explain a bit what the functional case is?

Because, you are performing a GlideRecord query, performing a while, etc.. Though actually, you are updating the current record? Not for example the GlideRecord retrieved records.

Feels like a mix up of logic in your script.

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

I want it for room booking application. I have 3 fields here named as

u_add_room, u_string_1 (availibility), u_room_status......... I want to update availibility and room status value based on previous data of those respected fields.

For Eg: In the attachment if u see the add room is "volga" the previous availability value is "yes" and  room_status value is "no". if the user again selects the add room as "volga" i want to change the current value of availibility as "no" and room status as "yes". But i m unable to do it because i m not able to get the previous value.

 

Performing a GlideRecord query does not give you a previous value. So you are a bit on wrong track here. Also like I mentioned, you are mixing GlideRecord and current.

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

so could u please tell me how to get a previous value?

 

There is no previous value on the current record, because you are only opening the record.

I can imagine you mean a previous value like you can find in the audit (if audit is enabled). Though is that correct to check? Sounds a bit strange to me. Also, what if a other user has the same record open at the same time? How are you handling a conflict like that?

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