Accessing record fields in a GlideRecord

Bob20
Tera Contributor

I have table which has three columns and I'm using a form OnChange event to trigger a client script to look for one record, then pull data from another field from the same record and place it on the form.

The code is:

function onChange(control, oldValue, newValue, isLoading) {

    if (isLoading || newValue == '') {

          return;

    }

    //alert('Click Rate update triggered');

    populateJobData();

}

function populateJobData(){

  var selected = g_form.getValue('u_mr_print_job'); //user data from the form

  //alert('Print item selection: ' + selected);

  var pjob = new GlideRecord('u_mr_print_shop_items'); //open the table

  alert('GlideRec created');

  pjob.addQuery('u_mr_print_service', selected); //find the record

  pjob.query();

  alert('Query has run');

  if (pjob.hasNext()) {

  //yes, found it, do it

  alert('Record found');

  alert('Click Rate: ' + pjob.u_mr_click_rate);

  } else {

  //no, didn't find it

  alert('GlideRec empty');

  }

}

The table is u_mr_print_shop_items and the fields are: u_mr_print_service, u_mr_click_rate, u_mr_delivery. I look for a matching record for the u_mr_print_service, then pull out the u_mr_click_rate value. In the code I get an "undefined" value. it's as if the field does not exist. I have looked through the Community but can't seen to find an explanation or solution.

Thanks,

Bob.

1 ACCEPTED SOLUTION

One correction to your code. Try below



function onChange(control, oldValue, newValue, isLoading) {


    if (isLoading || newValue == '') {


          return;


    }


    //alert('Click Rate update triggered');


    populateJobData();


}



function populateJobData(){


  var selected = g_form.getValue('u_mr_print_job'); //user data from the form


  //alert('Print item selection: ' + selected);



  var pjob = new GlideRecord('u_mr_print_shop_items'); //open the table


  alert('GlideRec created');


  pjob.addQuery('u_mr_print_service', selected); //find the record


  pjob.query();


  alert('Query has run');



  if (pjob.next()) {


  //yes, found it, do it


  alert('Record found');


  alert('Click Rate: ' + pjob.u_mr_click_rate);


  } else {


  //no, didn't find it


  alert('GlideRec empty');


  }


}



Please mark this response as correct or helpful if it assisted you with your question.

View solution in original post

11 REPLIES 11

One correction to your code. Try below



function onChange(control, oldValue, newValue, isLoading) {


    if (isLoading || newValue == '') {


          return;


    }


    //alert('Click Rate update triggered');


    populateJobData();


}



function populateJobData(){


  var selected = g_form.getValue('u_mr_print_job'); //user data from the form


  //alert('Print item selection: ' + selected);



  var pjob = new GlideRecord('u_mr_print_shop_items'); //open the table


  alert('GlideRec created');


  pjob.addQuery('u_mr_print_service', selected); //find the record


  pjob.query();


  alert('Query has run');



  if (pjob.next()) {


  //yes, found it, do it


  alert('Record found');


  alert('Click Rate: ' + pjob.u_mr_click_rate);


  } else {


  //no, didn't find it


  alert('GlideRec empty');


  }


}



Please mark this response as correct or helpful if it assisted you with your question.

Sanjiv!   Thank you that worked. I can now stop banging my head on my desk.


The problem with using GlideRecord on the client side is GlideRecord returns all of the fields and is synchronous, meaning there could be a significant delay in response and the end user experiencing a laggy browser. In contrast, GlideAjax will only return the data you request, meaning less payload, AND it can be done asynchronously with a callback function to keep the end user from experiencing a browser that locks up.


Pradeep Sharma
ServiceNow Employee
ServiceNow Employee

Hello Bob,



Create a client script and place the code between function template.


//Place the below code in between client script function template


var ga = new GlideAjax('printItems');


ga.addParam('sysparm_name','getPrintShopItems');


ga.addParam('sysparm_print_job', g_form.getValue('u_mr_print_job'));


ga.getXML(printItemsParse);




function printItemsParse(response) {


    var answer = response.responseXML.documentElement.getAttribute("answer");


    alert(answer); //will return click rate


}






Create a script include


Name : printItems


Client Callable: True


Script :


var printItems = Class.create();


printItems.prototype = Object.extendsObject(AbstractAjaxProcessor, {


    getPrintShopItems: function() {


    var   selected = this.getParameter('sysparm_print_job');


        var pjob = new GlideRecord('u_mr_print_shop_items');


        pjob.addQuery('u_mr_print_service', selected); //find the record


        pjob.query();


      if(pjob.next())


      {


      return pjob.u_mr_click_rate;


      }


    },




    _privateFunction: function() { // this function is not client callable        


   


    }




});








Pradeep,



I'll give this a try when I have some time.


Many thanks.



Bob.