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

AnubhavRitolia
Mega Sage
Mega Sage

Hi @AKASH BANERJEE 

 

You can write 2 Catalog Client Script onChange for Category and Subcategory fields.

 

There you need to pass call new Script Include and pass values of Category and Subcategory. 

This client callable Script Include will GlideRecord Assignment Group table and see for any record with match category and Subcategory. If found return 'Yes' else return 'No'.

On client script check the value returned from Script Include and than if value is 'Yes', just show Alert Pop - up with your message 'the category and subcategory is already associated to the group_name' and than using 'g_form.clearValue()' you can clear values of Category and subcategory variables.

 

 

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

Radhika Aluru
Tera Guru

Hi Akash,

 

You can implement this using a client script using GlideAjax and script include. you need to pass input category and subcategory values to script include and write a logic to query assignment data lookup table. return true if record exists and clear the values if the returned value is true.

refer below article for reference

 

https://developer.servicenow.com/print_page.do?release=quebec&category=null&identifier=c_GlideAjaxAP...

 

Saurav11
Kilo Patron
Kilo Patron

Hello,

 

You can write a Onchange client script on the subcategory variable:-

 

and then glide  to the dl_u_assignment_list and the code would look something like below:-

 

var gr=new GlideRecord('dl_u_assignment_list');
gr.addEncodedQuery('category='+categoryvaribalename+'^subcategory='+subcategoryvaribalename);
gr.query();
if(gr.next())
{
g_form.addInfoMessage('There is assignment group for this  combination');
}

 

Please mark my answer as correct as based on Impact.

I am using the below code onsubmit client script. But it is clearing the values of category and subcategory in ritm and sctask variables irrespective of the input category and subcategory record present in Assignment data lookup table:

 

function onSubmit() {
//Type appropriate comment here, and begin script below
var gr=new GlideRecord('dl_u_assignment');
gr.addEncodedQuery('category='+current_category+'^subcategory='+current_subcategory);
gr.query();
if(gr.next())
{
g_form.clearValue('current_category');
g_form.clearValue('current_subcategory');
alert("Please enter a new category or subcategory as it is already associated to another group");
}

}