Skip to main content

Notifications

Announcements

No record found.

Customer experience | Sales, Customer Insights,...
Answered

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

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?  

  • William Bradley Profile Picture
    William Bradley 175 on at
    RE: Set all fields on Opportunity to Read Only based on Option Set Value

    That works like a charm, the Bid Status field is excluded from the read only.  I think the only refinement left that I can think of is that when Bid Status does not equal No Bid , that the fields are editable

  • William Bradley Profile Picture
    William Bradley 175 on at
    RE: Set all fields on Opportunity to Read Only based on Option Set Value

    Thanks very much - I really appreciate it.

  • Suggested answer
    a33ik Profile Picture
    a33ik 84,323 Most Valuable Professional on at
    RE: Set all fields on Opportunity to Read Only based on Option Set Value

    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);
    		}
    	});
    }

  • William Bradley Profile Picture
    William Bradley 175 on at
    RE: Set all fields on Opportunity to Read Only based on Option Set Value

    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?

  • Verified answer
    a33ik Profile Picture
    a33ik 84,323 Most Valuable Professional on at
    RE: Set all fields on Opportunity to Read Only based on Option Set Value

    Replace line

    var formcontext=executionContext.getFormContext();

    with line

    var formContext=executionContext.getFormContext();

  • William Bradley Profile Picture
    William Bradley 175 on at
    RE: Set all fields on Opportunity to Read Only based on Option Set Value

    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

  • William Bradley Profile Picture
    William Bradley 175 on at
    RE: Set all fields on Opportunity to Read Only based on Option Set Value

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

  • Verified answer
    a33ik Profile Picture
    a33ik 84,323 Most Valuable Professional on at
    RE: Set all fields on Opportunity to Read Only based on Option Set Value

    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
    William Bradley 175 on at
    RE: Set all fields on Opportunity to Read Only based on Option Set Value

    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);

              }

          });

      }

  • a33ik Profile Picture
    a33ik 84,323 Most Valuable Professional on at
    RE: Set all fields on Opportunity to Read Only based on Option Set Value

    Can you please provide the code that you use?

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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,280 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,214 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans