user5493786
user5493786

Reputation: 41

get a value from lookup field in crm 2015 using javascript

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

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 );    

    }    
}

Upvotes: 2

Views: 15141

Answers (2)

Alex
Alex

Reputation: 23300

OData endpoint, once again, to the rescue:

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);
}

Upvotes: 1

James Wood
James Wood

Reputation: 17552

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 Calls and Retrieve Data using OData queries with Javascript in CRM 2013 to get you going in the right direction.

Upvotes: 1

Related Questions