what is the difference between getFields() and getElements()?

Prasanthi1
Giga Contributor

What is the difference between getElements and getFields method.

 

Pls explain with examples...

1 ACCEPTED SOLUTION

Hi @Prashanthi 

getFields() getElements()
getFields is a gliderecord method getElements is also a gliderecord method
getFields() works only in Global application not in scoped getElements works for both scoped applications and global
It returns an array of field names for the specified GlideRecord it returns an array (if in scope) or an ArrayList (if in global) of all of the elements on the record.

 

script examples

getFields:

var queryString = "priority=1^ORpriority=2";
var now_GR = new GlideRecord('incident');
now_GR.addEncodedQuery(queryString);
now_GR.query();
now_GR.next();

var gRU = new GlideRecordUtil();
var fieldList = gRU.getFields(now_GR);
gs.info(fieldList);​

output:

sys_id,skills,closed_by,assigned_to,contract,category,escalation,state,reassignment_count,location,
time_worked,order,due_date,number,upon_approval,sys_tags,sla_due,follow_up,reopen_count,notify,business_stc,
caused_by,rejection_goto,assignment_group,comments_and_work_notes,incident_state,opened_at,parent_incident,
business_service,wf_activity,calendar_duration,group_list,caller_id,comments,priority,sys_updated_by,
variables,delivery_task,resolved_at,sys_updated_on,parent,active,opened_by,expected_start,watch_list,
company,upon_reject,work_notes,sys_created_by,additional_assignee_list,approval_set,cmdb_ci,user_input,
sys_created_on,close_code,contact_type,resolved_by,rfc,approval_history,activity_due,severity,child_incidents,
,subcategory,work_end,closed_at,close_notes,variables,business_duration,knowledge,approval,sys_domain_path,
sys_mod_count,problem_id,calendar_stc,work_start,sys_domain,correlation_id,sys_class_name,short_description,
impact,description,correlation_display,urgency,made_sla,delivery_plan,work_notes_list

 

_______________________________________________________________________

getElements :

simple script for output:

var incident = new GlideRecord("incident");
incident.addQuery('sys_id','INC SYS_ID FOR TESTING');
incident.query();
if(incident.next()){;

var elements = incident.getElements();
gs.print(elements);

} 

output:

you will get field values of that particular record in array

find_real_file.png

Another script with all details:

You can refer this to use for both global application and scoped applications

var incident = new GlideRecord("incident");


incident.addActiveQuery();


incident.setLimit(1);


incident.query();


incident.next();




var elements = incident.getElements();


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


       //we are in global scope, so iterate


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


               var element = elements.get(i);


               gs.info("Element label is {0}, name is {1}, value is {2}", element.getLabel(), element.getName(), incident.getValue(element.getName()));


       }


} else {


       //we are in scope, so loop over the array


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


               var element = elements[i];


               gs.info("Element label is {0}, name is {1}, value is {2}", element.getLabel(), element.getName(), incident.getValue(element.getName()));


       }


}

Hope this helpfuls you understand,if so please mark the relevant answer correct and helpful.

 

BR,

Harika

View solution in original post

7 REPLIES 7

Harika Bhupathi
Giga Guru

Hello @prashanthi 

 

Refer below links which explains clearly the use of getFields() and getElements()

 

https://community.servicenow.com/community?id=community_question&sys_id=65ee3ae5db58dbc01dcaf3231f96...

https://developer.servicenow.com/dev.do#!/reference/api/orlando/server_legacy/c_GlideRecordUtilAPI

 

Please do mark the response correct/helpful, if applicable.

 

BR,

Harika

I did not understand.. Please explain.

 

Thank You

Hi @Prashanthi 

getFields() getElements()
getFields is a gliderecord method getElements is also a gliderecord method
getFields() works only in Global application not in scoped getElements works for both scoped applications and global
It returns an array of field names for the specified GlideRecord it returns an array (if in scope) or an ArrayList (if in global) of all of the elements on the record.

 

script examples

getFields:

var queryString = "priority=1^ORpriority=2";
var now_GR = new GlideRecord('incident');
now_GR.addEncodedQuery(queryString);
now_GR.query();
now_GR.next();

var gRU = new GlideRecordUtil();
var fieldList = gRU.getFields(now_GR);
gs.info(fieldList);​

output:

sys_id,skills,closed_by,assigned_to,contract,category,escalation,state,reassignment_count,location,
time_worked,order,due_date,number,upon_approval,sys_tags,sla_due,follow_up,reopen_count,notify,business_stc,
caused_by,rejection_goto,assignment_group,comments_and_work_notes,incident_state,opened_at,parent_incident,
business_service,wf_activity,calendar_duration,group_list,caller_id,comments,priority,sys_updated_by,
variables,delivery_task,resolved_at,sys_updated_on,parent,active,opened_by,expected_start,watch_list,
company,upon_reject,work_notes,sys_created_by,additional_assignee_list,approval_set,cmdb_ci,user_input,
sys_created_on,close_code,contact_type,resolved_by,rfc,approval_history,activity_due,severity,child_incidents,
,subcategory,work_end,closed_at,close_notes,variables,business_duration,knowledge,approval,sys_domain_path,
sys_mod_count,problem_id,calendar_stc,work_start,sys_domain,correlation_id,sys_class_name,short_description,
impact,description,correlation_display,urgency,made_sla,delivery_plan,work_notes_list

 

_______________________________________________________________________

getElements :

simple script for output:

var incident = new GlideRecord("incident");
incident.addQuery('sys_id','INC SYS_ID FOR TESTING');
incident.query();
if(incident.next()){;

var elements = incident.getElements();
gs.print(elements);

} 

output:

you will get field values of that particular record in array

find_real_file.png

Another script with all details:

You can refer this to use for both global application and scoped applications

var incident = new GlideRecord("incident");


incident.addActiveQuery();


incident.setLimit(1);


incident.query();


incident.next();




var elements = incident.getElements();


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


       //we are in global scope, so iterate


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


               var element = elements.get(i);


               gs.info("Element label is {0}, name is {1}, value is {2}", element.getLabel(), element.getName(), incident.getValue(element.getName()));


       }


} else {


       //we are in scope, so loop over the array


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


               var element = elements[i];


               gs.info("Element label is {0}, name is {1}, value is {2}", element.getLabel(), element.getName(), incident.getValue(element.getName()));


       }


}

Hope this helpfuls you understand,if so please mark the relevant answer correct and helpful.

 

BR,

Harika

Hi @Prashanti 

 

Hope you have got the chance look into the solution I have provided

 

Could you please mark the relevant response as correct/helpful and close this thread from unanswered list

BR,

Harika