Skip to main content

Notifications

Microsoft Dynamics 365 | Integration, Dataverse...
Answered

Prevent user to edit form based on option set value

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 ?

  • Prajwal Bhetwal Profile Picture
    Prajwal Bhetwal 65 on at
    RE: Prevent user to edit form based on option set value

    The main issue was the function name. I changed that and it is working now. Thank you.

  • Verified answer
    Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Prevent user to edit form based on option set value

    Hi Prajwal Bhetwal,

    According to the error message, the result of formContext.getAttribute("boq_contacttype") is null. 

    And as Bipin Kumar says, you need check the field's logical name and if this field on your form.

    1.Check the field's logical name:

    pastedimage1663219581968v1.png

    2. Check the field is on your form or not:

    pastedimage1663219723315v2.png

    If the issue persists, please clear the cache of browser.

    BTW,  the function name should be onload.

    pastedimage1663219780644v3.png

    And if you change the in your web resource, please remember to reupload your code and publish again.

    pastedimage1663219898144v4.png

  • Suggested answer
    Bipin D365 Profile Picture
    Bipin D365 28,964 Super User 2024 Season 1 on at
    RE: Prevent user to edit form based on option set value

    Hi,

    I would recommend you to debug your code

    carldesouza.com/.../

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

  • Prajwal Bhetwal Profile Picture
    Prajwal Bhetwal 65 on at
    RE: Prevent user to edit form based on option set value

    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
    Bipin D365 28,964 Super User 2024 Season 1 on at
    RE: Prevent user to edit form based on option set value

    Hi,

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

  • Prajwal Bhetwal Profile Picture
    Prajwal Bhetwal 65 on at
    RE: Prevent user to edit form based on option set value

    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?

  • Prajwal Bhetwal Profile Picture
    Prajwal Bhetwal 65 on at
    RE: Prevent user to edit form based on option set value

    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
    Prajwal Bhetwal 65 on at
    RE: Prevent user to edit form based on option set value

    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.

  • Verified answer
    Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Prevent user to edit form based on option set value

    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
    Prajwal Bhetwal 65 on at
    RE: Prevent user to edit form based on option set value

    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.

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!

Community AMA December 12th

Join us as we continue to demystify the Dynamics 365 Contact Center

Leaderboard

#1
André Arnaud de Calavon Profile Picture

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

#2
Martin Dráb Profile Picture

Martin Dráb 230,149 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Product updates

Dynamics 365 release plans