We're reclaiming inactive PDIs to keep them available for active builders. Learn what's changing, who's affected, and how to protect your work. Read More

On Change Client Script Not Working

Aqil
Tera Contributor

I have a OnChange client script running on a catalog item, that runs when the value of a "u_subcategory" variable changes. And, I just want to get the value of "u_subcategory" variable. I'm using the g_form.getValue() method but it's not working, then I used g_scratchpad, but that's too not working. I have checked the ServiceNow logs table, and it is giving the below error:

Exception while executing request: stream has already been operated upon or closed: com.glide.rest.util.RESTRuntimeException: Exception while executing request: stream has already been operated upon or closed: com.glide.rest.handler.impl.ServiceHandlerImpl.handleInvocationTargetException(ServiceHandlerImpl.java:76)
com.glide.rest.handler.impl.ServiceHandlerImpl.invokeService(ServiceHandlerImpl.java:49)
com.glide.rest.processors.RESTAPIProcessor.process(RESTAPIProcessor.java:346)
com.glide.processors.AProcessor.runProcessor(AProcessor.java:733)
com.glide.processors.AProcessor.processTransaction(AProcessor.java:291)
com.glide.processors.ProcessorRegistry.process0(ProcessorRegistry.java:187)
com.glide.processors.ProcessorRegistry.process(ProcessorRegistry.java:175)
com.glide.ui.GlideServletTransaction.process(GlideServletTransaction.java:58)
com.glide.sys.Transaction.run(Transaction.java:2645)
com.glide.ui.HTTPTransaction.run(HTTPTransaction.java:30)
java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
java.base/java.lang.Thread.run(Thread.java:829)

7 REPLIES 7

Kris Moncada
Kilo Sage

Hi @Aqil ,

 

Can you post the onChange catalog client script that you created?

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
     
     var
ajax = new GlideAjax('SG_submit_a_ticket');
    ajax.addParam('sysparm_name', 'getSubcategoryValue');
    ajax.addParam('sysparm_category', g_form.getValue('category'));
    ajax.getXMLAndWait();

    var response = ajax.getAnswer();
    alert(response);
    if (response == "Power BI Access Request") {
        g_form.setVisible("name_of_report", true);
    } else {
        g_form.setVisible("name_of_report", false);
    }
}
I tried GlideAjax now, I want to check if the value of the subcategory is "Power BI Access Request", then show the variable "name_of_report". But I'm not getting the "u_subcategory" value.


Aqil
Tera Contributor
var ajax = new GlideAjax('SG_submit_a_ticket');
    ajax.addParam('sysparm_name', 'getSubcategoryValue');
    ajax.addParam('sysparm_category', g_form.getValue('category'));
    ajax.getXMLAndWait();
 
    var response = ajax.getAnswer();
alert(response);
    if (response == "Power BI Access Request") {
        g_form.setVisible("name_of_report", true);
    } else {
        g_form.setVisible("name_of_report", false);
    }
And, below is the Script Include, because g_form.getValue() method is not working, so I tried GlideAjax too, but it is also not working.

getSubcategoryValue: function() {
var cat = this.getParameter('sysparm_category');
gs.info('Category parameter: ' + cat);
var gr = new GlideRecord('sys_choice');
gr.addEncodedQuery("name=incident^element=subcategory^inactive=false^dependent_value=" + cat);
gr.query();
if (gr.next()) {
gs.info('Subcategory value: ' + gr.getValue('value'));
return ''+ gr.getValue('value');
}
gs.info('No subcategory found');
return '';
}


Hi @Aqil ,

 

    ajax.getXMLAndWait(); Is incorrect method instead use ajax.getXMLWait();

 

https://www.servicenow.com/community/developer-articles/getxmlwait-alternative-for-service-portal/ta...

 

If my response helped, please mark it as the accepted solution and give a thumbs up👍.
Thanks,
Anand