简体   繁体   English

CRM 2011父级上的活动实体

[英]CRM 2011 Active entities on parent

I have been trying for ages to solve a problem but I don't know JavaScript so i'm chasing my tail around the Internet. 我一直在尝试解决问题,但是我不了解JavaScript,所以我在互联网上追逐尾巴。

I have inherited a JavaScript file which is supposed to fire when a plan is made on an account. 我继承了一个JavaScript文件,该文件应在对帐户进行计划时触发。 Each account can have multiple plans but only 1 active one at a time. 每个帐户可以有多个计划,但一次只能有一个活动计划。 That means that when you create a new one, you should only be able if all others are deactivated. 这意味着,当您创建一个新文件时,只有在所有其他文件均未激活的情况下,您才应该能够。 The code we have now (see below) only looks for the existence of a plan regardless of its state. 我们现在拥有的代码(请参见下文)仅查找计划的存在,而不管其状态如何。 Can anyone help me out? 谁能帮我吗?

Thanks 谢谢

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");                             
            }
        }
    }
}

You should add the active state filter ( StateCode/Value eq 0 ) to the OData filter variable, as follows: 您应将活动状态过滤器( 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");
        }

The result will include only the active account plans records, if any. 结果将仅包括有效的客户计划记录(如果有)。

retrievedMultiple.results is an array. retrievedMultiple.results是一个数组。 You'll need to do a for loop over the items, checking to see if their status is active. 您需要对项目进行for循环,检查其状态是否为活动状态。

Your code should be something like below: Add an else condition after retrievedMultiple != null || retrievedMultiple.results != null 您的代码应类似于以下内容:在restoredMultiple之后添加else条件retrievedMultiple != null || retrievedMultiple.results != null retrievedMultiple != null || retrievedMultiple.results != null to verify that there is nothing wrong with your query. 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");
                   }
               }                            
            }
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM