- Subscribe to RSS Feed
- Mark as New
- Mark as Read
- Bookmark
- Subscribe
- Printer Friendly Page
- Report Inappropriate Content
Today's blog deals with an interesting problem. We know that when we use script to query a table and pass the object back to another function, it is passed by reference. The calling function in this case can use the gliderecord object to manipulate the original gliderecord object and do data updates.
Below is a sample to show this in action.
function getGR() {
var gr = new GlideRecord("incident");
gr.addEncodedQuery("active=true");
gr.setLimit(1);
gr.query();
if (gr.next()) {
return gr;
}
}
var gr = getGR();
gs.info(gr.number);
gr.short_description = gs.nowDateTime();
gr.update();
In the above example, we use the gliderecord object from getGR() and update the short description of the record. While this is fine is in most cases, we may have some scenario when we want the caller of the function not to do such updates and make the object readonly. There can be different ways to implement this requirement, but one of the easiest way is to use an undocumented function of gliderecord class.
Gliderecord class has undocumented function called makeReadonly that you can use to make the gliderecord object readonly so the the caller function cannot make any updates using it. Do note that, system does not throw any error when you try to make an update using readonly object. This can be undesirable in many cases, which I am guessing why the function remains undocumented by ServiceNow. Using makeReadonly is very simple.
function getGR() {
var gr = new GlideRecord("incident");
gr.addEncodedQuery("active=true");
gr.makeReadonly();
gr.setLimit(1);
gr.query();
if (gr.next()) {
return gr;
}
}
var gr = getGR();
gs.info(gr.number);
gr.short_description = gs.nowDateTime();
gr.update();
When the above function is run, the record is not updated.
Bonus: There is a related function isReadonly() to check whether the gliderecord object is readonly or not.
function getGR(readOnly) {
var gr = new GlideRecord("incident");
gr.addEncodedQuery("active=true");
if (readOnly) {
gr.makeReadonly();
}
gr.setLimit(1);
gr.query();
if (gr.next()) {
return gr;
}
}
var gr = getGR(true);
gs.info(gr.number + "-" + gr.isReadonly());
var gr1 = getGR(false);
gs.info(gr1.number + "-" + gr1.isReadonly());
I understand it is not best practice to use undocumented features but I think it is cool that something like this exists. Use these functions at your own risk. And, I hope you found this blog interesting and useful.
NOTE: I was not able to find any reference to this script on community while searching. If someone has already talked about this, I would be happy to link the reference to this blog.
- 1,103 Views
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
