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

I don't think it's going to work since u_purchorder is a child of u_request_item, you could have multiples on the same u_request_item, correct? If it's a 1:1 (most of the time) then you could do it with a lookup.


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);


      }


}


I'm glad you got it figured out.


Many thanks for your help on this, Mr. T.!