- Post History
- Subscribe to RSS Feed
- Mark as New
- Mark as Read
- Bookmark
- Subscribe
- Printer Friendly Page
- Report Inappropriate Content
on 09-15-2021 07:54 AM
Hi Guys,
I recently had below requirement and it took quite a while to achieve this, so thought to document the steps.
Hope it helps.
Issue Description:
Consider you have 2 reference fields. Depending on the value selected on the first reference field the values populated in the second reference field should change.
For this, I'll take the example of Article form wherein we have
- Knowledge Base (Reference) field.
- Custom added group field referencing to sys_user_group.
Depending on the KB Selection we have to populate the values in the custom field 'Group'.
eg. If KB is Human Resource, then only groups of Type 'HR' should be populated.
High Level Approach:
- Create a 'Client Callable Script Include' with two methods.
- Method 1 to fetch the KB name & set the Group Type.
- Method 2 to fetch the Type from our reference qualifier.
- Create a onChange Client script on the KB field.
- Add advanced qualifier for the Custom group field.
Solution:
1. Script Include
var getGroups = Class.create();
getGroups.prototype = Object.extendsObject(AbstractAjaxProcessor, {
fetchID: function() {
var kbname = this.getParameter('sysparm_val');
var id = '';
if (kbname.toString() == 'Knowledge')
id = "typeLIKE8c81e8de87e2301080f299383cbb351d";
else if (kbname.toString() == 'Human Resources General Knowledge')
id = "typeLIKE7a5370019f22120047a2d126c42e705c";
else if (kbname.toString() == 'IT')
id = "typeLIKE1cb8ab9bff500200158bffffffffff62";
else
id = "typeLIKE7a5370019f22120047a2d126c42e705c";
//this is important
var session = gs.getSession();
session.putClientData('grouptype', id);
},
//below function will be used to call from the reference qualifier
fetchGrps: function() {
var val = session.getClientData('grouptype');
return val;
},
type: 'getGroups'
});
2. onChange Client Script (on KB field)
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
//Type appropriate comment here, and begin script below
var ga = new GlideAjax("getGroups");
ga.addParam('sysparm_name', 'fetchID');
ga.addParam('sysparm_val', g_form.getDisplayBox('kb_knowledge_base').value);
ga.getXML(result);
function result(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
}
}
3. Add Reference qualifier on the Custom group field
javascript: new getGroups().fetchGrps();
You'll have to tweak the code as per your requirements.
Also, if there is a better approach to achieve do let us know in the comments.
If you find it helpful, please mark helpful/bookmark it.
Thanks
- 5,606 Views