Dynamically retrieving the Labels of fields on the Current record

kevinclark-7EL
Tera Contributor

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?

6 REPLIES 6

athm
Tera Guru

Hi Kevin,



Could you please try the below code,



  1. var displayFields = subCatMap.additional_field_1.toString();                          
  2. var fieldArray = displayFields.split(",");  
  3. var fieldText = '';
  4. for (var a=0; a < fieldArray.length; a++){                                                                    
  5. var currentField = fieldArray[a];  
  6. fieldText += current.getElement(currentField).getLabel() + " : "+ current.getValue(currentField)+"\n";
  7. }

Hope this helps you Kevin.


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


}