Service Portal - Custom List - Display Field Label NOT Field Name

phil34
Tera Expert

Hi ServiceNow Masters,

 

I have a custom list displayed on the service portal however there is an issue I am hoping someone can help me to finish this script below so that the column headers will display the Label Value of the field and NOT the field name.

 

I understand from digging that the label values are stored in the sys_documentation table but I assume there would need to be a lookup to that table for each field name value in the array and then either create a new array with the label values from the sys_documentation table. I cannot figure out how to do this so any scripting help would be greatly appreciated.

 

SCRIPT:

(function() {
  /* populate the 'data' object */
  /* e.g., data.table = $sp.getValue('table'); */
 
data.rec = [];
 
data.table = $sp.getValue('table');
data.query = $sp.getValue('filter');
 
data.fieldList = $sp.getValue('field_list');
data.fieldList = data.fieldList.split(',');
 
data.header = $sp.getValue('field_list');
data.header = data.header.split(',');
 
var gr = new GlideRecord(data.table);
gr.addEncodedQuery(data.query);
gr.query();
while(gr.next()){
var json = {};
for (var i= 0; i< data.fieldList.length; i++){
json[data.fieldList[i]] = gr.getDisplayValue(data.fieldList[i]);
}
json.sys_id= gr.getValue('sys_id');
data.rec.push(json);
}
 
})();
 
What the header looks like
phil34_0-1695881250586.png

These column headers are the field names from the core_company table and there are a couple of custom fields in that table.

in the sys_description table I can see the entries as per below and I want to display "Support Email" instead of "u_support_email" and "Support Phone" instead of "u_support_phone".

phil34_1-1695881553335.png

 

4 REPLIES 4

Kalyani Jangam1
Mega Sage
Mega Sage

Hi @phil34 

try to use gr.[field name].getLabel();
When you call field in header

Hi Kalyani,

I am not sure how to incorporate that in the script I am not very good at scripting I tried
data.header = $sp.getValue('field_list.label');  (Did'nt Work)

 

If you can show me where and how to update the code that would be great.

Cheers

Phil

 

Hi @phil34 

Below you can try this

json[data.fieldList[i]] = gr.getLabel(data.fieldList[i]);

 

Please try and let me know if it helpful and not.

Hello Kalyani,

 

Sorry to be so stupid but I cannot figure out how to update my script to accommodate your line of code I have put the code below would be great if you can adjust the entire script to accommodate your suggested line?

 

Your Line = json[data.fieldList[i]] = gr.getLabel(data.fieldList[i]);

 

Put it where? in this code

SCRIPT:

(function() {
  /* populate the 'data' object */
  /* e.g., data.table = $sp.getValue('table'); */
 
data.rec = [];
 
data.table = $sp.getValue('table');
data.query = $sp.getValue('filter');
 
data.fieldList = $sp.getValue('field_list');
data.fieldList = data.fieldList.split(',');
 
data.header = $sp.getValue('field_list');
data.header = data.header.split(',');
 
var gr = new GlideRecord(data.table);
gr.addEncodedQuery(data.query);
gr.query();
while(gr.next()){
var json = {};
for (var i= 0; i< data.fieldList.length; i++){
json[data.fieldList[i]] = gr.getDisplayValue(data.fieldList[i]);
}
json.sys_id= gr.getValue('sys_id');
data.rec.push(json);
}
 
})();




 

 

Cheers

Phil