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,...
Answered

Set all fields on Opportunity to Read Only based on Option Set Value

(0) ShareShare
ReportReport
Posted on by 175

Hi there -

On an Opportunity record, I have a requirement that when a field "Bid Status" = No Bid, that an Opportunity essentially has to be 'Canceled'. It cannot be Won or Lost (because that would make their Win-Loss report innacurate), so it has to remain 'Open'. 

To handle this, and ensure that no other changes are made to the record, I want to make all fields Read Only (set disabled) when the Bid Status = No Bid.  Maybe even display a bold/color web resource message that says something to the effect of "This Opportunity is No Bid and has been Canceled". 

Can anyone help with an example of updated code for 9.0 that would work for this scenario?  

I have the same question (0)
  • Verified answer
    Bipin D365 Profile Picture
    28,983 Moderator on at

    Hi,

    Try below code to make controls disable/read only.

    function disableAllFields(executionContext) {

           var formcontext=executionContext.getFormContext();

           formcontext.ui.controls.forEach(function (control, i) {

               if (control && control.getDisabled && !control.getDisabled()) {

                   control.setDisabled(true);

               }

           });

       }

    If found helpful, Please mark my answer verified.

  • William Bradley Profile Picture
    175 on at

    Great - I had something similar.  But I want to set this function on change of field Bid Status (exe_bidstatus) = No Bid (283,780,002).  I tried entering in this condition and I am not getting results.  I have the event handler set on change of the field, but need to update the actual code correctly for the above values - any ideas ?

  • a33ik Profile Picture
    84,331 Most Valuable Professional on at

    Can you please provide the code that you use?

  • William Bradley Profile Picture
    175 on at

    Sure here is the code (written by a non-developer, apologies):

    --> Make All fields Read Only based on an Option Set Value

    function disableAllFields(executionContext) {

          var formcontext=executionContext.getFormContext();

          formcontext.ui.controls.forEach(function (control, i) {

    if (exe_bidstatus != 283,780,002) {  

              if (control && control.getDisabled && !control.getDisabled()) {

                  control.setDisabled(true);

              }

          });

      }

  • Verified answer
    a33ik Profile Picture
    84,331 Most Valuable Professional on at

    Here the code:

    function disableAllFields(executionContext) {

    var formcontext=executionContext.getFormContext();

    var exe_bidstatus = formContext.getAttribute("exe_bidstatus").getValue();

    if (exe_bidstatus !== 283780002) {

    return;

    }

    formcontext.ui.controls.forEach(function (control, i) {

    if (control && control.getDisabled && !control.getDisabled()) {

    control.setDisabled(true);

    }

    });

    }

    add handler to onload and field change.

  • William Bradley Profile Picture
    175 on at

    Thanks to the both of you.  Will try it out.

  • William Bradley Profile Picture
    175 on at

    Got the following onLoad and onChange...looks like line 3 of the code ?

    ReferenceError: formContext is not defined

       at disableAllFields (expansetest.crm.dynamics.com/.../exe_SetFieldsReadOnlyNoBid:3:21)

       at Mu.executeFunction (expansetest.crm.dynamics.com/.../app.js

       at Mu.execute (expansetest.crm.dynamics.com/.../app.js

       at expansetest.crm.dynamics.com/.../app.js

       at o (expansetest.crm.dynamics.com/.../app.js

       at Iu._executeIndividualEvent (expansetest.crm.dynamics.com/.../app.js

       at Iu._executeEventHandler (expansetest.crm.dynamics.com/.../app.js

       at Object.execute (expansetest.crm.dynamics.com/.../app.js

       at O._executeSyncAction (expansetest.crm.dynamics.com/.../app.js

       at O._executeSync (expansetest.crm.dynamics.com/.../app.js

  • Verified answer
    a33ik Profile Picture
    84,331 Most Valuable Professional on at

    Replace line

    var formcontext=executionContext.getFormContext();

    with line

    var formContext=executionContext.getFormContext();

  • William Bradley Profile Picture
    175 on at

    Yup, the case change did the trick.  Question though, if I wanted to take this a step further, and set all fields to read only except for the Bid Status field, how would that look in the code?

  • Suggested answer
    a33ik Profile Picture
    84,331 Most Valuable Professional on at

    Try following:

    function disableAllFields(executionContext) {
    	var formContext=executionContext.getFormContext();
    	var exe_bidstatus = formContext.getAttribute("exe_bidstatus").getValue();
    
    	if (exe_bidstatus !== 283780002) {
    		return;
    	}
    
    	formcontext.ui.controls.forEach(function (control, i) {
    		if (control && control.getDisabled && !control.getDisabled() && control.getAttribute().getName() !== "exe_bidstatus") {
    			control.setDisabled(true);
    		}
    	});
    }

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 137 Super User 2025 Season 2

#2
#ManoVerse Profile Picture

#ManoVerse 57

#3
Jimmy Passeti Profile Picture

Jimmy Passeti 50 Most Valuable Professional

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans