1

我一直在尝试解决一个问题,但我不知道 JavaScript,所以我在互联网上追逐我的尾巴。

我继承了一个 JavaScript 文件,该文件应该在对帐户制定计划时触发。每个帐户可以有多个计划,但一次只能有 1 个有效的计划。这意味着当您创建一个新的时,您应该只有在所有其他人都被停用的情况下才能使用。我们现在拥有的代码(见下文)仅查找计划的存在,而不管其状态如何。谁能帮我吗?

谢谢

checkActiveADP = function()
{
    // check if there is a key account populated
    if (Xrm.Page.getAttribute("new_keyaccountid").getValue() != null && Xrm.Page.ui.getFormType() == 1)
    {
        // get the id of the parent account of the account plan
        var keyaccountid = Xrm.Page.getAttribute("new_keyaccountid").getValue()[0].id;

        if (keyaccountid != null)
        {
            // build query to get all the account plans for the current parent account - if any
            var filter = "/New_accountplanSet()?$filter=new_keyaccountid/Id eq guid'" + keyaccountid + "'";             
            var retrievedMultiple = CCrm.JSCore.RetrieveMultipleRequest(filter);                        

            if (retrievedMultiple.results.length >=1) 
            {
                alert("Active ADP already exists, please update that one or deactivate before creating a new one");                             
            }
        }
    }
}
4

3 回答 3

1

您应该将活动状态过滤器 ( StateCode/Value eq 0) 添加到OData filter变量,如下所示:

        var filter = "/New_accountplanSet()?$filter=new_keyaccountid/Id eq guid'" + keyaccountid + "' and StateCode/Value eq 0";
        var retrievedMultiple = CCrm.JSCore.RetrieveMultipleRequest(filter);

        if (retrievedMultiple.results.length >= 1)
        {
            alert("Active ADP already exists, please update that one or deactivate before creating a new one");
        }

结果将仅包括活动帐户计划记录(如果有)。

于 2013-08-13T19:10:19.450 回答
0

retrievedMultiple.results是一个数组。您需要对这些项目进行 for 循环,检查它们的状态是否处于活动状态。

于 2013-08-12T14:37:12.593 回答
0

您的代码应如下所示: 在之后添加一个 else 条件retrievedMultiple != null || retrievedMultiple.results != null以验证您的查询没有问题。

checkActiveADP = function()
{
    // check if there is a key account populated
    if (Xrm.Page.getAttribute("new_keyaccountid").getValue() != null && Xrm.Page.ui.getFormType() == 1)
    {
        // get the id of the parent account of the account plan
        var keyaccountid = Xrm.Page.getAttribute("new_keyaccountid").getValue()[0].id;

        if (keyaccountid != null)
        {
            // build query to get all the account plans for the current parent account - if any
            var filter = "/New_accountplanSet()?$filter=new_keyaccountid/Id eq guid'" + keyaccountid + "'";             
            var retrievedMultiple = CCrm.JSCore.RetrieveMultipleRequest(filter);                        

            if (retrievedMultiple != null || retrievedMultiple.results != null) 
            {
               for(var i = 0;i<retrievedMultiple.results.length; i++)
               {
                   if(retrievedMultiple.results[i]["statecode"] == 0)
                   {
                       alert("Active ADP already exists, please update that one or deactivate before creating a new one");
                   }
               }                            
            }
        }
    }
}
于 2013-08-12T15:17:39.743 回答