使用 javascript 从 crm 2015 中的查找字段获取值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33369129/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 16:25:34  来源:igfitidea点击:

get a value from lookup field in crm 2015 using javascript

javascriptdynamics-crm-2015

提问by user5493786

i can get the id of the contact but who i get the email of this contact ?????

我可以获得联系人的 ID,但我会收到此联系人的电子邮件????

function getdata(){
var entityName, entityId, entityLabel, lookupFieldObject;

    // parentaccountid is the lookup field name that we try to reach its values
    lookupFieldObject = Xrm.Page.data.entity.attributes.get('mbmhr_employee');
    if (lookupFieldObject.getValue() != null) {
        entityId = lookupFieldObject.getValue()[0].id;
        entityName = lookupFieldObject.getValue()[0].entityType;
        entityLabel = lookupFieldObject.getValue()[0].name;

Xrm.Page.getAttribute("mbmhr_test22").setValue(entityLabel );    

    }    
}

回答by James Wood

You need to query the server for additional details of related records.

您需要向服务器查询相关记录的其他详细信息。

Have a look at Getting started with CRM 2011 JavaScript REST (OData) Web Service Callsand Retrieve Data using OData queries with Javascript in CRM 2013to get you going in the right direction.

查看CRM 2011 入门 JavaScript REST (OData) Web 服务调用使用 OData 查询和 CRM 2013 中的 Javascript 检索数据,让您朝着正确的方向前进。

回答by Alex

OData endpoint, once again, to the rescue:

OData 端点再次提供帮助:

var contactId = null;
try { contactId = Xrm.Page.getAttribute('mbmhr_employee').getValue()[0].id; } catch(ex) { contactId = null; }
if(contactId !== null)
{
    var req = new XMLHttpRequest();
    var url = Xrm.Page.context.getClientUrl() + "/XRMServices/2011/OrganizationData.svc/ContactSet(guid'" + contactId + "')?$select=EMailAddress1";
    req.open("GET", url, true);
    req.setRequestHeader("Accept", "application/json");
    req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
    req.onreadystatechange = function() {
        if(req.readyState == 4){
            var data = JSON.parse(req.responseText);
            // use data.d.EmailAddress1 
        }
    };
    req.send(null);
}