Skip to main content

Notifications

Dynamics 365 general forum

Form / Field Level Security Based on Record Owner

Posted on by 150

We have Salesreps that we want to have full form access to Accounts where they are the Record Owner. However, we want them to see certain key information about ALL Accounts, just not EVERYTHING. We have played with security roles, field security, and form security, but nothing gives us control at the USER / RECORD OWNER level (they all apply to security roles from what we have seen). We are fine with needing to have multiple forms and security roles to accomplish this, and even needing to implement some Javascript, but we don’t know where to begin. Any suggestions are greatly appreciated, thank you!

  • Taybo510 Profile Picture
    Taybo510 150 on at
    RE: Form / Field Level Security Based on Record Owner

    We have not fully tested this, as we are worried about relying too heavily on Javascript in case things break down in the future, but this response gives the entire framework we need when we are ready to give it a try. Thank you again Goutam! Wei Jie Fun below also brought up a good point below, that while this limits the view the User will have on the form, if their permissions in the background allow for them to see everything, they can use a workaround with Advanced Finds to see any columns they want. Not 100% what we want, but at this point, we will need to compromise on some things, and this still seems like the best solution. :-)

  • Alex Fun Wei Jie Profile Picture
    Alex Fun Wei Jie 33,626 on at
    RE: Form / Field Level Security Based on Record Owner

    Hi,

    I am wondering, lets say if you hide those fields, but users still can access those hidden field via advanced find , reports and etc. Have you thought about that? or you dont mind it .

  • Verified answer
    gdas Profile Picture
    gdas 50,085 on at
    RE: Form / Field Level Security Based on Record Owner

    Hi ,

    Thank you ! I am impressed your words , so thought why not I share full code to you  :) -

    You just need to

    - Create a section in a tab and include the field.

    - Create web resource and copy paste below code.

    - Replace Tab Name /Section Name.

           // Register Below method in the form onload
            function OnLoadSectionShowHide() {
                var currentUserId = Xrm.Page.context.getUserId();
                var ownerId = Xrm.Page.getAttribute("ownerid").getValue(); // Make sure owner fields should available in the form
                ownerId = ownerId.slice(1, -1);
    
                var userId = currentUserId.slice(1, -1);
                var req = new XMLHttpRequest();
                req.open("GET", Xrm.Page.context.getClientUrl() + "/api/data/v8.2/systemuserrolescollection?$select=roleid&$filter=systemuserid eq " + userId + "", true);
                req.setRequestHeader("OData-MaxVersion", "4.0");
                req.setRequestHeader("OData-Version", "4.0");
                req.setRequestHeader("Accept", "application/json");
                req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
                req.setRequestHeader("Prefer", "odata.include-annotations=\"*\"");
                req.onreadystatechange = function () {
                    if (this.readyState === 4) {
                        req.onreadystatechange = null;
                        if (this.status === 200) {
                            var results = JSON.parse(this.response);
                            for (var i = 0; i < results.value.length; i++) {
                                var userRoleId = results.value[i].roleid;
                                var userRoleName = GetRoleName(userRoleId);
                                // Assume that user will have one security role
                                if (userRoleName == "Salesreps" && userId != ownerId) { // Replace Role Name
                                    // Hide Section or fields
                                    Xrm.Page.ui.tabs.get("tabname").sections.get("sectionname").setVisible(false); //Replace tab Name /Section Name
    
                                }
                            }
                        } else {
                            Xrm.Utility.alertDialog(this.statusText);
                        }
                    }
                };
                req.send();
            }
    
            //Get Rolename based on RoleId
            function GetRoleName(roleId) {
                var req = new XMLHttpRequest();
                req.open("GET", Xrm.Page.context.getClientUrl() + "/api/data/v8.2/roles(" + roleId + ")?$select=name", false);
                req.setRequestHeader("OData-MaxVersion", "4.0");
                req.setRequestHeader("OData-Version", "4.0");
                req.setRequestHeader("Accept", "application/json");
                req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
                req.setRequestHeader("Prefer", "odata.include-annotations=\"*\"");
                req.onreadystatechange = function () {
                    if (this.readyState === 4) {
                        req.onreadystatechange = null;
                        if (this.status === 200) {
                            var result = JSON.parse(this.response);
                            var roleName = result["name"];
                            alert(roleName);
                        } else {
                            Xrm.Utility.alertDialog(this.statusText);
                        }
                    }
                };
                req.send();
            }

  • Taybo510 Profile Picture
    Taybo510 150 on at
    RE: Form / Field Level Security Based on Record Owner

    Thank you for the thorough and speedy response Goutam! I am going to work on implementing this with my team, and if it works, I will make sure to mark your answer as verified. :-)

  • Suggested answer
    gdas Profile Picture
    gdas 50,085 on at
    RE: Form / Field Level Security Based on Record Owner

    Hi,

    I think for your requirement you need to write client side JS. Lets assume you have 20 fields in account form and out of 20 fields you dont want to show 5 fields for all  accounts those are not owned by user and if the user having salesreps security role. So add those 5 fields in a section.

    Show hide  the fields or section based on the condition like below -

    Get the user security role , see below reference --

    community.dynamics.com/.../229425

    Get current record owner id in the form onload --

    community.dynamics.com/.../103674

    Get login user id  -Xrm.Page.context.getUserId()

    Once you get above information

    if(UserRole == "Salesreps" && UserID != OwnerId)

    {

      // Hide Section or fields

        //Xrm.Page.ui.tabs.get("tabname").sections.get("sectionname").setVisible(false);

    }

    else if(UserRole == "Salesreps" && UserID == OwnerId)

    {

      // Show section or fields

    //Xrm.Page.ui.tabs.get("tabname").sections.get("sectionname").setVisible(true);

    }

    else

    {

      // Show section or fields

    //Xrm.Page.ui.tabs.get("tabname").sections.get("sectionname").setVisible(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

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,253 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,188 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans