- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-26-2022 11:57 AM
Hey community! I am sure this is simple, but am new to the scripting world. Please bear with me:
Record 1 - Requested Item has a variable that is filled in by the user
Variable is called app_name
They fill out the rest of the form and submit it and they get their REQ/RITM number.
Record 2 - User goes to fill out the same form, they choose to follow up on the previous ticket
Variable is called previous_ritm (user types in their old RITM number)
They fill out the rest of the form and submit it and they get their REQ/RITM number.
Now here is my requirement. How do I pull in the app_name variable (from the old ticket) into the new ticket under the variable name previous_app? I created an onLoad catalog client script with the following code and keep getting either 'undefined' or the field just stays blank. Maybe extra sets of eyes can tell me what it is I am missing here or what that code looks like?
Here is my code:
function onLoad() {
//Type appropriate comment here, and begin script below
var cRITM = g_form.getValue('variables.previous_ritm'); //get value of variable previous_ritm
var pRITM = new GlideRecord('sc_req_item'); // check previous RITMS with value of current variable previous_ritm
pRITM.addQuery('number', cRITM);
pRITM.query();
if (pRITM.next())
variables.previous_app = g_form.getValue('pRITM.variables.app_name');
}
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-29-2022 07:02 AM
This is a limitation of running a GlideRecord in a client script, which I should have mentioned from the outset is not supported nor considered best practice anyway. The variables are stored as an object on the sc_req_item record, so it's just like if you wanted to return the Name (display value) of a reference field, you would need to dot-walk to get to it, which you can do all day on a server script.
To populate variables from a supplied RITM that is a reference variable, you would need a Client Script that looks like this - calling a Script Include via GlideAjax. You can do this onLoad, and/or onChange when the RITM value changes:
function onLoad() {
var gr = new GlideAjax('RITMUtils');
gr.addParam('sysparm_name', 'getVars');
gr.addParam('sysparm_ritm', g_form.getValue('previous_ritm'));
gr.getXML(vars);
}
function vars(response) {
var answer = JSON.parse(response.responseXML.documentElement.getAttribute("answer"));
g_form.setValue('previous_app', answer.app_name);
}
Next create a Script Include using the same name as in the GlideAjax call. Ensure the Client callable box is checked.
var RITMUtils = Class.create();
RITMUtils.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getVars: function() {
var varObj = {};
var json = new JSON();
var ritm = this.getParameter('sysparm_ritm');
var gr = new GlideRecord('sc_req_item');
gr.addQuery('sys_id', ritm);
gr.query();
if(gr.next()){
varObj.app_name = gr.variables.app_name.toString();
//add a similar line for any other fields or variables from the RITM that you want to use in the Client Script
}
return json.encode(varObj);
},
type: 'RITMUtils'
});
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-26-2022 05:52 PM
Hi Jimmy,
Is previous_ritm a reference variable? It sounds like it should be so the user can type in a (partial) number and it will find the actual record rather than relying on a text variable where we assume the number is entered completely and correctly. In any event, variables are treated the same as form variables for g_form, so to get the value of a variable use:
var cRITM = g_form.getValue('previous_ritm');
If/once this is a reference, then the addQuery line needs to be:
pRITM.addQuery('sys_id', cRITM);
since reference variables store the sys_id of the referenced table, even though they display a name or number. Once the correct record is returned by the GlideRecord query, to set the value of a field or variable, you would use:
g_form.setValue('previous_app', pRITM.app_name);
You can test/troubleshoot this script as it is executing by adding alert lines to make sure it's running and to see how far it gets and what is stored in variables or returned by GlideRecord queries. This will show you where it is failing.
function onLoad() {
alert ('script running');
var cRITM = g_form.getValue('previous_ritm'); //get value of variable previous_ritm
alert('cRITM =' + cRITM);
var pRITM = new GlideRecord('sc_req_item'); // check previous RITMS with value of current variable previous_ritm
pRITM.addQuery('sys_id', cRITM); // or 'number' if this is a text variable
pRITM.query();
if (pRITM.next()) {
alert('RITM found ' + pRITM.number);
g_form.setValue('previous_app', pRITM.app_name);
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-26-2022 09:03 PM - edited 10-27-2022 08:35 AM
Hey Brad! First of all, THANK YOU! The script seems to be working just fine, except for the last part for
g_form.setValue('previous_app', pRITM.app_name);
The variable field for app_name keeps showing up as "undefined"
The 3 alerts are showing as expected only when I modified the script as seen below:
function onLoad() {
alert ('script running');
var cRITM = g_form.getValue('variables.previous_ritm'); //get value of variable previous_ritm
alert('cRITM =' + cRITM);
var pRITM = new GlideRecord('sc_req_item'); // check previous RITMS with value of current variable previous_ritm
pRITM.addQuery('sys_id', cRITM); // or 'number' if this is a text variable
pRITM.query();
if (pRITM.next()) {
alert('RITM found ' + pRITM.number );
g_form.setValue('previous_app', pRITM.app_name);
}
}
Just the last line I am having issues with. Thoughts?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-27-2022 06:59 AM
Sorry I missed that - with the syntax I suggested in that line it's looking for app_name to be a field on the sc_req_item table, so if this is the name of one of your variables you would use
g_form.setValue('previous_app', pRITM.variables.app_name);
The app_name and previous_app variables also need to be the same type for this to work as expected.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-27-2022 07:46 AM - edited 10-27-2022 07:48 AM
Thanks for the response Brad! Yeah, for some reason it is still coming back as "undefined"
the script as it sits right now:
function onLoad() {
alert ('script running');
var cRITM = g_form.getValue('variables.previous_ritm'); //get value of variable previous_ritm
alert('cRITM =' + cRITM);
var pRITM = new GlideRecord('sc_req_item'); // check previous RITMS with value of current variable previous_ritm
pRITM.addQuery('sys_id', cRITM); // or 'number' if this is a text variable
pRITM.query();
if (pRITM.next()){
alert('Previous RITM found ' + pRITM.number );
g_form.setValue('variables.previous_app', pRITM.variables.app_name);
}
}
More interesting is the fact that if I edit the last line to show:
g_form.setValue('variables.previous_app', pRITM.stage);
It shows up just fine within the current variable of "previous_app"
I tried other variables on the previous ticket and they also show as "undefined" this is really stumping me. I appreciate your insight!