Help me with onchange catalog client script based on list collector field.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-08-2024 02:47 AM
Hi all,
Please help me with the below requirement.
We have a variable named "Applications"
Based on(Applications) List collector variable values selected by the user which are being pulled from the custom table, in the custom table for that application record there is a check box field named (procurement required ?)if procurement required is marked as true then display an error message in service catalog servicenow and display an error message.
Stating that "Admin access is required for selecting this particluar application".
Below is the code i used.
Onchange client script
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}var selectedValues = g_form.getValue('application').split(',');
if (selectedValues.length === 0) {
return;
}
var ga = new GlideAjax('CheckCheckboxField');
ga.addParam('sysparm_name', 'checkCheckboxField');
ga.addParam('selectedValues', selectedValues.join(','));
ga.getXMLAnswer(function(response) {
var result = JSON.parse(response);
if (!result.success) {
g_form.addErrorMessage('Admin access is required for selecting this particular application');
}
});
}
Script Includes
var CheckCheckboxField = Class.create();
CheckCheckboxField.prototype = Object.extendsObject(AbstractAjaxProcessor, {
checkCheckboxField: function() {
var result = {};
result.success = true;
var selectedValues = this.getParameter('selectedValues').split(',');
var gr = new GlideRecord('u_applications');
gr.addQuery('sys_id', 'IN', selectedValues);
gr.addQuery('u_procurement_required', true);
gr.query();
if (gr.hasNext()) {
result.success = false;
}
return JSON.stringify(result);
},
type: 'CheckCheckboxField'
});
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-08-2024 03:12 AM
Hi @Balaram7 ,
I believe the requirement can achieved in a different way.
Instead of showing the error message, filter the records using advance reference qualifier for the agents to show only those records with Procurement is false.
If the loggedIn user is Admin, then return all the records of the custom table.
Hope this helps.
Regards,
Najmuddin.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-08-2024 03:30 AM
Hi @Balaram7 ,
below are the issue that in your script
in client script:
ga.addParam('selectedValues', selectedValues.join(','));
it should be
ga.addParam('sysparm_selectedValues', selectedValues.join(','));
in script incldue
var selectedValues = this.getParameter('selectedValues').split(',');
needs to be
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}var selectedValues = g_form.getValue('application').split(',');
if (selectedValues.length === 0) {
return;
}
var ga = new GlideAjax('CheckCheckboxField');
ga.addParam('sysparm_name', 'checkCheckboxField');
ga.addParam('sysparm_selectedValues', selectedValues.join(','));
ga.getXMLAnswer(function(response) {
var result = JSON.parse(response);
if (!result.success) {
g_form.addErrorMessage('Admin access is required for selecting this particular application');
}
});
}
Script include:
var CheckCheckboxField = Class.create();
CheckCheckboxField.prototype = Object.extendsObject(AbstractAjaxProcessor, {
checkCheckboxField: function() {
var result = {};
result.success = true;
var selectedValues = this.getParameter('sysparm_selectedValues');
var gr = new GlideRecord('u_applications');
gr.addQuery('sys_id', 'IN', selectedValues);
gr.addQuery('u_procurement_required', true);
gr.query();
if (gr.hasNext()) {
result.success = false;
}
return JSON.stringify(result);
},
type: 'CheckCheckboxField'
});
Please mark helpful & correct answer if it's really worthy for you.
Thanks,
BK