Catalog Client Script Getting Null Value from Working Script Include.

Alan42
Tera Guru

I have a table that contains employee data by plant.    When I select 'plant' on a catalog item form I want the QC Manager, Plant Accountant, and Shipping Manager fields to auto populate.    My script include tests out correctly and returns the correct data.     

 

Name - PlantManagerFetcher
Client Callable
All Application scopes

 

var PlantManagerFetcher = Class.create();
PlantManagerFetcher.prototype = {
initialize: function() {},

getManagersByPlant: function(plantName) {
var result = {};
var gr = new GlideRecord('u_rwi_plants');

// Use the correct column names
gr.addQuery('u_plant', plantName);
gr.query();

if (gr.next()) {
result.qc_manager = gr.getValue('u_qc_manager');
result.plant_accountant = gr.getValue('u_plant_accountant');
result.shipping_manager = gr.getValue('u_shipping_manager');
}
return result;
},

type: 'PlantManagerFetcher'
};

 

I've tried a half dozen variants of the catalog client script but keep getting a null error.     Is there anything that stands out in the catalog client script?

Alan42_5-1722456777644.png

 

Catalog Client Script
Name - Plant
All
OnChange


Variable Name - u_plant

function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue === '') {
return;
}

console.log('Selected Plant:', newValue);

var ga = new GlideAjax('PlantManagerFetcher');
ga.addParam('sysparm_name', 'getManagersByPlant');
ga.addParam('plantName', newValue);
ga.getXMLAnswer(function(response) {
console.log('Response:', response);

try {
var managers = JSON.parse(response);
console.log('Parsed Managers:', managers);

// Update form fields with the fetched manager data
g_form.setValue('u_qc_manager', managers.qc_manager || '');
g_form.setValue('u_plant_accountant', managers.plant_accountant || '');
g_form.setValue('u_shipping_manager', managers.shipping_manager || '');

console.log('QC Manager:', g_form.getValue('u_qc_manager'));
console.log('Plant Accountant:', g_form.getValue('u_plant_accountant'));
console.log('Shipping Manager:', g_form.getValue('u_shipping_manager'));

} catch (e) {
console.error('Error parsing JSON response:', e);
}
});
}

 

Alan42_0-1722456567266.pngAlan42_2-1722456651659.png

 

Alan42_1-1722456600537.png   

Alan42_4-1722456731291.png

 

Alan42_3-1722456691921.png

 

 

 

1 ACCEPTED SOLUTION

Arya123
Tera Expert

Hi @Alan42 

Please try and see the below code:

 

Script Include:

 

var PlantManagerFetcher = Class.create();
PlantManagerFetcher.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {

getManagersByPlant: function(plantName) {
        var result = {};
        var gr = new GlideRecord('u_rwi_plants');

        // Use the correct column names
        gr.addQuery('u_plant', plantName);
        gr.query();

        if (gr.next()) {
            result.qc_manager = gr.getValue('u_qc_manager');
            result.plant_accountant = gr.getValue('u_plant_accountant');
            result.shipping_manager = gr.getValue('u_shipping_manager');
        }
        return result;
    },

    type: 'PlantManagerFetcher'
});
 
 
Client Script:
function onLoad() { 
var plantName = g_form.getValue('plant_name_field'); // Replace with the correct field name
if (plantName)
{
getManagersByPlant(plantName);
} }
function getManagersByPlant(plantName)
{
var ga = new GlideAjax('PlantManagerFetcher');
ga.addParam('sysparm_name', 'getManagersByPlant');
ga.addParam('plantName', plantName);
ga.getXMLAnswer(processResponse);
}
function processResponse(response)
{
var answer = response.responseXML.documentElement.getAttribute("answer");
var result = JSON.parse(answer);
if (result)
{ g_form.setValue('qc_manager_field', result.qc_manager); // Replace with the correct field name g_form.setValue('plant_accountant_field', result.plant_accountant); // Replace with the correct field name g_form.setValue('shipping_manager_field', result.shipping_manager); // Replace with the correct field name } else { g_form.addErrorMessage('No managers found for the selected plant.');
} }

