Why doesn't getElement work with GlideAggregate in a Scoped App?

Adam Stout
ServiceNow Employee
ServiceNow Employee

I'm trying to get the display value of a field that I'm passing as a variable.

Here is a sample:

var field = 'assigned_to';
var gr = new GlideAggregate('incident');
gr.groupBy(field);
gr.addNotNullQuery(field);
gr.query();

while(gr.next())
{
  var res = gr.getElement(field);
  gs.addInfoMessage(res.getDisplayValue());
}

In Global, this is fine, but in a scope, I get this:

"JavaException: com.glide.script.fencing.MethodNotAllowedException: Function getElement is not allowed in scope sn_customerservice"

GlideAggregate extends GlideRecord and in the scoped version, GlideRecord supports getElement, so shouldn't this work?

https://developer.servicenow.com/app.do#!/api_doc?v=london&id=r_ScopedGlideRecordGetElement_String

Any ideas for a workaround?

3 REPLIES 3

Adam Stout
ServiceNow Employee
ServiceNow Employee

I'm leaving this open because I don't understand why it doesn't work as-is but I did find a workaround:

var field = 'assigned_to';
var gr = new GlideAggregate('incident');
gr.groupBy(field);
gr.addNotNullQuery(field);
gr.query();

while(gr.next())
{
  var res = gr[field].getDisplayValue();
  gs.addInfoMessage(res);
}

Pooja Mallikarj
Kilo Sage

Hi,

 

getElement(String fieldName) for global application   --->Retrieves the GlideElement for a specified field.

getElement(String column name) for scoped application --->Retrieves the GlideElement object for the specified field.

 

 

GlideRecord  has a getElement() method. This returns an array (if in scope) or an ArrayList (if in global) of all of the elements on the record. You can then loop through the array (in scope) or iterate through it (in global) to get things like the label, field name, and value.

Refer the below example

var gr=new GlideRecord('incident');

gr.addActiveQuery();

gr.query();

gr.next();

var elements=gr.getElement();

if(typeof elements.size !='undefined')

{

// In Global scope ,so iterate

for(var  i=0 ; i<elements.size() ;i++)

{

var ele=elements.get(i);

gs.info("ele.getLabel() , ele.getName() ,gr.getValue(ele.getName()));

}

}

else

{

// For scope ,loop through array

for(var i=0;i<elements.length;i++)

{

var ele=elements[i];

gs.info("ele.getLabel() , ele.getName() ,gr.getValue(ele.getName()));

}

}

     

 

The above code should work for both global and scope.

 

Please mark correct/helpful if it helps for you.

Regards,

Pooja

 

I'm not sure if we are confusing getElement and getElements.  getElement takes a parameter (as you noted) but the example doesn't work as-is (I get the error "TypeError: Cannot read property "size" from null").  When I change it to use getElements it doesn't error (but doesn't really show me what I'm expecting either).