2013-08-12 75 views
1

我一直在努力解决一个问题,但我不知道JavaScript,所以我在互联网上追逐我的尾巴。客户关系管理2011家长活动实体

我已经继承了一个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");        
      } 
     } 
    } 
} 
+0

你会得到任何结果吗?尝试'alert(retrieveMultiple.results.length);' – Scorpion

+0

当它运行,并有停用计划链接到父帐户它引发消息,并不会让你保存一个新的。 – Powell21

回答

1

你应该激活状态滤波器(StateCode/Value eq 0)添加到的ODatafilter变量,如下所示:

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

结果将只包括主动交代计划的记录,如果有的话。

+0

在海上谢谢!这已经清除了它! – Powell21

0

retrievedMultiple.results是一个数组。您需要对项目执行for循环,检查其状态是否处于活动状态。

+0

非常感谢,就像我说我不懂JavaScript,但至少我现在有一个地方可以开始 – Powell21

+0

@ Powell21你有任何编程背景吗? – Daryl

+0

我是一名SQL开发人员,刚刚交给我 – Powell21

0

您的代码应如下所示: 在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"); 
        } 
       }        
      } 
     } 
    } 
} 
+0

不幸的是,这使得您可以拥有多个活动账户计划。谢谢你的帮助,我不指望你为我做这件事,但我完全与JS – Powell21

相关问题