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

Thank you Harika for your excellent explanation. It is very helpful for me.

 

Thank You

Prasanthi

Sushma Dhandge
Giga Contributor

Hi @prashanthi 

getElement 

You can access a GlideElement from a server-side GlideRecord object (for example, by using gr. number ), or by using the getElement method of a GlideRecord object. Best practice dictates that you always use the getter and setter functions: getValue() , and setValue() if you want to get the value of the field.

 

getFields(GlideRecord gr)

Returns a list of all the fields in the specified GlideRecord. A GlideRecord instance positioned to a valid record. An array of field names for the specified GlideRecord

Regards

sushma

Here I a asking getElements() not getElement().

 

Thank You