我正在开发一个 Dynamics 365 项目,我需要将登录用户的 Azure AD ID 与表单字段中的值进行比较,并根据比较结果显示图标。我的代码尝试执行此比较,但未按预期工作。
我正在 Dataverse 视图中实现此代码,该视图有 2 列,如下所示
function compareUser(executionContext) {
// Get Current Logged in user ID which is used to retrieve AAD id
var globalContext = Xrm.Utility.getGlobalContext();
var contextVal = globalContext.userSettings;
var loggedinUserId = contextVal.userId.replace("{", "").replace("}", "");
// Get value from field
var fieldValID = JSON.parse(executionContext).suyog_userlist_Value.id.guid;
// Pass both ids for comparison in function and return 0 or 1
var finalOutput = fetchAndCompareUserIds(loggedinUserId, fieldValID);
// Act based on returned value
switch (finalOutput) {
case 1:
console.log("User matched");
return ["suyog_testgreenicon", "Green"]; // User matched
case 0:
console.log("User doesn't match");
return ["suyog_testredicon", "Red"]; // User did not match
}
}
async function fetchAndCompareUserIds(loggedinUserId, fieldValID) {
try {
const result = await Xrm.WebApi.online.retrieveRecord("systemuser", loggedinUserId, "?$select=azureactivedirectoryobjectid");
var fetched_aad_id = result["azureactivedirectoryobjectid"];
console.log("Comparing " + fieldValID + " with " + fetched_aad_id);
if (fieldValID == fetched_aad_id) {
console.log("Match Found");
return 1;
} else {
return 0;
}
} catch (error) {
console.log(error.message);
}
}
另外,在控制台中,当我调试时,它说承诺待定,对此不确定。
对
fetchAndCompareUserIds(loggedinUserId, fieldValID)
的调用会返回一个 Promise,这就是为什么您会在控制台中看到“Promise {pending }”。
由于您需要异步可用的实际值,因此您应该等待承诺履行。您可以使用
await
来做到这一点,但是您的函数需要声明为 async
:
async function compareUser(executionContext) {
// ...
var finalOutput = await fetchAndCompareUserIds(loggedinUserId, fieldValID);
// ...
}