how to retrieve a value from a lookup field in CRM 2015 with JavaScript

How can I retrieve the email of a contact when I already have their ID?

function getEmailFromContactID(){
var contactId, contactName, contactEmail, contactObject;

    // mbmhr_employee is the lookup field name that we want to retrieve values from
    contactObject = Xrm.Page.data.entity.attributes.get('mbmhr_employee');
    if (contactObject.getValue() != null) {
        contactId = contactObject.getValue()[0].id;
        contactName = contactObject.getValue()[0].entityType;
        contactEmail = contactObject.getValue()[0].email;

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

    }    
}

Answer №1

In order to obtain more information about related records, you must make a request to the server.

Check out the following resources to help you get started in the correct path: Learn how to use CRM 2011 Javascript REST API and Using OData queries in CRM 2013 with Javascript.

Answer №2

Utilizing the OData endpoint once more for a solution:

let employeeId = null;
try { 
    employeeId = Xrm.Page.getAttribute('mbmhr_employee').getValue()[0].id; 
} catch(error) { 
    employeeId = null; 
}

if(employeeId !== null) {
    let request = new XMLHttpRequest();
    let url = Xrm.Page.context.getClientUrl() + "/XRMServices/2011/OrganizationData.svc/EmployeeSet(guid'" + employeeId + "')?$select=EmailAddress";
    request.open("GET", url, true);
    request.setRequestHeader("Accept", "application/json");
    request.setRequestHeader("Content-Type", "application/json; charset=utf-8");
    
    request.onreadystatechange = function() {
        if(request.readyState == 4) {
            let responseData = JSON.parse(request.responseText);
            // utilize responseData.d.EmailAddress 
        }
    };
    
    request.send(null);
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Error: React.js - operationStore.getItems function is not defined

I've encountered an issue while running my Web App with the command: node --harmony gulpfile.babel The error message states: Uncaught TypeError: operationStore.getItems is not a function I'm struggling to identify the root cause of this problem ...

A guide on extracting specific text from a div class

<div class="col_5"> <br> <i class="phone"> :: Before </i> 0212 / 897645 <br> <i class="print"> ...

The question of when to utilize userEvent.click versus fireEvent in React Testing Library

I'm currently diving into the world of React-Testing-Library. One question that I have is regarding testing mouse interaction on an element. I find myself a bit confused about whether to use userEvent.click(element) or fireEvent.click(element). Can b ...

Use PHP to highlight the row that has been selected in an HTML table

I am currently working on a project where I have styled multiple HTML tables on my website using alternating gray and white bands. Now, my goal is to highlight the selected row in a darker shade of gray. I have experimented with various solutions, and the ...

Display/hide a div containing distinct content

Upon clicking an image in the carousel, a position: absolute; div should appear containing two different images and a form related to the initial picture clicked with a submit button. How can I implement a show/hide feature that displays unique content for ...

How to prevent horizontal scrolling on the right side of a div element

Greetings! I am relatively new to coding and currently facing an issue with stopping horizontal scrolling when the second div is fully visible on the screen (while keeping the third div hidden). Here is the code snippet I have been working on: $(docum ...

What could be causing the double invocation of render() in ReactNative?

I'm currently working on an app with a single screen displaying a map centered on the user's current coordinates. In my code below, I've set the latitude and longitude to null in the state of the App component. Using the componentDidMount() ...

What is the reason behind Ramda having multiple curry functions in its source code, much like Redux having multiple compose functions?

There are _curry1, _curry2, _curry3, and _curryN functions within the depths of Ramda's source code This interesting pattern is also utilized in the redux compose function I find myself pondering: why did they choose this intricate pattern over a mo ...

AngularJS Login Popup with SpringSecurity

I have successfully integrated spring security with my AngularJS webpage utilizing Rest API. However, I am facing an issue where every time I attempt to log in using the rest api from my customized login page, it prompts me for the login credentials in a p ...

What specific blur algorithm is utilized by the Flash platform within their blur filter implementation?

I'm in the process of translating AS3 code to JavaScript, and I've come across an AS3 application that utilizes Flash's built-in Blur Filter Could someone provide insight into the blurring algorithm used by this filter or suggest ways to re ...

Struggling to retrieve information from session storage to pass along to a PHP class

Is there a way to fetch the email of the currently logged-in user from sessionStorage and send it to a PHP file? I have tried implementing this, but it seems to be not functioning properly. Could you assist me in resolving this issue? <?php $mongoCl ...

Embrace AngularJS: Employ the ".then" method and retrieve the response

In order to send a http request and receive the response of this request, I am trying to implement a mechanism where if the data is successfully saved, I can retrieve database information, and if it fails to save, I can track errors. To achieve this, I pla ...

Choosing the Angular5 model

I'm currently working with an Angular5 project that has a <select> element bound to an array of customers. Here's the code snippet: <select class="form-control" [ngModel]="record.customer_id" (ngModelChange)="setCustomer($event)" name=" ...

I am puzzled as to why my DataGrid MUI component is not functioning properly

I am taking my first steps with MUI, the MaterialUI React Component library. After following the installation instructions in the documentation, I am now attempting to integrate the DataGrid component into my React project. My goal is to replicate the fir ...

From NodeJS to full word feature module: Expand all abbreviations to enhance readability!

Is there a way to easily convert abbreviated words to their full form in a string sentence using NodeJS? For Example i'm => I am i've => I have w'ill => we will lets => Let us it's => It is I currently have the gingerb ...

Adjusting AngularJS scroll position based on key presses

I am currently working on an autocomplete feature using AngularJS. I seem to be experiencing a problem with the scrollbar behavior. In the interactive gif provided, you can observe that the scrollbar remains stationary when navigating with the arrow keys. ...

Using php variable to pass data to jquery and dynamically populate content in jquery dialog

I am facing challenges trying to dynamically display MySQL/PHP query data in a jQuery dialog. Essentially, I have an HTML table with MySQL results. Each table row has an icon next to its result inside an anchor tag with the corresponding MySQL ID. echo &a ...

Utilize jQuery to automatically assign numbers to <h1-h6> headings

Looking to develop a JavaScript function that can automatically number headings (h1- h6) for multiple projects without relying on CSS. Currently achieving this with CSS: body { counter-reset: h1; } h1 { counter-reset: h2; } h2 { counter-reset: h3; } ... T ...

Javascript function that triggers at the end of a time update

How can I ensure that the alert event is triggered only once at the 15-second mark of a 45-second video? The code snippet below initiates the alert, but how can I modify it to make sure the alert only appears once. var video = document.getElementById("m ...

Endless Loop Seamless Vertical JavaScript Experience

I've been working with an HTML table structure of data and was successful in setting up a vertical list loop using some JavaScript. However, I'm facing challenges in achieving a smooth constant vertical scroll. Currently, it goes row by row when ...