View solution in original post

3 REPLIES 3

Arya123
Tera Expert

Hi @Alan42 

Please try and see the below code:

 

Script Include:

 

var PlantManagerFetcher = Class.create();
PlantManagerFetcher.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {

getManagersByPlant: function(plantName) {
        var result = {};
        var gr = new GlideRecord('u_rwi_plants');

        // Use the correct column names
        gr.addQuery('u_plant', plantName);
        gr.query();

        if (gr.next()) {
            result.qc_manager = gr.getValue('u_qc_manager');
            result.plant_accountant = gr.getValue('u_plant_accountant');
            result.shipping_manager = gr.getValue('u_shipping_manager');
        }
        return result;
    },

    type: 'PlantManagerFetcher'
});
 
 
Client Script:
function onLoad() { 
var plantName = g_form.getValue('plant_name_field'); // Replace with the correct field name
if (plantName)
{
getManagersByPlant(plantName);
} }
function getManagersByPlant(plantName)
{
var ga = new GlideAjax('PlantManagerFetcher');
ga.addParam('sysparm_name', 'getManagersByPlant');
ga.addParam('plantName', plantName);
ga.getXMLAnswer(processResponse);
}
function processResponse(response)
{
var answer = response.responseXML.documentElement.getAttribute("answer");
var result = JSON.parse(answer);
if (result)
{ g_form.setValue('qc_manager_field', result.qc_manager); // Replace with the correct field name g_form.setValue('plant_accountant_field', result.plant_accountant); // Replace with the correct field name g_form.setValue('shipping_manager_field', result.shipping_manager); // Replace with the correct field name } else { g_form.addErrorMessage('No managers found for the selected plant.');
} }

Thank you.   Your suggested changes worked.  Here are the edits that finally did the trick.        

 

var PlantManagerFetcher = Class.create();
PlantManagerFetcher.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getManagersByPlant: function() {
var plantName = this.getParameter('sysparm_plantName');
var result = {};

var gr = new GlideRecord('u_rwi_plants');
gr.addQuery('u_plant', plantName);
gr.query();

if (gr.next()) {
result.qc_manager = gr.getValue('u_qc_manager');
result.plant_accountant = gr.getValue('u_plant_accountant');
result.shipping_manager = gr.getValue('u_shipping_manager');
}

return JSON.stringify(result);
},

type: 'PlantManagerFetcher'
});

 

function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue === '') {
return;
}

console.log('Selected Plant:', newValue);

var ga = new GlideAjax('PlantManagerFetcher');
ga.addParam('sysparm_name', 'getManagersByPlant');
ga.addParam('sysparm_plantName', newValue); // Correct parameter name
ga.getXMLAnswer(function(response) {
console.log('Response:', response);
try {
var managers = JSON.parse(response);
console.log('Parsed Managers:', managers);

// Update form fields with the fetched manager data
g_form.setValue('u_qc_manager', managers.qc_manager || '');
g_form.setValue('u_plant_accountant', managers.plant_accountant || '');
g_form.setValue('u_shipping_manager', managers.shipping_manager || '');
console.log('QC Manager:', g_form.getValue('u_qc_manager'));
console.log('Plant Accountant:', g_form.getValue('u_plant_accountant'));
console.log('Shipping Manager:', g_form.getValue('u_shipping_manager'));
} catch (e) {
console.error('Error parsing JSON response:', e);
}
});
}

Bhavani Shankar
Tera Guru

Hi @Alan42,

 

Like @Arya123 suggests, client callable script include must extend AbstractAjaxProcessor which is missing from your script include. Please try to modify your script to extend AbstractAjaxProcessor.

 

Regards,

Bhavani Shankar