WEB API- Part 5 : Get lookup text

Get lookup text using WEB API

Following code will fetch the originating lead lookup text and Id from an opportunity .

var req = new XMLHttpRequest();
req.open("GET", Xrm.Page.context.getClientUrl() + "/api/data/v8.0/opportunities?$select=_originatingleadid_value&$filter=_originatingleadid_value ne null", true);
req.setRequestHeader("OData-MaxVersion", "4.0");
req.setRequestHeader("OData-Version", "4.0");
req.setRequestHeader("Accept", "application/json");
req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
req.setRequestHeader("Prefer", "odata.include-annotations=\"OData.Community.Display.V1.FormattedValue\"");
req.onreadystatechange = function () {
    if (this.readyState === 4) {
        req.onreadystatechange = null;
        if (this.status === 200) {
            var results = JSON.parse(this.response);
            for (var i = 0; i < results.value.length; i++) {
                 var _originatingleadid_value = results.value[i]["_originatingleadid_value"];// Id
                 var _originatingleadid_value_formatted = results.value[i]["_originatingleadid_value@OData.Community.Display.V1.FormattedValue"];// text value
            }
        }
        else {
            alert(this.statusText);
        }
    }
};
req.send();
Advertisements