How to fetch Records from Lookup and set one record to that lookup on form load in CRM 2011 ? RRS feed

  • Question

  • Hi,

    Can we get the records of lookup on form load

    and if we get the records then can we set first record to lookup field ?

    If this post answers your question, please click "Mark As Answer" on the post and "Mark as Helpful"

    Thursday, August 8, 2013 8:31 AM


  • 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";

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

    Hope this helps!

    • Marked as answer by DynamicsCRM31 Monday, August 12, 2013 6:35 AM
    Thursday, August 8, 2013 9:20 AM