Skip to main content

Notifications

Dynamics 365 Community / Forums / Finance forum / How to set a default v...
Finance forum
Answered

How to set a default value to a checkbox form control

Posted on by 37

Hi

I am working on a task where I need to change the behaviour of a form control- OneTime (checkbox form control) exist on LogisticsPostalAddress. My requirement is when I open this form then the OneTime control must be checked bydefault & non-editable. 

For this I have write an OnIntialzed Event Handler on form to get this behaviour when form will first open.

I have write this code below: 

[FormEventHandler(formStr(LogisticsPostalAddress), FormEventType::Initialized)]
    public static void LogisticsPostalAddress_OnInitialized(xFormRun sender, FormEventArgs e)
    {
        FormRun element = sender as FormRun;
        FormControl oneTimeAddress = element.design().controlName(formControlStr(LogisticsPostalAddress,OneTime));
        oneTimeAddress = NoYes::Yes; // throwing type error
        oneTimeAddress.allowEdit(false);
    }

This code is working fine & make chechbox non-editable but not getting the idea how to set its value to checked as this control is not bound to any datasource & I have tried : 

oneTimeAddress = NoYES::YES, but that is obviously throwing type error. then how to set its value as its a form control type.

I am using the current version D365 F&O.

Thanks & Regards,

Dinesh

  • Dinesh Tyagi Profile Picture
    Dinesh Tyagi 37 on at
    RE: How to set a default value to a checkbox form control

    Hi Mohit,

    The issue gets resolved. It was some system Error.

    Thanks,

    Dinesh

  • Dinesh Tyagi Profile Picture
    Dinesh Tyagi 37 on at
    RE: How to set a default value to a checkbox form control

    Hi Mohit,

    I have wrote COC for updated control method but getting error on UI side when I clicked on MenuItemButton. The error is: Object Reference not set an instance of an object. Is this issue related to getting formRun. If yes, How to get the formRun in Class COC.

    [ExtensionOf(classStr(LogisticsPostalAddressFormEventHandler))]
    public static final class HSHTLogisticsPostalAddressFormEventHandlerClass_Extension
    {
        public static void updatedControls(FormRun _formRun)
        {
            next updatedControls(_formRun);
            
            FormCheckBoxControl oneTimeControl = _formRun.design().controlName(formControlStr(LogisticsPostalAddress, OneTime));
                
            if (oneTimeControl)
            {
                oneTimeControl.checked(true);
                oneTimeControl.allowEdit(false);
            }
    
        }
    
    }

    Thanks,

    Dinesh

  • Verified answer
    Mohit Rampal Profile Picture
    Mohit Rampal 12,540 Moderator on at
    RE: How to set a default value to a checkbox form control

    Yes, please create CoC or event handler on updatedControl method and change oneTime checkbox value back to 1. This should work but you need to test if its impacting any functionality specific to oneTime checkbox.

  • Dinesh Tyagi Profile Picture
    Dinesh Tyagi 37 on at
    RE: How to set a default value to a checkbox form control

    Hi Mohit,

    I have debug that code again & found that after that form init COC, this updatedControl method is being called which changing that checkbox value from 1 to 0 again.

    Now Do I need to put my code in UpdatedControl method & need to write COC for updatedControl method of this LogisticsPostalAddressFormEventHandler class as this method is subscribe to updatedControls event handler of LogisticspostalAddress form.

    Could you pls suggest something.

    Thanks,

    Dinesh

  • Mohit Rampal Profile Picture
    Mohit Rampal 12,540 Moderator on at
    RE: How to set a default value to a checkbox form control

    I have debugged and it gets executed line by line and value was changing from 1 to 0 in this method.

    Form init is called first and later updateControls is called. Can you provide more information on behavior, which line gets hit and how it's not executed.

  • Dinesh Tyagi Profile Picture
    Dinesh Tyagi 37 on at
    RE: How to set a default value to a checkbox form control

    Hi Bharani,

    I have tried in run method as well on form but its working same as init method. oneTime checkbox gets non-editable but the value set to 0 only.

    Thanks,

    Dinesh

  • Dinesh Tyagi Profile Picture
    Dinesh Tyagi 37 on at
    RE: How to set a default value to a checkbox form control

    Hi Mohit,

    I Put a breakpoint & debug the code but how to check this updatedControls method execution, means when this method will execute. Because I wrote OnInitialized EH, so I debug that code is executing properly but this updatedControls method is getting hit but not executed, so how can I check its execution line-by-line.

    Thanks,

    Dinesh

  • Suggested answer
    Bharani Preetham Peraka Profile Picture
    Bharani Preetham Pe... 3,538 Moderator on at
    RE: How to set a default value to a checkbox form control

    Have you tried the same code suggested by Girish in run method of form?

    Like in run method,

    Checkbox control.checked(true);

  • Suggested answer
    Mohit Rampal Profile Picture
    Mohit Rampal 12,540 Moderator on at
    RE: How to set a default value to a checkbox form control

    Hi Dinesh, The value of OneTime checkbox would be setting back to Zero after Form is Initialized. Put a breakpoint on updatedControls method of LogisticsPostalAddressFormEventHandler class, find where it is overriding the value and manipulate that code as well.

  • Dinesh Tyagi Profile Picture
    Dinesh Tyagi 37 on at
    RE: How to set a default value to a checkbox form control

    Hi Girish,

    I have tried controlName.Value(1); & tried debug, the value property is getting changed at this line from bydefault false to true.

    But on the UI side, it showing unchecked only. But the checkbox getting non-editable using allowEdit property.

    I am not getting why its value not changing on form.

    [FormEventHandler(formStr(LogisticsPostalAddress), FormEventType::Initialized)]
      public static void LogisticsPostalAddress_OnInitialized(xFormRun sender, FormEventArgs e)
        {
            FormRun element = sender as FormRun;
            FormCheckBoxControl oneTimeAddress = element.design().controlName(formControlStr(LogisticsPostalAddress,OneTime));
            oneTimeAddress.value(1);
            oneTimeAddress.allowEdit(false);
        }

    Address.jpg

    Thanks & Regards,

    Dinesh

Helpful resources

Quick Links

Dynamics 365 Community Update – Sep 9th

Welcome to the next edition of the Community Platform Update. This is a weekly…

Announcing Our 2024 Season 2 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 290,186 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 227,996 Super User 2024 Season 2

#3
nmaenpaa Profile Picture

nmaenpaa 101,148

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans