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 :
Microsoft Dynamics 365 | Integration, Dataverse...
Answered

Prevent user to edit form based on option set value

(0) ShareShare
ReportReport
Posted on by 65

Hi,

I want to lock all the fields in the form based on the value user selects in an option type field. 

Eg:

If the user selects Contact type = A, the user should be able to edit the fields but of Contact type = B, the form should lock all the fields and make it read only. 

I could have done it using business rules but there are more than 100 fields so it will take time. IS there a way to do with JS ? If anyone could help ?

I have the same question (0)
  • Suggested answer
    Amira Beldjilali Profile Picture
    on at

    Hello Prajwal,

    You can refer to these documentations to use the setDisabled

    docs.microsoft.com/.../setdisabled

    docs.microsoft.com/.../getcontrol

    This could be also helpful Back to Basics # 63: Make All Form Fields as Read-only with Webresource in Dynamics CRM - Microsoft Dynamics CRM Community

    Regards,

    Amira

  • Suggested answer
    Bipin D365 Profile Picture
    28,983 Moderator on at

    Hi,

    You can try code from below blog to make all fields on your form real only.

    xrmcenter.wordpress.com/.../

  • Prajwal Bhetwal Profile Picture
    65 on at

    I want to make the form read-only when the user selects a certain option set value. I don't want it to be read-only all the time.

  • Verified answer
    Community Member Profile Picture
    on at

    Hi 

    Please create an onload event with the following code(Change the logical name of field and field value to yours):

    function onload(executionContext){
    	var formContext = executionContext.getFormContext();
    	var contactType = formContext.getAttribute("cr1bc_contacttype");
    	lockAllFields(executionContext);
    	contactType.addOnChange(lockAllFields);
    }
    
    function lockAllFields(executionContext){
    	var formContext = executionContext.getFormContext();
    	var contactType = formContext.getAttribute("cr1bc_contacttype").getValue();
    	var allControls = formContext.ui.controls;
    	//contactType == "A"
    	if(contactType == 516560000){
    		allControls.forEach((ctrl,index)=>{ctrl.setDisabled(false)})
    	}else if(contactType == 516560001){
    		allControls.forEach((ctrl,index)=>{ctrl.setDisabled(true)})
    	}
    	//if you need contactType still editable
    	//formContext.getControl("cr1bc_contacttype").setDisabled(false);
    }
    

    Result:

    Type B

    pastedimage1663209705683v1.png

    Type A

    pastedimage1663209737250v2.png

  • Prajwal Bhetwal Profile Picture
    65 on at

    Hi Steve, this is exactly what I want.

    But I am very new to d365 CRM so I don't know who I can create an onLoad event in CRM. If there is any document around it that you could pass it would be great.

  • Prajwal Bhetwal Profile Picture
    65 on at

    Hi   

    I've made this little change in the code so that for all other option type read-only is disabled except one contact type.

    pastedimage1663212505587v2.png


    I've added the form on load as well.

    pastedimage1663212569677v3.png

    When the form loads I'm getting this error "One of the scripts for this record has caused an error. For more details, download the log file."

    Cannot read properties of null (reading 'getValue')

    pastedimage1663212423533v1.png

    How can I fix this error?

  • Prajwal Bhetwal Profile Picture
    65 on at

    Hi   

    I've made this little change in the code so that for all other option type read-only is disabled except one contact type.

    3718.pastedimage1663212505587v2.png


    I've added the form on load as well.

    1072.pastedimage1663212569677v3.png

    When the form loads I'm getting this error "One of the scripts for this record has caused an error. For more details, download the log file."

    Cannot read properties of null (reading 'getValue')

    7612.pastedimage1663212423533v1.png

    How can I fix this error?

  • Suggested answer
    Bipin D365 Profile Picture
    28,983 Moderator on at

    Hi,

    Please make sure that contact type field schema name is correct and field is present on the form.

  • Prajwal Bhetwal Profile Picture
    65 on at

    Yes, the schema name is boq_contacttype and the field is present in the form as well.

    pastedimage1663214770084v3.png

    I've changed the attribute name in the code to match the schema name. But still, I'm getting errors.

    pastedimage1663214691041v1.png

  • Suggested answer
    Bipin D365 Profile Picture
    28,983 Moderator on at

    Hi,

    I would recommend you to debug your code

    carldesouza.com/.../

    Please recheck if contact type field is added on the form.

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 > Microsoft Dynamics 365 | Integration, Dataverse, and general topics

#1
Martin Dráb Profile Picture

Martin Dráb 41 Most Valuable Professional

#2
iampranjal Profile Picture

iampranjal 39

#3
Satyam Prakash Profile Picture

Satyam Prakash 35

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans