Requirement to check values of category and subcategory in assignment data lookup

AKASH BANERJEE
Mega Guru

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.

1 ACCEPTED SOLUTION

AnubhavRitolia
Mega Sage
Mega Sage

Hi @AKASH BANERJEE 

 

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.

 

 

 

Please mark this as correct answer and helpful if it resolved, or mark this helpful if this help you to reach towards solution.

Thanks
Anubhav Ritolia
ServiceNow Rising Star 2023

View solution in original post

15 REPLIES 15

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

Yes its a catalog client script

variables are current_category and current_subcategory

both are single line text.

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.

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');
}
}

 

Can you paste the screenshot of the script