We've updated the ServiceNow Community Code of Conduct, adding guidelines around AI usage, professionalism, and content violations. Read more

Catalog client script to populate reference variable not working in Service Portal

Maria DeLaCruz
Tera Guru

Hello,

I've created the following onLoad catalog client script to auto-populate a reference variable, and it works great on the Desktop UI, but not the Service Portal UI.  Is there something in this script that is not supported in the Service Portal?  I do have "All" selected for the UI type.

 

function onLoad() {
//Type appropriate comment here, and begin script below
var proj = [];
var gr = new GlideRecord('u_project_name');
gr.addQuery('u_used', false);
gr.query();
while(gr.next())
{
proj.push(gr.u_number.toString());
}

proj.sort(function(a, b){return a-b});
//gs.print(proj[0]);

var confidential = new GlideRecord('u_project_name');
confidential.addQuery('u_number', proj[0]);
confidential.query();

if(confidential.next())
g_form.setValue('project_name', confidential.sys_id);
}

 

Any assistance would be greatly appreciated!


Thanks,
Maria

1 ACCEPTED SOLUTION

Mike Patel
Tera Sage

Doing Glirerecord query is not recommended in client script. Use GlideAJAX instead and script include

View solution in original post

7 REPLIES 7

Mike Patel
Tera Sage

Doing Glirerecord query is not recommended in client script. Use GlideAJAX instead and script include

Thanks Mike!  The GlideAjax worked with populating the variable.

I do have another question.  In an 'onSubmit' catalog client script, I need to update a record in another table.  So, GlideRecord only works on Desktop UI not Service Portal UI.  Would I be able to utilize GlideAjax for that?  If so, how?  Thanks!

Yes, you can.

Can you share your script so I can help you with that.

Here's my script.  I really appreciate you looking into this.

 

function onSubmit() {

var projName = g_form.getReference('project_name', callback);
function callback(projName)
{
var used = projName.u_used;

var ga = new GlideAjax('ConfidentialProject');
ga.addParam('sysparm_name', 'getProjectName');

if (used == 'true'){
ga.getXML(projectNameUsed);
}

if (used == 'false'){
ga.getXML(projectNameUnused);
}
}

function projectNameUsed(response){
var answer = response.responseXML.documentElement.getAttribute("answer");
g_form.setValue('project_name', answer);
g_form.setReadOnly('project_name', true);
}

function projectNameUnused(response2){

var answer2 = response2.responseXML.documentElement.getAttribute("answer");

var proj = new GlideRecord('u_project_name');
proj.addQuery('sys_id', answer2);
proj.query();
if (proj.next()){
proj.u_used = 'true';
proj.update();
}
}
}