web
You’re offline. This is a read only version of the page.
close
Skip to main content
Community site session details

Community site session details

Session Id :
Customer experience | Sales, Customer Insights,...
Suggested answer

lock a field depending on the team permissions

(1) ShareShare
ReportReport
Posted on by 2
Hello everyone,
 
in my topic it´s about customizing permissions on the field. 
 
The starting point is: in the entity "account" there is a basic field called "status". This field should only written by the users belong to Team A (name example). User who are belong to Team B aren´t allowed to write this field, They are only allowed to see it. 
 
What did I do?
First, there isn´t the opportunity to give permissions on the field level. Only on the entity (correct me if I´m wrong). I tried to set the permission with the ribbon workbench ( I think there also should be a way to solve it). I tried with a javasript but it didn´t work.
 
So, then I figuered out, that I can work with a script. I put the script in the library on the account form. But it also didn´t work as expectet. 
 
Have you been confronted with a simmular problem? Do you have ideas? Or what should/ could I do? I would be really thankfull! 
 
Here´s the script I used for the field on the account form:
 
async function setStatecodeReadOnlyIfInTeam(executionContext) {
    const formContext = executionContext.getFormContext();
    const userId = Xrm.Utility.getGlobalContext().userSettings.userId.replace(/[{}]/g, "");
    try {
       
// Get all teams the user is a member of
        const teamMemberships = await Xrm.WebApi.retrieveMultipleRecords("teammembership", `?$filter=systemuserid eq ${userId}&$expand=teamid($select=name)`);
       
// Check whether one of the teams has the desired name
        const isInTargetTeam = teamMemberships.entities.some(entity =>
            entity.teamid && entity.teamid.name === "Team MDS Standard User"
        );
// If so, make the “statecode” field read-only
        if (isInTargetTeam) {
            const control = formContext.getControl("statecode");
            if (control) {
                control.setDisabled(true);
            }
        }
    } catch (error) {
        console.error("Error when checking the team membership:", error.message);
    }
}
 
Thank you verry much. 
Merve
I have the same question (0)
  • Suggested answer
    Muhammad Shahzad Shafique Profile Picture
    2,336 Most Valuable Professional on at
    lock a field depending on the team permissions
    Based on your requirement to make the "status" field read-only for Team B while allowing Team A to edit it, here are the best approaches:

     Solutions

    1. Field Security Profile (Most Proper Solution)
    - Create a Field Security Profile in Settings > Security
    - Add the "status" field to this profile
    - Assign Team B to this profile with only Read permission
    - Team A should not be assigned to this profile (or have Edit permission)

    2. JavaScript Alternative (If Field Security Isn't Available)
    Your script is close but needs these adjustments:

    function setStatusReadOnly(executionContext) {
        const formContext = executionContext.getFormContext();
        const userTeams = Xrm.Utility.getGlobalContext().userSettings.teams;
       
        // Check if user is in Team B (by name or ID)
        const isInTeamB = userTeams.some(team =>
            team.name === "Team B" // or use team.id if you know GUID
        );
       
        if (isInTeamB) {
            const statusControl = formContext.getControl("statuscode"); // or your field name
            if (statusControl) {
                statusControl.setDisabled(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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Abhilash Warrier – Community Spotlight

We are honored to recognize Abhilash Warrier as our Community Spotlight honoree for…

Leaderboard > Customer experience | Sales, Customer Insights, CRM

#1
Rishabh Kanaskar Profile Picture

Rishabh Kanaskar 258

#2
MVP-Daniyal Khaleel Profile Picture

MVP-Daniyal Khaleel 178

#3
Tom_Gioielli Profile Picture

Tom_Gioielli 142 Super User 2025 Season 2

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans