Passing sys_id from UI Action to UI Page to Script Include

y-allen
Tera Contributor

Hello. I am creating a UI action and UI page to allow users to make updates to records. Upon entering the requested information, the UI page client script captures the information entered, sends it to script include that updates the record. I am attempting to capture the sys_id of the record that is calling the UI action/ui page, etc to pass it to the script include so I can do a glidequery on the table and update the comments on that specific record. Here are the lines of code I am using to achieve this:

 

This is the line in the UI Action to pass the sys_id to the UI page:

 

 

gm.setPreference('current', g_form.getUniqueValue());

 

 

I am declaring two variables in the HTML section of the UI page to obtain the sys_id. The second entry was the result of troubleshooting and was not the original code. Also, it have it wrapped in a <g:evaluate>.

 

 

<j:set var= "jvar_current" value= "${current}"/>

var recordSysId =  RP.getWindowProperties().currentRec;

 

 

I have the "jvar_current" declared in hidden variable in hopes of being able to access it in the client script.

 

 

<input type="hidden" name="currentRec" value="${jvar_current}" id="currentRec" />

 

 

 
In the client script, I have the scripts below. The first to bring the sys_id into the client script and the second line is a part of the GlideAjax to send the sys_id to the script include.
 
In this configuration, the script include returns "unable to find a record to update". Adding a gs.info line returns and object Nodelist. In the past, this was received while working with many sys_id's and was solved by adding a .split(','); in the script include. That did not work here. I also tried adding .val();, .value;, and [0].value;, to the end of the line but the script include either returned and empty log entry or object Nodelist. I also tried document.getElementbyId, but the UI page did not close and refresh the page. There were no long entries when attempting this method.

 

 

var iscpRecord = document.getElementsByName('currentRec');
//I also tried:
var iscpRecord = gel('currenRec');


//Line in the GLideAjax call.
ga.addParam('sysparm_record', iscpRecord);

//Call the variable in the script include
const record = this.getParameter('sysparm_record').split(',');

//Use the variable to look up the record and update the comments:
new global.GlideQuery('x_snc_authorizatio_table_name')
.where('sys_id', 'IN', record)
.update({
comments: allEnteredData
});

 

 

The .where clause was originally:

 

 

.where('sys_id', record)

 

 

Again, I have been trying any and all ideas to get this to work, but I have been unsuccessful.
 
Can anyone provide assistance with properly passing the sys_id?
 
As always, thanks in advance.
 
 
 
 
3 REPLIES 3

Allen Andreas
Administrator
Administrator

Hi,

Sorry, your post was a bit confusing as you had a lot of content and it wasn't clear where your question was actually at, perhaps consider bolding it or highlighting it to help us? Otherwise, not everyone is going to read line-by-line of all this text initially. We want to see if we can help, firstly.

 

Anyways, in your script include have you checked the value of "record" to see what you're getting? Why are you splitting on it if it's just a record sys_id? Also, I would recommend reviewing your usage of GlideQuery to ensure you're using it correctly, otherwise, stick with GlideRecord?


Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!

Sure, the question has be formatted for bold.

As for testing what was being passed to record, yes, as stated. In an earlier similar task, with multiple sys_ids, .split was used to separate them. I simply used here as well as a test as the sys_id was being sent in the same way. Hence the structure of the GlideQuery, which I can only assume prompted your recommendation. 

Ilakkia S
Tera Contributor

Hi, 

I have tried similar requirement and I've tried many like gel/setpreference etc but nothing worked. You can get the current window sys_id using below script in UI page(client script)

var params = new URL(location.href).searchParams;
var recordSysId = params.get('sys_id');

 

In this video I'll show you how to make your server side JavaScript easier to maintain. We've talked about Script Includes as a way to improve unit testing, abstracting and reusing logic, but there's another use for them to help remove hard-coded values, decrease maintenance, and improve ...