Stack Community. I have the code below. In short, it’s enabling and disabling fields on conditions.
Below works perfect.
function unLockNonSalaryCostFieldsBasedOnRolesTeams(executionContext) {
const formContext = executionContext.getFormContext();
const roles = ["Service Ops User", "DocuSignUser", "Geographic Enhanced User", "Support Business Advisor"];
// Store Security Roles
const userRoles = Xrm.Utility.getGlobalContext().userSettings.roles;
// Check if the user has any of the roles in the 'roles' array
const hasAnyRole = userRoles.get().some(r => roles.includes(r.name));
// Check specifically for the 'Support Business Advisor' role
const hasSupportBusinessAdvisorRole = userRoles.get().some(r => r.name === "Support Business Advisor");
const currentFundingStatus = formContext.getAttribute("fsdyn_fundingstatus").getValue();
const grantIntentionType = formContext.getAttribute("fsdyn_grantintentiontype").getValue();
// Ensure 'grantIntentionType' has a value before accessing [0]
const grantIntentionTypeLookUpValueName = grantIntentionType && grantIntentionType.length > 0 ? grantIntentionType[0].name : "";
if (grantIntentionTypeLookUpValueName !== "Post" && currentFundingStatus === FundingStatus.Proposed) {
// Unlock or lock fields based on whether the user has any of the specified roles
formContext.getControl("fsdyn_amount").setDisabled(!hasAnyRole);
formContext.getControl("fsdyn_fundingperiodmonths").setDisabled(!hasAnyRole);
// Specifically unlock 'activity code' if the user has the 'Support Business Advisor' role
if (hasSupportBusinessAdvisorRole) {
formContext.getControl("fsdyn_activitycode").setDisabled(false);
} else {
formContext.getControl("fsdyn_activitycode").setDisabled(true);
}
} else {
// Optionally, lock the fields if the main condition is not met
formContext.getControl("fsdyn_amount").setDisabled(true);
formContext.getControl("fsdyn_fundingperiodmonths").setDisabled(true);
formContext.getControl("fsdyn_activitycode").setDisabled(true);
}}
I have a function unLockFieldsBasedOnConditions below as well. I want to combine it with the top function, but don’t want to break any functionality or regress anything.
function unLockFieldsBasedOnConditions(executionContext) {
const formContext = executionContext.getFormContext();
const roles = ["Service Ops User"];
//Store Security Roles
const userRoles = Xrm.Utility.getGlobalContext().userSettings.roles;
const hasRole = userRoles.get().some(r => roles.indexOf(r.name) != -1);
const currentFundingStatus = formContext.getAttribute("fsdyn_fundingstatus").getValue();
if (currentFundingStatus === FundingStatus.NotInFunding || currentFundingStatus === FundingStatus.Adopted) {
formContext.getControl("fsdyn_hours").setDisabled(!hasRole);
formContext.getControl("fsdyn_wte").setDisabled(!hasRole);
formContext.getControl("fsdyn_wageband").setDisabled(!hasRole);
}
}
My question is: Should I add the logic of unLockFieldsBasedOnConditions to unLockNonSalaryCostFieldsBasedOnRolesTeams or should I leave them as separate functions?
Also, if I do add the logic, please advise how to combine without regressing?
Things I tried:
- Tried storing Service Ops User role in own variable.
- Added the if logic like above.
Code that didn’t work properly:
function unLockNonSalaryCostFieldsBasedOnRoles(executionContext) {
const formContext = executionContext.getFormContext();
// Store Security Roles
const roles = ["Service Ops User", "DocuSignUser", "Geographic Enhanced User"];
const userRoles = Xrm.Utility.getGlobalContext().userSettings.roles;
// Check if the user has any of the roles in the 'roles' array
const hasAnyRole = userRoles.get().some(r => roles.includes(r.name));
// Check specifically for the 'Support Business Advisor' role
const hasSupportBusinessAdvisorRole = userRoles.get().some(r => r.name === "Support Business Advisor");
// Check specifically for the 'Service Ops User' role
const hasServiceOpsUserRole = userRoles.get().some(r => r.name === "Service Ops User");
const currentFundingStatus = formContext.getAttribute("fsdyn_fundingstatus").getValue();
const grantIntentionType = formContext.getAttribute("fsdyn_grantintentiontype").getValue();
// Ensure 'grantIntentionType' has a value before accessing [0]
const grantIntentionTypeLookUpValueName = grantIntentionType && grantIntentionType.length > 0 ? grantIntentionType[0].name : "";
if (grantIntentionTypeLookUpValueName !== "Post" && currentFundingStatus === FundingStatus.Proposed) {
// Unlock or lock fields based on whether the user has any of the specified roles
formContext.getControl("fsdyn_amount").setDisabled(!hasAnyRole);
formContext.getControl("fsdyn_fundingperiodmonths").setDisabled(!hasAnyRole);
// Specifically unlock 'activity code' if the user has the 'Support Business Advisor' role
if (hasSupportBusinessAdvisorRole) {
formContext.getControl("fsdyn_activitycode").setDisabled(false);
}
else {
formContext.getControl("fsdyn_activitycode").setDisabled(true);
}
}
else {
if (hasServiceOpsUserRole && currentFundingStatus === FundingStatus.NotInFunding || currentFundingStatus === FundingStatus.Adopted) {
formContext.getControl("fsdyn_hours").setDisabled(false);
formContext.getControl("fsdyn_wte").setDisabled(false);
formContext.getControl("fsdyn_wageband").setDisabled(false);
}
}
}
;