我多年来一直试图解决问题,但我不知道JavaScript,所以我在网上追逐我的尾巴。
我继承了一个JavaScript文件,该文件应该在对帐户进行计划时触发。每个帐户可以有多个计划,但一次只能有一个活动计划。这意味着当您创建一个新的时,您应该只有在所有其他人都被停用时才能使用。我们现在拥有的代码(见下文)仅查找计划的存在,无论其状态如何。任何人都可以帮助我吗?
由于
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 :(得分: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");
}
结果将仅包括有效的帐户计划记录(如果有)。
答案 1 :(得分:0)
retrievedMultiple.results
是一个数组。您需要对项目执行for循环,检查其状态是否处于活动状态。
答案 2 :(得分: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");
}
}
}
}
}
}