2

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

    }    
}
ArK
  • 20,698
  • 67
  • 109
  • 136
user5493786
  • 41
  • 1
  • 1
  • 3
  • 1
    This is really really helpful! I was looking for that all day. The `lookupFieldObject.getValue()[0].name` _will_ return the input value of the field upon validate and loss of focus! – scorpiophd Oct 29 '15 at 17:39

2 Answers2

1

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.

James Wood
  • 17,286
  • 4
  • 46
  • 89
1

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);
}
Alex
  • 23,004
  • 4
  • 39
  • 73