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 :
Customer experience | Sales, Customer Insights,...
Suggested Answer

Lock all controls on the form

(3) ShareShare
ReportReport
Posted on by 28,983 Moderator
Hi,
 
We have a business requirement where we need to lock all the fields and controla on the form when Campaign status reason is Executing, Execution queued or complete.
 
We don't want to deactivate the record so we have written below method to lock all the controls on the form from javascript on form load.
 
function disableAllFormFields(context) {
var formContext = context.getFormContext();
formContext.ui.controls.forEach(function (control, i) {
if (control && control.getDisabled && !control.getDisabled()) {
control.setDisabled(true);
}
});
}
 
I am seeing weird behaviour where some of the fields are still not locked when Campaign status reason criteria is met.
 
I have already verified no business rules are present on the campaign form.
 
No other javascript code found which will enables those fields.
 
I tried adding this code/method using data.addOnLoad in form on load event but no help.
 
I tried adding delay with the help of setTimeout but that also did not fix the issue.
 
Reaching out to the community members to help me figure out this issue and provide guidance.
 
 
I have the same question (0)
  • Waed Ayyad Profile Picture
    9,039 Super User 2025 Season 2 on at
     
    On which D365 product are you working?
  • Suggested answer
    Holly Huffman Profile Picture
    6,530 Super User 2025 Season 2 on at
    Good morning, afternoon, or evening :) depending on your location!
     
    If you are using Dynamics 365 Marketing, the issue with some fields not locking when the Campaign status reason is set to "Executing," "Execution queued," or "Complete" could be due to how the JavaScript code interacts with the form controls.
     
    1. Ensure All Controls Are Targeted
    • The provided code loops through all controls on the form, but some controls might not be directly editable (e.g., subgrids, composite fields, or custom controls). Update the code to handle these cases:
      function disableAllFormFields(context) {
          var formContext = context.getFormContext();
          formContext.ui.controls.forEach(function (control) {
              if (control && control.getDisabled && !control.getDisabled()) {
                  control.setDisabled(true);
              }
          });
      }

      Ensure that all controls, including subgrids and composite fields, are explicitly targeted if needed.
    2. Check Field Dependencies
    • Some fields might have dependencies or conditions set by business rules or field-level customizations. Even if no business rules are visible, check for:
      • Field-level security: Ensure the fields are not restricted by security roles.
      • OnChange Events: Verify if any field has an OnChange event that might re-enable it.
    3. Use Form Load Timing
    • If fields are being re-enabled after the form loads, ensure the script is executed at the right time. Use the formContext.data.addOnLoad method to ensure the script runs after the form is fully loaded:
      function onFormLoad(context) {
          var formContext = context.getFormContext();
          formContext.data.addOnLoad(function () {
              disableAllFormFields(context);
          });
      }
    4. Debug the Script
    • Use the browser's developer tools to debug the script. Add console.log statements to check which controls are being processed and identify any that are skipped:
      console.log(control.getName());
    5. Consider Power Automate
    • If JavaScript alone doesn't resolve the issue, consider using Power Automate to enforce field locking by updating the record's status or applying a custom workflow.
    6. Alternative Approach: Business Rules
    • If JavaScript proves unreliable, use Business Rules to lock fields based on the Campaign status reason. Business Rules are executed server-side and can ensure consistent behavior.
     
    Hope this helps!
  • Bipin D365 Profile Picture
    28,983 Moderator on at
    Dynamics CRM ON-PREMISE version. 
  • André Arnaud de Calavon Profile Picture
    301,025 Super User 2025 Season 2 on at
    Moved the question from the Dynamics 365 General to the CRM forum.

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 > Customer experience | Sales, Customer Insights, CRM

#1
Tom_Gioielli Profile Picture

Tom_Gioielli 108 Super User 2025 Season 2

#2
Jimmy Passeti Profile Picture

Jimmy Passeti 50 Most Valuable Professional

#3
Gerardo Rentería García Profile Picture

Gerardo Rentería Ga... 49 Most Valuable Professional

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans