gr[fieldName].getLabel() does not work on sys_email table

xiaix
Tera Guru

As I had originally posted here:   https://community.servicenow.com/thread/216254

I just found out that if I query the sys_email table with a sys_id, the gr[fieldName].getLabel() breaks the script include.

1 ACCEPTED SOLUTION

I figured out what was breaking it.



if (gr.next())


{


      var directURL = (gs.getProperty('glide.servlet.uri') + "nav_to.do?uri=" + tableName + ".do?sys_id=" + sysID);


     


      fields = new GlideRecordUtil().getFields(gr);


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


      {


              fieldName = fields[i];


             


              // WITHOUT THIS IF() STATEMENT,


              // THIS SCRIPT WILL **BREAK** BECAUSE


              // "sys_table_name" IS NOT A FIELD ON THE sys_email TABLE!!


              //


              // IF IT'S NOT ON THE TABLE, WHY THE HECK DOES IT FIND IT!!?????


              if (fieldName == "sys_table_name")


                      continue;


             


              recordArray.push({ field_name: fieldName, value: gr.getDisplayValue(fieldName), url: directURL, label: gr[fieldName].getLabel() });


              fieldsFound = 'true';


      }


     


      if (fieldsFound == 'true')


              return (new JSON().encode(recordArray));


      else


              return "No table fields were found in the GlideRecordUtil().getFields(gr) call";


}



Notice the new if() in my code above.



How the heck does "sys_table_name" get found when it's nowhere on the sys_email table!??


View solution in original post

5 REPLIES 5

srinivasthelu
Tera Guru

Hi David,


It just works fine for me



Here is the script i have tried



var gr=new GlideRecord('sys_email');


gr.query();


if(gr.next())


{


gs.print(gr['instance'].getLabel())


}



Could you please paste the script include that is breaking.



thanks


Srinivas



Hope that helps


mikeblack
Tera Contributor

You should rather use :


var fields = grINC.getFields();


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


var glideElement = fields.get(i);


if (glideElement.hasValue()) {


gs.print( glideElement.getName() )


}


}


Brad Tilton
ServiceNow Employee
ServiceNow Employee

Hi David,



I con't think there's anything special about the email table, but some of those sys_ tables have some special rules around them. Could you post the script include code and the code that is calling the script include here?


Here is my Script Include:



var search_for_sysID = Class.create();


search_for_sysID.prototype = Object.extendsObject(AbstractAjaxProcessor, {


     


// 1st Function


      getRecords: function()


      {


              var tableSysID = this.getParameter('sysparm_tableSysID');


              var sysID = this.getParameter('sysparm_sysID');


             


              var tableName = '';


             


              // First, query the "Table" table to get the requested table "name".


              var gr = new GlideRecord('sys_db_object');


              gr.addQuery('sys_id', tableSysID);


              gr.query();


             


              if (gr.next())


              {


                      tableName = gr.name.toString();


              }


             


              if (tableName.length < 1)


                      return 'no table name';


             


              // We now have a proper 'table_name'


              // Now query the correct table by the given sysID and return the values of the found record


              gr = new GlideRecord(tableName);


              gr.addQuery('sys_id', sysID);


              gr.query();


             


              var fields;


              var recordArray = [];


              var fieldName = '';


              var fieldsFound = 'false';


             


              if (gr.next())


              {


                      var directURL = (gs.getProperty('glide.servlet.uri') + "nav_to.do?uri=" + tableName + ".do?sys_id=" + sysID);


                     


                      fields = new GlideRecordUtil().getFields(gr);


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


                      {


                              fieldName = fields[i];


                              recordArray.push({ field_name: fieldName, value: gr.getDisplayValue(fieldName), url: directURL, label: gr[fieldName].getLabel() });


                              fieldsFound = 'true';


                      }


                     


                      if (fieldsFound == 'true')


                              return (new JSON().encode(recordArray));


                      else


                              return "No table fields were found in the GlideRecordUtil().getFields(gr) call";


              }


             


              return 'no record';


      }


     


});




Here is the UI Page:



HTML:


<?xml version="1.0" encoding="utf-8" ?>


<j:jelly trim="false" xmlns:j="jelly:core" xmlns:g="glide" xmlns:j2="null" xmlns:g2="null">




<style style="text/css">


      .hoverTable tr:hover {


              background-color: #00FF00;


      }


</style>




