- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-07-2016 12:51 PM
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!
Solved! Go to Solution.
- Labels:
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-11-2016 08:42 AM
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);
}
}

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-07-2016 01:40 PM
I thought you were looking for the newest record, which means you need a descending sort. Hence my reason for throwing the GlideAjax solution out there. More flexibility, better control of what is returned, take advantage of the full server side code.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-07-2016 01:34 PM
Here is some highly untested code that should get you started.
Client script:
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 ga = new GlideAjax('POAjax');
ga.addParameter('sysparm_name', 'getLatestPO');
ga.getXMLAnswer(poCallback);
}
}
function poCallback(answer) {
var aObj = JSON.decode(answer);
g_form.setValue('po_number', aObj.u_po_number);
g_form.setValue('sq_timestamp', aObj.u_sq_timestamp);
g_form.setValue('requisition_numnam', aObj.u_request_item_number);
g_form.setValue('description1', aObj.u_request_item);
}
Script Include:
var POAjax = Class.create();
POAjax.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getLatestPO : function() {
var po = new GlideRecord('u_purchorder');
var aObj = {};
po.orderByDesc('sys_created_on');
po.setLimit(1);
po.query();
if (po.next()) {
aObj.u_po_number = po.getValue('u_po_number');
aObj.u_sq_timestamp = po.getValue('u_sq_timestamp');
aObj.u_request_item_number = po.getValue('u_request_item_number');
aObj.u_request_item = po.getValue('u_request_item');
return JSON.stringify(aObj);
}
return;
},
type: 'POAjax'
});
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-07-2016 01:57 PM
Hmmm... I'm thinking the Ajax script could run into the same issue I was thinking of above. Let's say user 1 comes into SN through an integration. That entrance triggers the record creation on the u_purchorder table and thus the sys_created_on field is populated. User 1 doesn't do anything for awhile and then goes to the catalog item and finally orders something.
In the meantime, user 2 comes into SN through same integration and another record is triggered on the u_purchorder table and that then becomes the record with the newest sys_created_on date. But, user 2 beats user 1 to the punch and completes an order on the catalog item.
When user 1 finally enters the catalog item to order something, he/she will trigger the script onLoad and have the fields populated by the most current sys_created_on date from u_purchorder table, which is actually user 2's since user 2 entered the catalog item first and triggered the onLoad script first.
Does that make sense? I'm quite new to much of this, and perhaps the Ajax script has a check against this that I'm not seeing?
Thanks again all!

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-07-2016 02:01 PM
Yes, there is definitely a 'race condition' design flaw in your design the way it is right now.
You need to find a way to tie that user to that automatically created record somehow, but I don't see it based on the information given. Is it possible to have the user pick a record (based on a filtered list of only their records?)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-07-2016 04:08 PM
Hmmm... the u_purchorder has a column that references the task table field u_request_item. Could there be a way to check against the current u_request_item.