set attribute-lookup value with retrieved lookup attribute from odata endpoint


  • Hi guys!


    I´m trying to set a custom-lookup field in my entity with the retrieved lookup-data I got via odata query.

    Obviously I can´t because nothing happens, no exception is thrown and the lookup-field is not set at all.


    the code:

    var oDataSvcPath = "/" + Xrm.Page.context.getOrgUniqueName() + "/XrmServices/2011/OrganizationData.svc";

    function retrieveRecordByGuid(id, entityName, guid, successCallback, errorCallback)
        //-- cut unnecessery characters
        guid = guid.replace("{", "").replace("}","");

        //id is required
        if (!id) {
            alert("record id is required.");
        //entityName is required, i.e. "AccountSet"
        if (!entityName) {
            alert("entityName is required.");

        //Asynchronous AJAX function to Retrieve a CRM record using OData
            type: "GET",
            contentType: "application/json; charset=utf-8",
            datatype: "json",
            url: oDataSvcPath + "/"+entityName+"Set?$select=*&$filter=FirstName eq 'test",
            beforeSend: function (XMLHttpRequest) {
                //Specifying this header ensures that the results will be returned as JSON.            
                XMLHttpRequest.setRequestHeader("Accept", "application/json");
            success: function (data, textStatus, XmlHttpRequest) {
                if (successCallback) {
                    successCallback(data.d.results[0], textStatus, XmlHttpRequest);                             
            error: function (XmlHttpRequest, textStatus, errorThrown) {
                if (errorCallback)
                    errorCallback(XmlHttpRequest, textStatus, errorThrown);
                    errorHandler(XmlHttpRequest, textStatus, errorThrown);

    function setDefaultBU(entity, textStatus, XmlHttpRequest)

                //--- this field is existing and has the type of a bu-lookup, but setting the value with the businessunitid does not work, why ??????????
        catch (ex)
            alert("error: "+ex.description);

    function setBUtoOwnersBU()
        //-- get Owner-guid
        var ownerId = Xrm.Page.getAttribute("ownerid").getValue()[0].id;
        //-- try to get filtered user
        retrieveRecordByGuid(ownerId, "SystemUser", ownerId, setDefaultBU, null);

    function errorHandler(xmlHttpRequest, textStatus, errorThrow) {
        alert("Error occured: " + textStatus + " --> " + xmlHttpRequest.statusText);

    Dienstag, 18. Januar 2011 12:20