How do I call the most current record in a client script on a custom table?

ctsmith
Mega Sage

function onLoad() {

      var express = new GlideRecord('sys_user');

      express.addQuery('sys_id', g_user.userID);

      express.query();

      while (express.next()) {

              g_form.setValue('NetID', express.user_name);

              g_form.setValue('Email', express.email);

              g_form.setValue('Phone', express.phone);

              g_form.setValue('Name', express.name);

              var express2 = new GlideRecord('u_purchorder');

              express2.addQuery(sys_id, g_form.getUniqueValue()); //Yeah, obviously this is wrong.   What kind of addQuery can I do to pull the most recent record that matches info above?

              express2.query();

              while (express2.next()) {

                      g_form.setValue('po_number', express2.u_po_number);

                      g_form.setValue('sq_timestamp', express2.u_sq_timestamp);

                      g_form.setValue('requisition_numnam', express2.u_request_item_number);

                      g_form.setValue('description1', express2.u_request_item);

              }

      }

}

The way it runs, it will pull a random record from u_purchorder instead of the current one and populate the variables with an older record instead of the current one.   Can this be fixed?

Many thanks!

1 ACCEPTED SOLUTION

I was able to get this to work.   I pulled the reference value of the request item number on the individual request item form and then queried the purchorder table and compared the request item number that is stored in that table to the current request item number in the reference field.



This runs on the sc_req_item table:



function onLoad() {


      var isit = g_form.getValue('number');


      var express1 = new GlideRecord('u_purchorder');


      express1.addQuery('u_request_item_number', isit);


      express1.query();


      while (express1.next()) {


              g_form.setValue('variables.po_number', express1.u_po_number);


              g_form.setValue('variables.sq_timestamp', express1.u_sq_timestamp);


      }


}


View solution in original post

13 REPLIES 13

Chuck Tomasi
Tera Patron

Hi Chris,



Have you tried adding



express.orderByDesc('sys_created_on'); // sort by descending created date.


Chuck Tomasi
Tera Patron

Sorry, orderByDesc() isn't available client side. Looks like you're going to have to use GlideAjax on this one buddy.




Client Side GlideRecord - ServiceNow Wiki


GlideAjax - ServiceNow Wiki


Actually, OrderBy works.   I needed it ascending.   So... the only thing I can think of where this could be a problem if for some reason there is a mismatch between the order and the current userID, but I'm not smart enough to figure out exactly how they could happen.  


Hi Chris,



To add, Client scripting uses either data available on the client or data retrieved from the server. Use client data as much as possible to eliminate the need for time-consuming server lookups. The best practice is to use GlideAjax here. Script is already shared by Chuck.


Client Script Best Practices - ServiceNow Wiki



More info on GlideAjax here.


GlideAjax - ServiceNow Wiki