- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-02-2022 11:03 AM
We have a requirement in which user will fill values in category and subcategory variable present in catalog form.
We will check the value filled by user for category and subcategory with the assignment data lookup table(Note Assignment data lookup table have category, subcategory and group). After checking if any category and subcategory is associated to any assignment group, we have to show it in catalog form and give a warning like 'the category and subcategory is already associated to the group_name'. After that we have to clear the category and subcategory field.
Please let mw know if anyone having any idea on how we can implement this.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-03-2022 07:29 AM
Please try below:
Client Script onChange of Subcategory field:
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var category=g_form.getValue('current_category');
var subcategory=g_form.getValue('current_subcategory');
var ga = new GlideAjax('MatchCategorySubcategory');
ga.addParam('sysparm_name','assignmentRuleExistence');
ga.addParam('sysparm_category', category);
ga.addParam('sysparm_subcategory', subcategory);
ga.getXML(matchingAssignRule);
// the callback function for returning the result from the server-side code
function matchingAssignRule(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
if(answer) {
alert("the category and subcategory is already associated to the group_name");
g_form.clearValue('current_category');
g_form.clearValue('current_subcategory');
}
}
}
Client Callable Script Include:
var MatchCategorySubcategory= Class.create();
MatchCategorySubcategory.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {
assignmentRuleExistence: function() {
var cat= this.getParameter("sysparm_category");
var subCat = this.getParameter("sysparm_subcategory");
var assignRule = new GlideRecord('dl_u_assignment');
assignRule.addQuery('category',cat);
assignRule.addQuery('subcategory',subCat);
assignRule.query();
if(assignRule.next())
{
return true;
}
return false;
},
type: 'MatchCategorySubcategory'
});
NOTE: Catalog variables name may differ based on variable names on your instance and check spelling mistakes if any by mistake.
GlideRecord() is a server side API so we cannot use it on client script.
Thanks
Anubhav Ritolia
ServiceNow Rising Star 2023
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-03-2022 04:40 AM - edited ‎11-03-2022 04:44 AM
Hello,
have you written the above script on the catalog item? is it a catalog client script?
If yes what is the name of the category and subcategory variable?
Also I would suggest you to change the above to a on change catalog client script as well.
var category=g_form.getValue(categoryvariable);
var subcategory=g_form.getValue(subcategoryvariable);
var gr=new GlideRecord('dl_u_assignment');
gr.addEncodedQuery('category='+category+'^subcategory='+subcategory);
gr.query();
if(gr.next())
{
g_form.addInfoMessage('There is assignment group for this combination');
}
Also what type of variables are category and subcategory is it a lookup select box?
Let me know on the above
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-03-2022 04:49 AM
Yes its a catalog client script
variables are current_category and current_subcategory
both are single line text.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-03-2022 04:59 AM
Hello
Please try the below code once:-
var category=g_form.getValue(categoryvariable);
var subcategory=g_form.getValue(subcategoryvariable);
var gr=new GlideRecord('dl_u_assignment');
gr.addQuery('category',category);
gr.addQuery('subcategory',subcategory);
gr.query();
if(gr.next())
{
g_form.clearValue('current_category');
g_form.clearValue('current_subcategory');
g_form.addInfoMessage('There is assignment group for this combination');
}
Please mark my answer as correct based on Impact.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-03-2022 06:47 AM
The above script is not working it is clearing the value but for ritm, i have changed the script to onchange:
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
//Type appropriate comment here, and begin script below
var category=g_form.getValue(categoryvariable);
var subcategory=g_form.getValue(subcategoryvariable);
var gr=new GlideRecord('dl_u_assignment');
gr.addQuery('category',category);
gr.addQuery('subcategory',subcategory);
gr.query();
if(gr.next())
{
g_form.clearValue('current_category');
g_form.clearValue('current_subcategory');
g_form.addInfoMessage('There is assignment group for this combination');
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-03-2022 07:11 AM
Can you paste the screenshot of the script