0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

javascript using formcontext in dynamics 365

Last updated at Posted at 2022-08-25

function OnLoad() {
// グローバルコンテキストのユーザ設定情報を取得
var userSettings = Xrm.Utility.getGlobalContext().userSettings;
// ユーザ名を取得して表示
var userName = document.getElementById("userName");
userName.innerHTML = "ユーザ名は " + userSettings.userName + " です。";
// ユーザIDを取得して表示
var userId = document.getElementById("userId");
userId.innerHTML = "ユーザIDは " + userSettings.userId + " です。";
}

/*

  • ログインユーザー情報取得

*/
function GetUserInfo() {
debugger;
var result = new Array();
var loggedInUserId = Xrm.Utility.getGlobalContext().userSettings.userId;
loggedInUserId = loggedInUserId.replace('{', '').replace('}', '');
var globalContext = Xrm.Utility.getGlobalContext();
var globalUrl = globalContext.getClientUrl();
var req = new XMLHttpRequest();
req.open("GET", globalUrl + "/api/data/v9.1/systemusers(" + loggedInUserId + ")?$select=new_system_executive,internalemailaddress,fullname,firstname,lastname", false);
req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
req.setRequestHeader("Accept", "application/json");
req.setRequestHeader("OData-MaxVersion", "4.0");
req.setRequestHeader("OData-Version", "4.0");
req.onreadystatechange = function () {
if (this.readyState === 4) {
req.onreadystatechange = null;
if (this.status === 200) {
var data = JSON.parse(this.response);
result[0] = data.new_system_executive;
result[1] = data.internalemailaddress;
result[2] = data.fullname;
result[3] = data.firstname;
result[4] = data.lastname;
} else {
Xrm.Utility.alertDialog(this.statusText);
}
}
};
req.send();

return result;

}

===================================
img_5cb698b8bc827.png

***********LookUp項目取得、設定する
/

  • エンティティのOnLoadイベントに追加します。

  • @para: formContext コンテキスト
    */
    function OnLoad(executionContextObj) {
    debugger;
    var formContext = executionContextObj.getFormContext();
    var loginUserId = Xrm.Utility.getGlobalContext().userSettings.userId;
    loginUserId = loginUserId.replace('{', '').replace('}', '');
    var loginName = Xrm.Utility.getGlobalContext().userSettings.userName;

    var systemUserLookUp = new Array();
    systemUserLookUp[0] = new Object();
    systemUserLookUp[0].id = loginUserId;
    systemUserLookUp[0].name = loginName;
    systemUserLookUp[0].entityType = "systemuser";

    formContext.getAttribute("new_field_name").setValue(systemUserLookUp);

    var filedLabel, filedId, entityType, lookupFieldObject;
    lookupFieldObject = formContext.getAttribute("new_lookup_user");
    if (lookupFieldObject != null) {
    if (lookupFieldObject.getValue() != null) {
    filedLabel = lookupFieldObject.getValue()[0].name;
    filedId = lookupFieldObject.getValue()[0].Id;
    entityType = lookupFieldObject.getValue()[0].entityType;
    }
    }

}

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?