Skip to main content
Dynamics 365 Community / Forums / Finance forum / How to access the Dyna...
Finance forum
Suggested answer

How to access the Dynamic Control and use OnModified event on DirPartyQuickCreateForm

editSubscribe (0) ShareShare
ReportReport
Posted on by 68
Hi, for my customization, I have added three customized fields to the DirPartyQuickCreateForm form by dragging them into the quickCreateDetails field groups of the CustTable.
 
//
From the figure above, my user requirement is to set if TIN field is filled in, then the Business registration field needs to be changed to a non-mandatory field. If the business registration field is filled in, the TIN field will become non-mandatory.
 
However, I cannot use formControlEventHandler for my fields since they are added as the dynamic control. The system will show me an error message like /Formcontrol DynamicDetail_ads_TIN is not found on the DirPartyQuickCreateForm form./

I write the event handler this way:
[FormControlEventHandler(formControlStr(DirPartyQuickCreateForm, DynamicDetail_ads_TIN), FormControlEventType::Modified)]
    public static void DynamicDetail_ads_TIN_OnModified(FormControl sender, FormControlEventArgs e)
    {
    }

Hence, my question is what should I use to access the dynamic control on DirPartyQuickCreateForm?
  • Yue Zhen Profile Picture
    Yue Zhen 68 on at
    How to access the Dynamic Control and use OnModified event on DirPartyQuickCreateForm
    Hi Martin, thank you for your prompt explanation. I think I get what you are trying to pass to me now. I will try and update it here later. Thank you for your guidance.
  • Martin Dráb Profile Picture
    Martin Dráb 222,570 Super User on at
    How to access the Dynamic Control and use OnModified event on DirPartyQuickCreateForm
    If you're that the name of the control will always be the same, then you can find it by name, but hard-coding the name isn't a good approach. Instead, you something like strFmt('%1_%2', formControlStr(DirPartyQuickCreateForm, DynamicDetail), fieldStr(YourTable, ads_TIN)). This will make sure that the group and the field exist and it'll maintain cross-references.
     
    I already showed you how to a register an event handler method: control.OnModified += eventHandler(...). The parameter of eventHandler() is either an instance method (e.g. this.myMethod or anotherObject.myMethod) or a static method (MyClass::myMethod).
     
    You can see an example in CostStatement form, for instance.
  • Yue Zhen Profile Picture
    Yue Zhen 68 on at
    How to access the Dynamic Control and use OnModified event on DirPartyQuickCreateForm
    Hi Martin, I think I am struggling with everything haha. I don't understand why do we need to iterate the control in DynamicHeader, because from my current coding, I can get the control by using sender.design().controlName("DyanmicDetail_ads_TIN") like this.
     
    final class DirPartyQuickCreateFrom_Form_Handler
    {
        /// <summary>
        ///
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        [FormEventHandler(formStr(DirPartyQuickCreateForm), FormEventType::Initialized)]
        public static void DirPartyQuickCreateForm_OnInitialized(xFormRun sender, FormEventArgs e)
        {
            ads_ConsoEInvoiceParameters consoParameters;
    
            FormCheckBoxControl consoCB = sender.design().controlName("DynamicDetail_ads_ConsolidateEInvCheckbox");
            FormStringControl tin = sender.design().controlName("DynamicDetail_ads_TIN");
            FormStringControl ic = sender.design().controlName("DynamicDetail_IdentificationNumber");
    
            ic.label("Business registration/ identification/ passport number ");
            consoCB.autoDeclaration(true);
            
            select * from consoParameters;
    
            if(consoParameters.ads_ConsolidateEInvCheckbox == 1)
            {
                consoCB.checked(true);
            }
            else
            {
                consoCB.checked(false);
                tin.mandatory(true);
                ic.mandatory(true);
            }
            
            Info(strFmt("%1", consoCB.value()));
        }   
    
    
    }
    I am struggling on how to use an OnModified event handler on my formStringControl for the TIN and IC
  • Martin Dráb Profile Picture
    Martin Dráb 222,570 Super User on at
    How to access the Dynamic Control and use OnModified event on DirPartyQuickCreateForm
    Yen, you didn't explain which part you're struggling with, so I'm assuming it's the first one. Code like this can iterate the controls added at runtime:
    for (int controlNo = 1; controlNo <= DynamicHeader.controlCount(); controlNo++)
    {
        FormControl control = DynamicHeader.controlNum(controlNo);
    }
     
     
     
  • Andre Arnaud de Calavon Profile Picture
    Andre Arnaud de Cal... 283,045 Super User on at
    How to access the Dynamic Control and use OnModified event on DirPartyQuickCreateForm
    Hi Yue,
     
    Are these fields standard or part of a customization? Is the Business registration field mandatory as table property? If the field is not mandatory, you can just add the field and add coding when validating the CustTable record.
  • Yue Zhen Profile Picture
    Yue Zhen 68 on at
    How to access the Dynamic Control and use OnModified event on DirPartyQuickCreateForm
    Hi Layan,
     
    I add the field by dragging the ads_TIN, ads_SSTRegistrationNum, IdentificationNum and the checkbox into a field group of the CustTable, named quickCreateDetails. So the form control is not manually added in the form design. Hence, I cannot use the FormControlEventHandler to add my additional coding logic. Furthermore, I will need to write the FormStringControl as below to access the formControl.

    FormStringControl tin = sender.formRun().design().controlName("DynamicDetail_ads_TIN");
  • Layan Jwei Profile Picture
    Layan Jwei 2,785 Super User on at
    How to access the Dynamic Control and use OnModified event on DirPartyQuickCreateForm
    Hi Yue,
     
    You meen you added form controls that are not bound to any datasource? I mean you added the control manually to the form without attaching it to datasource? If this is the case, then I think you should be able to use event handlers, just go to the control and copy event handler, but u can't access the control directly by it's name inside the handler method, you will need sth like this
     
    FormStringControl control = sender.formRun().design().controlName(formControlStr(FormName, YourControl)) as FormStringControl
     
    Or did you add the controls by code?
     
    Also are u getting the error you mentioned without writing anything inside the method?
     
    Thanks,
    Layan Jweihan
  • Yue Zhen Profile Picture
    Yue Zhen 68 on at
    How to access the Dynamic Control and use OnModified event on DirPartyQuickCreateForm
    Hi Sir Martin, can you give me some further explanation on how to get the reference of the control?
  • Suggested answer
    Martin Dráb Profile Picture
    Martin Dráb 222,570 Super User on at
    How to access the Dynamic Control and use OnModified event on DirPartyQuickCreateForm
    Your code can't work, because there is no such a control when you compile your code. It's created only later, at runtime, and only then you can subscribe to its events.
     
    You'll need to get a reference of the control (e.g. by iterating controls in DynamicHeader and checking the table/field ID) and then add an event handler at runtime by control.OnModified += eventHandler(...).

Helpful resources

Quick Links

What Motivates a Super User?

We know many of you visit the Dynamics 365 Community and Power Platform…

Demystifying Copilot with Georg Glantschnig…

Industry experts answer burning questions directly from our amazing Community…

Setting Up Knowledge Sources for Copilot…

Look at how configuring a comprehensive knowledge base is crucial…

Leaderboard

#1
Andre Arnaud de Calavon Profile Picture

Andre Arnaud de Cal... 283,045 Super User

#2
Martin Dráb Profile Picture

Martin Dráb 222,570 Super User

#3
nmaenpaa Profile Picture

nmaenpaa 101,138

Product updates

Dynamics 365 release plans