.addOption for catalog variable (lookup select box) in case ref qual not satisfied - BUT only on first change!!!

Zod
Giga Guru

Hey,

SO strange. I need help here!

I have a scoped variable set up as a lookup select box and link to a custom table that contains similar entries like sys_choice ... to be used for the lookup.

I'm using a ref qual condition that works fine when choosing valid entries from the table within a catalog item ... All fine!

No I need for certain cases (depending on a change mad in an other variable) to ADD an OPTION that is NOT part of the table I'm using for my choices.

SO a have a catalog client script on change, that sets the ADDITIONAL option (not matching the ref qualifier) in case required (and I also found an way to remove it again in case the other value changes again ... ).

All works fine ... just the first time the option is to be set, it disappears again! Looks link there would be some other setting redoing what was just done in the catalg client script. I added an alert at the end of the client script and when it pops up the correct value is still there. Just overwritten with blank afterwards.

I 5 times checked ALL client scripts ... none of them can be responsible for overwriting.

And anyhow .. when changing the variable causing it .. back and force it works .. allways ... its JUST the first time this does not work ... anyone with an idea? Look more like a bug to me ... 

1 ACCEPTED SOLUTION

Table?! We are still talking about variables .. 

BUT I found it myself. Its the reference qualifier I'm using to define what kind of values are valid on the lookup select box. This technically is most probably nothing else than a client script. As the values initially are set via the change on an other variable - this will be "loaded" afterwards. So I need to remove the ref qualifyer there and buid in the logic manually within a client script.

Anyhow ! Thnx for your effort!

View solution in original post

29 REPLIES 29

The Ajax call is made in the onchange client script?

Yes. It's in a catalog item and scoped ... no other way to get the CI values into the variables via an other way .. 

So, i have tried calling the GlideAjax and adding the option in onChange Client script. This adds the option to the dropdown list

Following is an example 

function onChange(control, oldValue, newValue, isLoading) {
   if (isLoading || newValue == '') {
      return;
   }
	var ga = new GlideAjax("Utils");
	ga.addParam ("sysparm_name", "getOptions");
	ga.getXMLAnswer(response);

	function response(answer){
		g_form.addOption('addoption_catalog', answer, answer);
	}
}

the on change run on an other variable (A) change ... and will set the option to variable B.

 

Will try to post my code here in some min.

function onChange(control, oldValue, newValue, isLoading) {
	if (isLoading || newValue == '') {
		return;
	}
	var yyyClass = g_form.getValue('xxx_class');
	var ga = new GlideAjax('x_xxx.yyyGetAjaxRecord');
	ga.addParam('sysparm_name', 'getReference');
	ga.addParam('sysparm_obj_Id', newValue);
	ga.addParam('sysparm_obj_classId', yyyClass);
	ga.getXML(ResponseAjax);
	
}

function ResponseAjax(response){
	var answer = JSON.parse(response.responseXML.documentElement.getAttribute("answer"));
	var choiceTmp ='';
	var choiceisOk ='';
	
	// remove options that are non-standard
	choiceTmp = g_form.getValue('yyy_choice_tmp');
	g_form.removeOption('yyy_choice_tmp', choiceTmp);
	g_form.clearValue('yyy_choice_tmp');
	
	// check if value for optional external appearance  is standard sys choice
	choiceIsOk = answer.choiceIsOk;
	if (choiceIsOk == 'true') {
		g_form.setValue('xxx_variable_B',answer.choice); // sets the choices a ajax confornfirms its a standard value
	}
	else {  // no standard value in CI - add option and tmp variable to be able removing it again
		choiceTmp = answer.choice;  // non-standard - add option
		g_form.addOption('xxx_variable_B', choiceTmp , choiceTmp);
		g_form.setValue('xxx_variable_B', choiceTmp ); // tmp choice added
		g_form.setValue('yyy_choice_tmp', choiceTmp ); // to keep new option to be able to remove it again
	}
}

 

But as said before .. all works fine here. The issue seems to be outside. The standard choice (either none or if none not set the first possible choice) is set automatically the first time this field is getting filled ... . Just AFTER my script has filled it.