JavaScript 条件语句

问题描述 投票:0回答:1

我对 JS 开发非常陌生,我试图在 else 中实现以下目标,但没有得到执行,它在 Dynamics 365 中不起作用。也许语法错误...请告知。

如果拨款意向类型不等于发布且当前资金状态 = 发布并检查用户角色等于支持业务顾问,则我希望实现以下目标,然后解锁字段活动代码字段。第一个 if 工作正常。然而,这不起作用,我认为我的其他条件甚至没有受到影响。

我只能为 1 个角色创建相同的函数,但我相信这会重复代码。

请指教。

            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;
                const hasRole = userRoles.get().some(r => roles.indexOf(r.name) != -1);

                const currentFundingStatus = formContext.getAttribute("fsdyn_fundingstatus").getValue();

                const grantIntentionType = formContext.getAttribute("fsdyn_grantintentiontype").getValue();

                //get the name of currently lookup field
                const grantIntentionTypeLookUpValueName = grantIntentionType[0].name;

                if (grantIntentionTypeLookUpValueName !== "Post" && currentFundingStatus === FundingStatus.Proposed) {
                    formContext.getControl("fsdyn_amount").setDisabled(!hasRole);
                    formContext.getControl("fsdyn_fundingperiodmonths").setDisabled(!hasRole);
                }

                else {

                    if (grantIntentionTypeLookUpValueName !== "Post" && currentFundingStatus === FundingStatus.Proposed) {
                        formContext.getControl("fsdyn_activitycode").setDisabled(!hasRole);

                    }
                }
            }

我尝试了其他条件来单独执行,但是仅检查支持业务顾问角色,我需要存储另一个变量吗?

javascript typescript dynamics-crm
1个回答
0
投票

我认为 else 块的问题在于它永远不会被执行,因为条件与 if 相同。当第一个 if 条件为 false 时,else 块会检查相同的内容,因此它不会运行。可以通过删除 else 并在主逻辑中添加对“支持业务顾问”角色的特定检查来简化事情。这样,代码就不会重复,并且应该正确解锁正确用户角色的活动代码字段。

还添加了一个检查以确保 grantIntentionType 不为 null 以避免错误。现在,它应该像您希望的那样工作,并且仅针对正确的角色和条件解锁字段。

试试这个:

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);
}}
© www.soinside.com 2019 - 2024. All rights reserved.