web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Finance | Project Operations, Human Resources, ...
Answered

How to set a default value to a checkbox form control

(1) ShareShare
ReportReport
Posted on by 61

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

I have the same question (0)
  • Verified answer
    GirishS Profile Picture
    27,827 Moderator on at

    Hi Dinesh,

    If its checkbox control you can use checkBox.Checked(true) to make it checked.

    Thanks,

    Girish S.

  • Verified answer
    GirishS Profile Picture
    27,827 Moderator on at

    Also you need to change FormControl to FormCheckBoxControl.

    FormCheckBoxControl oneTimeAddress = element.design().controlName(formControlStr(LogisticsPostalAddress,OneTime));

    Thanks,

    Girish S.

  • Dinesh Tyagi Profile Picture
    61 on at

    Hi Girish,

    I have used what you have suggest: 

        [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.checked(true);
            oneTimeAddress.allowEdit(false);
        }

    I checked but still this form control set to unchecked after opening the form. 

    Also, I tried some code before to resolve the issue, I assign some noyes value to this formcontrol, that time it was throwing two errors- one was type error bcz I attempt to assign enum to form control & the second one was: oneTime control is read-Only.

    I was confused that this checkbox can be checked/unchecked from UI but throwing this kind of error. while working with x .

    Thanks & Regards,

    Dinesh

  • GirishS Profile Picture
    27,827 Moderator on at

    Instead of setting the value on the code,  try setting the "value" property on the form control. Set the value property to 1 so it should be checked by default.

    Thanks

    Girish S.

  • Dinesh Tyagi Profile Picture
    61 on at

    Hi Girish,

    I found the value property of OneTime form control but how can I change this property because can't change microsoft base form & if I take extension 

    then this value property will be disabled. Then how can I set this property.

    Thanks & Regards,

    Dinesh

  • GirishS Profile Picture
    27,827 Moderator on at

    If its an extension then it won't be available for editing.

    Try setting controlName.Value(1);

    Also try debugging and check whether your code is getting called.

    Thanks,

    Girish S.

  • Dinesh Tyagi Profile Picture
    61 on at

    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

  • Suggested answer
    Mohit Rampal Profile Picture
    12,565 Moderator on at

    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.

  • Suggested answer
    Bharani Preetham Peraka Profile Picture
    3,634 Moderator on at

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

    Like in run method,

    Checkbox control.checked(true);

  • Dinesh Tyagi Profile Picture
    61 on at

    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

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > Finance | Project Operations, Human Resources, AX, GP, SL

#1
Martin Dráb Profile Picture

Martin Dráb 559 Most Valuable Professional

#2
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 464 Super User 2025 Season 2

#3
Sohaib Cheema Profile Picture

Sohaib Cheema 250 User Group Leader

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans