Adding a valuelist from the Solution Model

Hi,

I need to create a valuelist based on a query, and assign it to an existing field.
I only have the query for the valuelist to begin with, say,

_sql = ‘select a, b from tableX’
Then i use,
var _dataset = databaseManager.getDataSetByQuery(_database,_sql,null,-1);

then I need to create the valuelist from the dataset,
application.setValueListItems(_valuelistName,_dataset);

How do I, set this newly created valuelist to an existing field?

Regards,
Hareendra

hareendra:
then I need to create the valuelist from the dataset,
application.setValueListItems(_valuelistName,_dataset);

First of all, this function doesn’t create a valuelist for you. It just assigns this dataset to an existing valuelist.
This almost includes the answer to the next question:

hareendra:
How do I, set this newly created valuelist to an existing field?

just create an empty valuelist with the name of ‘_valuelistName’.
As this valuelist does exist (but empty) you can assign this to any field you like.
Please take into account that every valuelist is created for the first type of column it will be loaded for.(so nothing new)

To accomplish the above no solution model is involved at all, still: if you need/want to use the solutionmodel > you can, but you’ll need to use other functions, which you’ll find at the ‘SolutionModel’ node in the Solution Explorer.

Hope this helps!

Hi Hareendra,

with the solutionModel you can create a field or get it with JSForm getField() method, and then used its valueList property, like this:

var frm = solutionModel.getForm("myForm");
var fld = frm.getField("myField");
var vl = solutionModel.getValueList("myValueList");
fld.valueList = vl;

Hi mboegem,

I didnt quite understand what you said.

“just create an empty valuelist with the name of ‘_valuelistName’.
As this valuelist does exist (but empty) you can assign this to any field you like.”

How do you create a valuelist in the code? Dont you mean you have to first create the valuelist (an empty one) and then assign data to it in the code?
Then again, how do you assign the valuelist to a field without the solution model?
Can you please give a code example

Thank you
Regards,
Hareendra

hi Hareendra,

Maybe I should have started off in a different way:
Your actual question is: ‘Adding a valuelist from the Solution Model’, but the examplecode you included isn’t specific solutionModel.
So: are you using an existing form or is this created from the solutionModel?

