Hi,
Do you mean you have lookup on some particular entity? You want to retrieve all records those are of type lookup.entitytype and set the first record to lookup. etrieve
You can use the below query.
function retriveAndSetLookUp() {
var serverUrl = Xrm.Page.context.getServerUrl();
// For all the records
var oDataUrl = serverUrl + "/xrmservices/2011/OrganizationData.svc/YourEntityTypeSet"
// If you want Top 1 record
//oDataUrl ="/xrmservices/2011/OrganizationData.svc/YourEntityTypeSet?$top=1";
jQuery.ajax({
type: "GET",
contentType: "application/json; charset=utf-8",
datatype: "json",
url: oDataUrl,
async: false,
beforeSend: function (XMLHttpRequest) {
XMLHttpRequest.setRequestHeader("Accept", "application/json");
},
success: function (data, textStatus, XmlHttpRequest) {
ProcessReturnedEntities(data.d.results);
},
error: function (XmlHttpRequest, textStatus, errorThrown) { alert('OData Select Failed: ' + odataSelect); }
});
}
//Get Entities
function ProcessReturnedEntities(ManyEntities) {
var oneEntity = ManyEntities[0];
var recordIdAttribute = oneEntity.fieldId;
var recordIdValue = oneEntity.ValueField;
SetLookupValue("field Name", recordIdAttribute, recordIdValue, "Entity Type");
}
// Set the value of a lookup field
function SetLookupValue(fieldName, id, name, entityType) {
if (fieldName != null) {
var lookupValue = new Array();
lookupValue[0] = new Object();
lookupValue[0].id = id;
lookupValue[0].name = name;
lookupValue[0].entityType = entityType;
Xrm.Page.getAttribute(fieldName).setValue(lookupValue);
}
}
Hope this helps!