Reputation: 11
I've added new fields to the InventNonConformanceTable form, by adding them to the Overview and ProblemInformation data groups, and they show up on the form, but whenever I try and reference them with formControlStr compilation fails with "Form control 'x' is not found in InventNonConformanceTable.
I can use formRun.design().controlName('x') at runtime so the controls are definitely being added.
This also only happens with existing form group controls, if I add a new form group control with either of those field groups as data groups I can create my event handlers as usual.
Upvotes: 1
Views: 2344
Reputation: 11
I forgot about this until a coworker just ran into the same issue, coincidentailly again with InventNonConformanceTable. What I ended up doing was to register the event handlers at runtime when the form got initialized, the code looks something like this:
class InventNonConformanceTableFormHandler
{
/// <summary>
/// Registers event handlers at runtime to add lookups.
/// The event handlers have to be added at runtime due to build issues when calling formControlStr on new controls added to existing field groups
/// </summary>
/// <param name="sender">The sender</param>
/// <param name="e">The event args</param>
[FormEventHandler(formStr(InventNonConformanceTable), FormEventType::Initialized),
SuppressBPWarning('BPParameterNotUsed', 'Parameter required by the event interface')]
public static void InventNonConformanceTable_OnInitialized(xFormRun sender, FormEventArgs e)
{
//str extensionFieldCtrlStr = formControlStr(InventNonConformanceTable, ExtensionField);
str extensionFieldCtrlStr = 'ExtensionField';
FormRun formRun = sender;
FormDesign design = formRun.design();
FormStringControl extensionFieldCtrl = design.controlName(extensionFieldCtrlStr);
extensionFieldCtrl.OnLookup += eventhandler(InventNonConformanceTableFormHandler::ExtensionField_OnLookup);
}
// [FormControlEventHandler(formControlStr(InventNonConformanceTable, ExtensionField), FormControlEventType::Lookup)]
public static void ExtensionField_OnLookup(FormControl sender, FormControlEventArgs e)
{
// Lookup code goes here
// Cancel the super call so the system doesn't try and open multiple lookup forms
FormControlCancelableSuperEventArgs cancelableSuperEventArgs = e;
cancelableSuperEventArgs.CancelSuperCall();
}
}
Upvotes: 0
Reputation: 101
In my case I had to set the visibility of the field on the form according to some logic.
This can be achieved on the datasource field instead of working with formControlStr with the below logic.
This is even better as it will also prevent users from for example adding the field on the form through customizations.
FormDataSource purchLine_ds = formRun.dataSource(formDataSourceStr(PurchTable, PurchLine));
if(this.customFieldShouldBeVisible())
purchLine_ds.object(fieldNum(PurchLine, CustomFieldName)).visible(true);
else
purchLine_ds.object(fieldNum(PurchLine, CustomFieldName)).visible(false);
Upvotes: 0