If it’s a solutionModel, you should create the valuelist with ```
solutionModel.newValueList(name, number type)

Then use the code you included in your question and wrap it up by using Patrick's code example.

On the other hand: if you're referring to an existing form, you could just create a valuelist, without any settings.
Attach this one to the (also existing) field and use your code...

Hope this will clarify the response you had from Patrick and myself.

Hi,

I tried using the code you suggested, but I get an error.

I use,
solutionModel.newValueList(‘valuelist_name’,VALUELIST.DATABASE_VALUES);

but I get the error,
ReferenceError: “VALUELIST” is not defined.

Regards,
Hareendra

Hi Hareendra,

it should be:

solutionModel.newValueList('valuelist_name', SM_VALUELIST.DATABASE_VALUES);

Patrick

Hi Again,

I’m sorry but i just cant seem to get it to work. Thanks for the tip, that issue is gone, but the solution model does not load the list dynamically.
So I tried creating a value list beforehand, and then assigning it to a field through the solution model at run time.

Somehow this does not work. Here’s my code;

history.removeForm(‘frm_main’);
var _form = solutionModel.getForm(‘frm_main’);
var _field = _form.getField(‘field1’);

var _sql = ‘select cola, colb from table1’;
var _dataset = databaseManager.getDataSetByQuery(‘db_test’,_sql,null,10);
application.setValueListItems(‘lst_list1’,_dataset);

_field.valuelist = solutionModel.getValueList(‘lst_list1’);
_field.displayType = SM_DISPLAYTYPE.COMBOBOX;

at times i want to remove the valuelist from the field so i do;
_field.displayType = SM_DISPLAYTYPE.TEXT_FIELD;
_field.valuelist = null;

Unfortunately this does not work. Does anyone have an idea?

Thanks
Regards
Hareendra

First of all you are mixing runtime stuff with solutionModel (if i just look at your code as one thing)

This is runtime:

var _sql = ‘select cola, colb from table1’;
var _dataset = databaseManager.getDataSetByQuery(‘db_test’,_sql,null,10);
application.setValueListItems(‘lst_list1’,_dataset);

this is solution model

_field.valuelist = solutionModel.getValueList(‘lst_list1’);
_field.displayType = SM_DISPLAYTYPE.COMBOBOX;

try to avoid mixing those stuff, will only make stuff later harder to read and understand.

If you do this:

_field.displayType = SM_DISPLAYTYPE.TEXT_FIELD;
_field.valuelist = null;

are you sure that right hefore that you do:

var removed= history.removeForm(‘frm_main’);

removed == true??

Hi,
The solution model is acting strangely. Some times the value list appears and sometimes it appears when i dont want it to. If there’s nothing wrong with the coding why isn’t it functioning properly? Here’s the code again

history.removeForm(‘frm_main’);
var _form = solutionModel.getForm(‘frm_main’);

var _field = _form.getField(‘field1’);

//check if a valuelist is assigned to the field. if so retrieve the list and assign it to the filed
if(forms.frm_main.foundset.valuelist_id != null){
var _sql = ‘select cola, colb from table1’;
var _dataset = databaseManager.getDataSetByQuery(‘db_test’,_sql,null,10);
application.setValueListItems(‘lst_list1’,_dataset);

_field.valuelist = solutionModel.getValueList(‘lst_list1’);
_field.displayType = SM_DISPLAYTYPE.COMBOBOX;

}
else{
_field.displayType = SM_DISPLAYTYPE.TEXT_FIELD;
_field.valuelist = null
}

Please reread my previous post

“Dont mix solution model with runtime…”

And you are still doing that
In 4.1.4 we made a check for this and if you would run the under lying code then you would get an exception in your developer that you have stale form(s)
And with stale forms we mean, you have a UI form instance that is not in line with a Solution Model form blueprint. Because you altered the form blueprint when there is an UI instance of that same form.

But look at your code you do this:

history.removeForm('frm_main');
var _form = solutionModel.getForm('frm_main');

var _field = _form.getField('field1'); 

//check if a valuelist is assigned to the field. if so retrieve the list and assign it to the filed
if(forms.frm_main.foundset.valuelist_id != null){
var _sql = 'select cola, colb from table1';
var _dataset = databaseManager.getDataSetByQuery('db_test',_sql,null,10);
application.setValueListItems('lst_list1',_dataset);

_field.valuelist = solutionModel.getValueList('lst_list1');
_field.displayType = SM_DISPLAYTYPE.COMBOBOX; 

}
else{
_field.displayType = SM_DISPLAYTYPE.TEXT_FIELD;
_field.valuelist = null
}

The correct code to do this is this:

var comboBox = false;
// First check the live form instance (forms.frm_main.xxx code!) 
// if a valuelist is assigned to the field. if so retrieve the list and assign it to the filed
if(forms.frm_main.foundset.valuelist_id != null){
var _sql = 'select cola, colb from table1';
var _dataset = databaseManager.getDataSetByQuery('db_test',_sql,null,10);
application.setValueListItems('lst_list1',_dataset);
comboBox = true;
}

// Then really check if the form is removed.
if (history.removeForm('frm_main'))
{
	var _form = solutionModel.getForm('frm_main');

	var _field = _form.getField('field1'); 

	if (comboBox)
	{
	  _field.valuelist = solutionModel.getValueList('lst_list1');
	  _field.displayType = SM_DISPLAYTYPE.COMBOBOX; 

	}
	else{
	  _field.displayType = SM_DISPLAYTYPE.TEXT_FIELD;
	  _field.valuelist = null
	}
}
else
{
    application.output("form couldnt be removed, is it still visible!?");
}

This code is correctly. Problem is that that you try to alter stuff on basis of some data in a foundset
And after this code the frm_main is reconstructor and will get a foundset again. But i dont know if that is
also the foundset you where testing against before yo called removeForm().
So it could be that you have to rememeber the foundset:

var fs = frm_main.foundset

right before the first if and then as the last line in the above code

froms.frm_main.controller.loadRecords(fs)

Thank you very much. It works now!

Regards
Hareendra