从查找中获取所选用户

时间:2013-10-01 12:39:52

标签: javascript dynamics-crm-2011 dynamics-crm dynamics-crm-online

在我的场景中,我有一个自定义实体表单。

此自定义实体表单正在查找用户记录,我的要求是从javascript中获取所选用户。

var lookupObject = Xrm.Page.getAttribute("new_accountmanager0");

if (lookupObject != null) 
{

    var lookUpObjectValue = lookupObject.getValue();

    if ((lookUpObjectValue != null))         
    {
     var lookuptextvalue = lookUpObjectValue[0].name;
     var lookupid = lookUpObjectValue[0].id;
     }

  }

我希望获得用户“phonenumber”,这是使用JavaScript查找的专栏!

1 个答案:

答案 0 :(得分:1)

您需要调用CRM网络服务来检索电话号码,您可以使用此功能(如果您的查找指向系统用户,这将有效)

function getPhoneNumberBySystemUserId(userId) {
var serverUrl;
if (Xrm.Page.context.getClientUrl !== undefined) {
    serverUrl = Xrm.Page.context.getClientUrl();
} else {
    serverUrl = Xrm.Page.context.getServerUrl();
}
// build the request
var ODataPath = serverUrl + "/XRMServices/2011/OrganizationData.svc"; 
var userRequest = new XMLHttpRequest();
userRequest.open("GET", ODataPath + "/SystemUserSet(guid'" + userId + "')", false); 
userRequest.setRequestHeader("Accept", "application/json"); 
userRequest.setRequestHeader("Content-Type", "application/json; charset=utf-8");
// execute the request
userRequest.send();
if (userRequest.status === 200) {
    var retrievedUser = JSON.parse(userRequest.responseText).d;
    // retrieve the phone number of the user
    var phonenumber = retrievedUser.Address1_Telephone1; // change to MobilePhone to get the mobile phone, to HomePhone to get the home phone
    return phonenumber;
} else {
    return "error";
}
}

您可以使用以下方式使用您的代码:

var lookupObject = Xrm.Page.getAttribute("new_accountmanager0");
var lookUpObjectValue = lookupObject.getValue();
if (lookUpObjectValue != null) {
    var phonenumber = getPhoneNumberBySystemUserId(lookUpObjectValue[0].id);
    alert(phonenumber);
}