I want to dynamically add a foreign key relation to the datasource in my form so that I get the values from that foreign key as a dropdown in one of my form fields.
So there is field A, which is of type enum and has 2 options:- Text(default) and List. There is another field B whose value depends on value selected in field A. Whenever user selects List in field A, a dropdown should occur in field B whereas when Text is selected in field A, then field B should become a simple text field.
I implemented this manually by adding a relation explicitly on the form datasource and it works fine when I select List on field A ( a dropdown appears which contains list of values from the foreign key) but when I select Text again on field A the dropdown still appears on field B which means the control(relation) did not get cleared which is not what I want. So to do it dynamically, I am using modify() method for that form field (field A).
Here is a snippet of my code:-
public boolean modified()
{
boolean ret;
ret = super();
if(Field_A::List)
{
QueryBuildDataSource qbds;
RelatedTable relatedtableobject;
FormDataSource formDataSource = FormDataSourceTable.dataSource();
qbds = formDataSource.query().dataSourceTable(tablenum(FormDataSourceTable));
qbds.addSelectionField(fieldnum(FormDataSourceTable, Field_A));
qbds = qbds.addDataSource(tablenum(RelatedTable));
qbds.relations(true);
qbds.addForeignkeyRelation('FormDataSourceTable','RelatedTable');
qbds.addDynalink(fieldnum(FormDataSourceTable,ForeignKeyField), relatedtableobject, fieldnum(RelatedTable, Field_B));
}
return ret;
}
I am using querybuilddatasource and dynalink to create relations. FormDataSourceTable is the data source table in my form and RelatedTable is the table which I need to add relation to with my form datasource for referencing foreign key.
But I am getting below error:-
The specified data source cannot be found.
on this line:-
qbds.addForeignkeyRelation('FormDataSourceTable','RelatedTable');
Basically I want the relation to be applied dynamically only when List is selected and for Text the relation should be cleared. Can anyone suggest a better way to do this since I am new to x++?