下面的代碼將返回從innner函數到父函數displayButton()的布爾值嗎?在動態CRM中單擊按鈕時會調用父功能。該函數應該返回一個布爾值,具體取決於是否選擇了一個大小寫,並且所選擇的是活動還是解析。將內部函數的返回值返回父函數-javascript,動態crm
//function called on click of a button in ms crm.
function displayButton()
{
var Obj = parent.Xrm.Page.getAttribute("regardingobjectid");
var ObjValue = Obj.getValue();
//parent.Xrm.Utility.alertDialog(" Value: " + ObjValue);
if (ObjValue == null)
return false;
//else
// parent.Xrm.Utility.alertDialog(" Hi");
var EntityType = ObjValue[0].entityType;
var Guid = ObjValue[0].id;
var id = Guid.slice(1, -1);
//parent.Xrm.Utility.alertDialog(" Guid: " + id);
//Checking if regarding field is selected a case lookup value
if (EntityType == "incident")
{
var req = new XMLHttpRequest();
req.open("GET", parent.Xrm.Page.context.getClientUrl() + "/api/data/v8.2/incidents(" + id + ")?$select=statecode", true);
req.setRequestHeader("OData-MaxVersion", "4.0");
req.setRequestHeader("OData-Version", "4.0");
req.setRequestHeader("Accept", "application/json");
req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
req.setRequestHeader("Prefer", "odata.include-annotations=\"*\"");
req.onreadystatechange = function()
{
if (this.readyState === 4)
{
req.onreadystatechange = null;
if (this.status === 200)
{
debugger;
var result = JSON.parse(this.response);
//checking if selected case is active or resolved.
var statecode = result["statecode"];
var statecode_formatted = result["[email protected]"];
if (statecode_formatted == "Active") {
return true;
}
else if (statecode_formatted == "Resolved")
return false;
else {
return false;
}
}
else
{
parent.Xrm.Utility.alertDialog("Zero");
}
}
};
req.send();
}
else {
return false;
}
}
這也不起作用。 isActive變量包含值true或false,但不會向父函數返回true或false。我寫它爲 if(isActive){ return true; } else { return false; } 這不會返回true或false到displayButton函數。 – Dinoop
它不會像你期望的那樣工作。這就是爲什麼我的答案和你的問題的其他答案都是**否**:你不能將你的異步請求的結果返回給你的父函數。如果你想使用你的內部函數的結果,你必須使用回調。如果'displayButton'本身不是父函數,也有父函數,那麼最初將回調傳遞給'displayButton',然後傳遞給'getCaseState'。如果'displayButton'是父函數,那麼你已經在使用'isActive'結果,並且不需要使用'return true'。 –
displayButton是父函數。我返回isActive通過編寫: getCaseState(關於Id,函數(狀態){0} {0} {0}} is isctive = state ===「Active」; return isActive }); crm功能區上的按鈕取決於displayButton返回值。但即使解決了案例,該按鈕也正在顯示。如果case解決時isActive爲false,則不應顯示該按鈕。 – Dinoop