I have been scratching my head on this one for a few days now, for what seemed like a simple requirement. Basically I need to be able add a Language field to an existing form through an extension that allows users to select multiple languages. The languages should be listed with their LanguageId and Description in the dropdown, and the user should be able to select as many as they need.
I followed examples from the two following sites and was able to come very close to being able to complete this:
Adding multi-select lookup field in System parameters form using form extension
HOW TO CREATE A MULTI-SELECT LOOKUP AND SAVE VALUES IN TABLE
There are a couple of issues with the solutions from those sites. The first issue is that you need to use a QueryBuildDataSource in order to populate the Language dropdown and the language description is surfaced through a data method rather than a field, and I have not been able to find any possible way to include the Description as the QBDS needs to use actual fields on the table. The second issue is that it appears that the RecId needs to be included in the query (which causes it to display in the dropdown as well). If the RecId is not included, the dropdown still actually works and will save the correct values to the form, but whenever you try to change the values in the dropdown at a later date it does not remember your selections and has the first item in the list selected by default.
In order for me to be able to get the above working as intended I would both need a way to include the Description in the dropdown, and also hide the RecId from being visible in the dropdown. I have included a sample of the code I have used so far if that is of any help.
[FormDataSourceEventHandler(formDataSourceStr(EcoResProductParameters, EcoResProductParameters), FormDataSourceEventType::QueryExecuted)]
public static void EcoResProductParameters_OnQueryExecuted(FormDataSource sender, FormDataSourceEventArgs e)
{
Object mySender = sender.formRun();
mySender.setLanguageLookupValues();
}
public void init()
{
next init();
this.initLanguageMultiSelectLookup();
}
private void initLanguageMultiSelectLookup()
{
Query languageQuery = new Query();
QueryBuildDataSource languageDataSource = languageQuery.addDataSource(tableNum(LanguageTable));
languageDataSource.addSelectionField(fieldNum(LanguageTable, LanguageId));
languageDataSource.addSelectionField(fieldNum(LanguageTable, RecId));
languageDataSource.addOrderByField(fieldNum(LanguageTable, LanguageId));
languageLookupControl = SysLookupMultiSelectCtrl::constructWithQuery(
this,
EcoResProductParameters_SPCLanguages,
languageQuery,
true);
}
public void setLanguageLookupValues()
{
container ids;
container recIds;
int i;
FormDataSource ecoresproductparameters_ds = this.dataSource(formDataSourceStr(EcoResProductParameters, EcoResProductParameters)) as FormDataSource;
EcoResProductParameters ecoResProductParameters = ecoresproductparameters_ds.cursor();
for(i=1; i<=conLen(str2con(ecoResProductParameters.SPCLanguages,";")); i )
{
recIds = LanguageTable::Find(conPeek(str2con(ecoResProductParameters.SPCLanguages,";"),i)).RecId;
ids = conPeek(str2con(ecoResProductParameters.SPCLanguages,";"),i);
}
languageLookupControl.set([recIds,ids]);
ids = conNull();
recIds = conNull();
}
[FormControlEventHandler(formControlStr(EcoResProductParameters, EcoResProductParameters_SPCLanguages), FormControlEventType::Modified)]
public static void EcoResProductParameters_SPCLanguages_OnModified(FormControl sender, FormControlEventArgs e)
{
FormDataSource ecoresproductparameters_ds = sender.FormRun().dataSource(formDataSourceStr(EcoResProductParameters, EcoResProductParameters)) as FormDataSource;
EcoResProductParameters ecoResProductParameters = ecoresproductparameters_ds.cursor();
FormStringControl languagesControl = sender as FormStringControl;
ttsbegin;
ecoResProductParameters.selectForUpdate(true);
ecoResProductParameters.SPCLanguages = languagesControl.text();
ecoResProductParameters.update();
ttscommit;
}
Failing being able to get it working as described above, it has also been suggested that I could create a Form Lookup to accomplish the same but I am not familiar with that so if you would be able to provide some guidance it would be appreciated. My Google skills have been failing me on trying to find any example solutions.