You are currently viewing ServiceNow GlideElement API Functions Explained!!!

ServiceNow GlideElement API Functions Explained!!!

The GlideElement API provides a number of convenient script methods for dealing with fields and their values. GlideElement methods are available for the fields of the current GlideRecord.

How to check users permission to create record?

To do so, we use GlideElement – canCreate(), function which Determines if the user’s role permits the creation of new entries in the associated field.

// The following example shows how to determine if the user has permissions to create
entries for the three most recent records in the Incident table.
var gr = new GlideRecord('incident');
gr.addQuery('state','2');
gr.orderByDesc('sys_created_on');
gr.setLimit(3); 
gr.query();
while(gr.next()){
 if(gr.description.canCreate()){ 
 gs.info("I can create new records for the field incident statement for - " + gr.number);
 }
}

//Output
*** Script: I can create new records for the field incident statement for - INC0010099
*** Script: I can create new records for the field incident statement for - INC0010096
*** Script: I can create new records for the field incident statement for - INC0010079

How to check if logged in user is allowed to read record?

This can be done by using GlideElement – canRead(), which determines whether the user’s role permits them to read the associated GlideRecord.

//The following example shows how to get a list of active Incident records with a readable
Description field
var grIncident = new GlideRecord('incident');
grIncident.addEncodedQuery("active=true"); 
grIncident.orderByDesc('number');
grIncident.setLimit(3); 
grIncident.query();
while (grIncident.next()) {
 if (grIncident.description.canRead()) {  
 gs.info('You have permission to read Descrition of : ' + grIncident.number);
 }
}
//Output
*** Script: You have permission to read Descrition of : INC0010099
*** Script: You have permission to read Descrition of : INC0010096
*** Script: You have permission to read Descrition of : INC0010093

How to check if logged in user is allowed to Write/update record?

This can be done by using GlideElement – canWrite(), which determines whether the user’s role permits them to write the associated GlideRecord

//The following example shows how to get a list of active Incident records with a writeable
Description field
var grIncident = new GlideRecord('incident');
grIncident.addEncodedQuery("active=true");
grIncident.orderByDesc('number');
grIncident.setLimit(3); 
grIncident.query();
while (grIncident.next()) {
 if (grIncident.description.canWrite()) {  
 gs.info('You have permission to write to the description of: ' + grIncident.number);
 }
}
//Output
*** Script: You have permission to write to the description of: INC0010099
*** Script: You have permission to write to the description of: INC0010096
*** Script: You have permission to write to the description of: INC0010093

How to find if current field is modified in servicenow?

This can be done using GlideElement – changes() function in server side script.

  • This is not available for Journal fields types
  • The changes() method is not supported within ACL scripts
  • The function return True if the field has changed, false otherwise
//Piece of code from Business rule checking if assigned to field changes or not.

if (current.assigned_to.changes()) 
{
gs.log("Changed");
}
else
{
gs.log("Not Changed");
}
end footer

Leave a Reply