Dynamically retrieving the Labels of fields on the Current record
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-23-2017 03:20 PM
I've been struggling with this one for a little while now, and was hoping to find some help here.
I have a business rule on a table in a Scoped App which is running some functions to look at a related record to retrieve a comma separated list of field values, then use those field values elsewhere (to programatically create a text block for a child Incident record for example).
Current Record -> Related Configuration record (contains a comma separated list of field values called "additional_field_1")
//construct the text for all the display fields in the Subcategory Map string field
var displayFields = subCatMap.additional_field_1.toString(); // Grab the Display Field from the Mapping table and split into array.
var fieldArray = displayFields.split(",");
for (var a=0; a< fieldArray.length; a++){ // use the field array to create a loop
var currentField = fieldArray[a];
fieldText += current.getElement('currentField').getLabel(); +": "+ current.getValue(currentField)+"\n"; // for each field in the loop, capture the Label and value with a Line Break
The line "current.getElement('currentField').getLabel();" doesn't get me what I want - I think at the moment it errors out.
I've tried "current.getLabel('currentField');' but this only gives me the label of the current record type - not the field that is named by my "currentField" string
Any idea how i might be able to ask ServiceNow to just give me current.currentField.getLabel() where currentField is dynamically set earlier in the loop?
- Labels:
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-23-2017 04:18 PM
Hi Kevin,
Could you please try the below code,
- var displayFields = subCatMap.additional_field_1.toString();
- var fieldArray = displayFields.split(",");
- var fieldText = '';
- for (var a=0; a < fieldArray.length; a++){
- var currentField = fieldArray[a];
- fieldText += current.getElement(currentField).getLabel() + " : "+ current.getValue(currentField)+"\n";
- }
Hope this helps you Kevin.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-23-2017 04:24 PM
Thanks Anudeep,
Looks like it was a dumb syntax error - used '' when I shouldn't have. Shawn's solution was a little more elegant again.
Here's what worked for me:
//construct the text for all the display fields in the Subcategory Map string field
var displayFields = subCatMap.additional_field_1.toString(); // Grab the Display Field from the Mapping table and split into array.
var fieldArray = displayFields.split(",");
for (var a=0; a< fieldArray.length; a++){ // use the field array to create a loop
var currentField = fieldArray[a];
fieldText +="\n"+ current[currentField].getLabel() +": "+ current.getValue(currentField)+"\n"; // for each field in the loop, capture the Label and value with a Line Break
}