<table id="theTable">


    <tr>


          <td colspan="3">Search by sys_id <div><span style="font-style: italic; font-size: x-small;">(v1.0)</span></div></td>


    </tr>


    <tr>


          <td colspan="3" style="height:20px;"></td>


    </tr>


    <tr>


          <td>SysID:</td>


          <td><input size="50" id="sysid" name="sysid" type="text" maxlength="32" onkeyup="sysid_onKeyUp()" /></td>


          <td id="sysid_error"></td>


    </tr>


    <tr>


          <td>Table:</td>


          <td><g:ui_reference name="QUERY:nameISNOTEMPTY" table="sys_db_object" onchange="hideIcon()" /></td>


          <td id="table_error"></td>


    </tr>


    <tr>


          <td colspan="3"><button id="searchBtn" onclick="search(); return false;">Search</button></td>


    </tr>


    <tr>


          <td colspan="3"></td>


    </tr>


    <tr>


          <td id="url" colspan="3"></td>


    </tr>


    <tr>


          <td colspan="3" id="theInfo"></td>


    </tr>


</table>




</j:jelly>




CLIENT SCRIPT:


function hideIcon()


{


      var icon = gel('view.sys_db_object');


      if (icon)


      {


              icon.parentNode.removeChild(icon);


      }


      var tableError = gel('table_error');


      if (tableError) { tableError.innerHTML = ''; }


}




function sysid_onKeyUp()


{


      var sysidError = gel('sysid_error');


      if (sysidError) { sysidError.innerHTML = ''; }


}




function onLoad()


{


      var el = gel('sys_display.QUERY:nameISNOTEMPTY');


      if (el)


      {


              el.size = 50;


      }


} onLoad();




function displayData(recordSysID, tblSysID)


{


      var gaj = new GlideAjax('search_for_sysID');


      gaj.addParam('sysparm_name','getRecords');


      gaj.addParam('sysparm_sysID', recordSysID);


      gaj.addParam('sysparm_tableSysID', tblSysID);


      gaj.getXMLWait();


      var answer = gaj.getAnswer();


      console.debug('answer: ' + answer);


     


      var info = '<table style="border: 1px solid black;" class="hoverTable">';




      if (answer)


      {


              if (answer == 'no record')


              {


                      var el = gel('theInfo');


                      if (el)


                      {


                              el.innerHTML = "That sysID was not found on the table you specified.";


                      }


                      el = gel('url');


                      if (el)


                      {


                              el.innerHTML = "";


                      }


                      return;


              }


              else if (answer == 'no table name')


              {


                      var el = gel('theInfo');


                      if (el)


                      {


                              el.innerHTML = "That table was not found.";


                      }


                      el = gel('url');


                      if (el)


                      {


                              el.innerHTML = "";


                      }


                      return;


              }


              else


              {


                      var answerArray = answer.evalJSON();


                      answerArray.sort(compare);


                      var directLink = '';


                     


                      info += '<tr><td>FIELD NAME</td><td>FIELD VALUE</td></tr><tr><td colspan="2"><hr /></td></tr>';




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


                      {


                              info += '<tr><td style="font-weight: bold; background-color: #E6E6E6;">';


                              info += answerArray[i]['label'];


                              info += "</td><td>";


                              info += answerArray[i]['value'];


                              info += "</td></tr>";


                             


                              directLink = answerArray[i]['url'];


                      }


                      info += "</table>";


                     


                      var el = gel('theInfo');


                      if (el)


                      {


                              el.innerHTML = info;


                      }


                      el = gel('url');


                      if (el)


                      {


                              el.innerHTML = '<div><br /><a style="color:blue;" target="_blank" href="' + directLink + '">Direct link to this found record</a> (a new page will be opened)<br /></div>';


                      }


              }


      }


}




function search()


{


      var table = '';


      var tableSysID = '';


      var badFields = 'false';


     


      var el = gel('sys_display.QUERY:nameISNOTEMPTY');


      if (el)


      {


              if (el.value.length < 1)


              {


                      var tableError = gel('table_error');


                      if (tableError) { tableError.style.color = 'red'; tableError.innerHTML = '<img src="redLeftHand.jpgx" width="50" height="25"/> You must choose a valid table.'; }


                      badFields = 'true';


              }


             


              table = el.value;


      }


      else


      {


              alert("No table found");


              return;


      }


     


      el = gel('sysid');


      if (el)


      {


              if (el.value.length < 32)


              {


                      var sysidError = gel('sysid_error');


                      if (sysidError) { sysidError.style.color = 'red'; sysidError.innerHTML = '<img src="redLeftHand.jpgx" width="50" height="25"/> The SysID is not correct.'; }


                      badFields = 'true';


              }


             


              var el2 = gel('QUERY:nameISNOTEMPTY');


              if (el2)


              {


                      tableSysID = el2.value;


              }


              else


              {


                      alert("No tableSysID found.");


                      return;


              }


             


              if (badFields == 'false')


                      displayData(el.value, tableSysID);


      }


      else


      {


              alert("No sysid found");


              return;


      }


}




function compare(a, b)


{


  if (a.field_name < b.field_name)


      return -1;


  else if (a.field_name > b.field_name)


      return 1;


  else


      return 0;


}