GlideRecord usage in Client Script during onLoad

Ankit28
Kilo Contributor

FINDING: Client Script is using GlideRecord during an onLoad event. GlideRecord on the client is a synchronous call to the server, which introduces delay as it retrieves the entire record or records.

Table Object Name(s):

Incident [incident] Chase Count Incrementer

Script:

function onLoad() {
      //Add a UI macro to a non-reference field
      //Set the name of the field in the 'field' variable
      var field = 'u_chase_count';
      //Append the table name to get the field id
      field = g_form.tableName + '.' + field;

      try {
              //Create the image element and add to the dom
              var img = document.createElement('img');
              img.src = 'plussymbol.pngx';
              img.alt = 'Chase Count Incrementer';
              img.title = 'Chase Count Incrementer';
              var link = document.createElement('a');
              if (navigator.appName == 'Microsoft Internet Explorer') {
                      link.setAttribute('onclick', Function('doSomething()'));
              }
              else {
                      link.setAttribute('onclick', 'doSomething()');
              }
              link.name = 'chase_count_incrementer';
              link.id = 'chase_count_incrementer';
              link.appendChild(img);
              document.getElementById(field).parentNode.appendChild(link);
      }
      catch (e) {
              //alert('Error');
      }
}

//onclick event to fire when the image is clicked
function doSomething() {

      var temp = g_form.getValue('u_chase_count');

      if (temp == '' || temp == null) {

              temp = 0;

      }

      var state = g_form.getValue('incident_state');
      var assGrp = g_form.getValue('assignment_group');
      var id = g_user.userID;
      var gr = new GlideRecord('sys_user_grmember');
      gr.addQuery('group', assGrp);
      gr.addQuery('user', id);
      gr.addQuery('user.active', true);
      gr.query();
      while (gr.next())
      {
              if (state != '7')
              {
                      var newValue = parseInt(temp) + 1;
                      var stringToDisplay = "Chase counter incremented from " + temp + " to " + newValue;
                      g_form.setValue('u_chase_count', newValue);
                      g_form.showFieldMsg('u_chase_count', stringToDisplay, 'info');
              }
      }
}

HideIncidentStateChoices

Script:

function onLoad() {

    if (!g_form.isNewRecord()) {
                                                          // If the incident was "new" and the person updating is either not the caller, or is the assignee regardless of if they are the caller, then we change it to "open"
                                                          if (g_form.getValue('incident_state') == '1' && (g_user.userID != g_form.getValue('caller_id') || g_form.getValue('caller_id') == g_form.getValue('assigned_to'))) { //force incident to active state
                                                                                        g_form.setValue('incident_state', '2');
                                                          }
                                                          // If the incident is anything other than "new" then remove the "new" option
                                                          if(g_form.getValue('incident_state') != 1) {
                                                                                        g_form.removeOption('incident_state', '1');
                                                          }
                            }


var active;    
var gr = new GlideRecord('incident');
gr.addQuery('number',g_form.getValue('number'));
gr.query();
if (gr.next())
{
    active = gr.active;
}

var assGrp = g_form.getValue('assignment_group');
var id=g_user.userID;
var ga = new GlideAjax('u_PriorIncGroupUtil');
ga.addParam('sysparm_name','isUserMemberOfGrp');
ga.addParam('sysparm_id', id);
ga.addParam('sysparm_assGrp',assGrp);
ga.getXMLWait();

if(ga.getAnswer()=='false' && active == 'true') {
    g_form.removeOption('incident_state', '7');
}
}

RECOMMENDATION Given by Service Now: Replace GlideRecord with a Display Business Rule to place needed information into the g_scratchpad object which is available in Client Scripts. For more information, please visit Wiki:Display Business Rules and Wiki: Minimize Server Lookup

I am not familiar with this and want to know how to replace the code in the above client scripts according to the recommendation given by service now. Can someone please provide the display business rule and change to client scripts and explain this conversion. Thanks!

3 REPLIES 3

Pradeep Sharma
ServiceNow Employee
ServiceNow Employee

HI Ankit,



Please refer the section 3.1 example in below wiki lnk


http://wiki.servicenow.com/index.php?title=Client_Script_Best_Practices



I hope this helps


Hi Pradeep,



I have checked this section. It was helpful. But in my code I am little bit confused so can you provide me the exact code of display business rule and client script please.



Thanks,



Ankit


Hi Ankit,


It May help you


Please write a display business rule in your table


in script


what ever value you want in client script assign that value to g_scratchpad


eg: g_scratchpad.valueFromDB='yes';



In Client script   write


var value1=g_scratchpad.valueFromDB;


Now You can use   value1 this or you can use directly g_scratchpad.valueFromDB.



Thanks


KB