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