Skip to main content

Notifications

Announcements

No record found.

Dynamics 365 general forum

How to make record read only based on field value?

Posted on by 2,645

Hello,

I have recreated the Contract entity so we don't lose it with the next major release.  However, now I need to put the record in read only when my status field = Active.  Then when the status is Active, I need to grant just certain users edit capabilities so that they can edit any field on that record still.

I am thinking Java Script is the best way to do this, but not sure how to write this.  Any help is appreciated.  Thank you.

  • USA80 Profile Picture
    USA80 2,645 on at
    RE: How to make record read only based on field value?

    So after the first script runs the second one will and know if the user has rights or not based on looking at the user security roles?  I don't have to specify the security role in the script?

  • Suggested answer
    LeoAlt Profile Picture
    LeoAlt 16,329 on at
    RE: How to make record read only based on field value?

    Hi Partner,

    Yes you are right, using javascript should be the best way to achieve your requirement.

    Here are the code logics and samples.

    You could create a security role which allows users to modify the records when status filed=Active.

    1.Get the current user's security role.

    function GeteRole (executionContext) {
        var roleNameList=[];
        var formContext = executionContext.getFormContext();
        var currentUserRoleIds = Xrm.Utility.getGlobalContext().userSettings.securityRoles;
        for (i=0i<currentUserRoleIds.lengthi++) {
          var currentRoleUniqueId = currentUserRoleIds[i];
          Xrm.WebApi.retrieveRecord("roles",currentRoleUniqueId,"?$select=name").then(
            function success(result) {
            var roleName = result.name;
            roleNameList.push(roleName);
      },
            function error (error) {
              Xrm.Navigation.openAlertDialog({ text: error.message });
            }
      );}
            return roleNameList;
    }

    2.If the current user has no permission to modify the record, then disable all the fiels.

    function ReadOnly ()
    {
     var cs = Xrm.Page.ui.controls.get();
     for (var i in cs
     {
      var c = cs[i];
      if (c.getName() != "" && c.getName() != null
      {
       if (!c.getDisabled()) {c.setDisabled(true);}
      }
     }
    }

    Hope it helps.

    Best Regards,

    Leo

  • Suggested answer
    TylerS.Dev Profile Picture
    TylerS.Dev 725 on at
    RE: How to make record read only based on field value?

    I see.

    There is existing topic that discussed how to disable all fields within a form, please see link below, you would want to call the function  during onLoad event

    community.dynamics.com/.../make-an-attribute-readonly-inside-a-form

    To enable/disable edit by user, you need to get login user information and use if statement to check if the user can edit those fields:

    var userSettings = Xrm.Utility.getGlobalContext().userSettings;

    if(userSettings.userName='Jane Doe'){

    //you can use code provided in article above

    disableAllFields();

    }

    you can do this based on security role by checking userSettings.securityRoles , or use userSettings.userId to check by user guid (to avoid granting access by mistake to another user with same name).

  • USA80 Profile Picture
    USA80 2,645 on at
    RE: How to make record read only based on field value?

    A lot of different groups will already have write access to the Contract record when it is in a draft status.  When it is in a Active status, i just want certain groups to be able to modify the fields then.  Hope that makes sense.

  • Suggested answer
    TylerS.Dev Profile Picture
    TylerS.Dev 725 on at
    RE: How to make record read only based on field value?

    Is there a reason you do not want to do this through user role?

    By system default, they will not be able to edit inactive records.  All you need to do is set read only for your custom entity for all roles and create a custom role to grant write access to this custom entity.  And assign this role to the people you wish to grant write access to.

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

November Spotlight Star - Khushbu Rajvi

Congratulations to a top community star!

Forum Structure Changes Coming on 11/8!

In our never-ending quest to help the Dynamics 365 Community members get answers faster …

Dynamics 365 Community Platform update – Oct 28

Welcome to the next edition of the Community Platform Update. This is a status …

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