addOption.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-13-2017 12:43 PM
Just learn one side effect of using g_form.addOption it will trigger the onchange event on the client form.... So you need to check at the beginning you event outcome. so I added...
if (isLoading) {
return;
}
if(isLoading)
return;
clearOptions();
if(newValue == '')
return;
else
{
do by of populating the subcategory choice fields..
}
I believe GlideAjax is getting fire before the function call clear where populating the choice field gets done first and then getting clear out by the clear function...
Is there a way to sync this..?
- Labels:
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-13-2017 10:39 PM
Hi Michael,
Would you mind sharing a little bit more of what you're trying to accomplish?
In case it helps, the reason why OOB onChange client scripts have a validation like the one below is because the onChange will get triggered while the form is loading. If you have the below condition within your onChange client script and the addOption occurs within an OnLoad client script, you then will be safe.
if (isLoading || newValue === '') {
return;
}
Thanks,
Berny
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-14-2017 07:41 AM
Let me see if I understand you...... this check only applies when loading the form? correct so I dont needed it because the problem happens during on onchange.
I have to fields category and subcategory from a customize table which are are part of an application. During loading of the page the category fields gets loaded dynamically with the choice values..... that work as expected.... get all the unique values from the table..... when I click on one of the category options, the subcategory is a depended choice field to Category and it gets populated it with the options that pertain to that category.... sometimes it work and sometime it does not I believe the addoption trigger the form so I added those lines but did not work.... it looks like the glideajax gets call before I clear the previous choice values of the subcategory...... this is the code
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading) return;
if(newValue == '') return;
else
{
clearOptions();
var getSubcategoryFields = new GlideAjax('SubCategoryFields');
getSubcategoryFields.addParam('sysparm_name','subCategoryFields');
getSubcategoryFields.addParam('sysparm_category',g_form.getValue('categoryexample'));
getSubcategoryFields.getXML(populateFields);
}
function populateFields(response){
var answer = response.responseXML.documentElement.getAttribute("answer");
answer = answer.evalJSON();
if (g_form.getValue('categoryexample') != 'fruits' &&
g_form.getValue('categoryexample') != 'vegetables')
{
var i;
for (i = 0; i < answer.length; i++)
{
g_form.addOption('subcategoryreference',answer[i].subCategory,answer[i].subCategory);
}
}
}
function clearOptions(){
g_form.clearOptions('subcategoryreference');
//g_form.clearValue('subcategoryreference');
}
}
var SubCategoryFields = Class.create();
SubCategoryFields.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {
subCategoryFields: function() {
var array = [];
var gr = new GlideRecord('u_categories');
gr.addQuery('u_category',this.getParameter('sysparm_category'));
gr.query();
while(gr.next()){
var obj = {};
obj.subCategory = gr.getValue('u_subcategory');
array.push(obj);
}
return JSON.stringify(array);
},
type: 'SubCategoryFields'
});
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-14-2017 10:45 AM
Hi Michael,
Thanks for sharing the use case you're trying to satisfy. That can be solved in a much simple way using Reference Qualifiers.
The following should be helpful:
Reference Qualifiers - ServiceNow Wiki
Query Regarding Advance Reference Qualifiers
Thanks,
Berny