web
You’re offline. This is a read only version of the page.
close
Skip to main content
Community site session details

Community site session details

Session Id :
Customer experience | Sales, Customer Insights,...
Suggested answer

lookup field value dispaly [object Object value]

(0) ShareShare
ReportReport
Posted on by 37

Hi,

I have a button in my Order screen and when i click on that, it copies and creates a new order with the details from the previous order. While copying the values, one of my lookup field value is displayed with [object Object] value by default and when i refresh the page, the correct value gets set.

Can some guide me on this. The js behind the code is:

function CopyBooking() {
    var args = window.dialogArguments;
    var arg = args.split("|");
    var serverurl = arg[0];
    var entityId = arg[2];
    var departureId = document.getElementById("dropdown").value;
    var tripId = document.getElementById("tripdropdown").value;
    if ((departureId == null) || (departureId == 0) || (tripId == null) || (tripId == 0)) {
        alert("Error!");
        return;
    }
    CopybookingTodeparture(departureId, tripId, entityId);
}
function CopybookingTodeparture(value1, value2, id) {
    var args = window.dialogArguments;
    var arg = args.split("|");
    var serverurl = arg[0];
    var odata = serverurl + "/xrmservices/2011/OrganizationData.svc/";
    var url = odata + "col_bookingSet(guid'" + id + "')";

    $.ajax({
        type: 'POST',
        contentType: "application/json; charset=utf-8",
        url: url,
        dataType: 'json',
        data: window.JSON.stringify({
            col_copiedbookingid: value1,
            col_copiedtripId: value2
        }),
        beforeSend: function (XMLHttpRequest) {
            XMLHttpRequest.setRequestHeader("Accept", "application/json");
            XMLHttpRequest.setRequestHeader("X-HTTP-Method", "MERGE");
        }
    })
    .done(function (d, s, q) {
parent.window.location.reload();
        window.close();
       top.window.location.reload();
    })
    .fail(function (jqxhr, status, err) {
        var er = JSON.parse(jqxhr.responseText);
        if (er && er.error) {
            alert(er.error.message.value);
        } else {
            alert("Error updating booking with Id '" + id + "': " + err);
        }

    });
}

I have the same question (0)
  • Community Member Profile Picture
    on at
    RE: lookup field value dispaly [object Object value]

    Aside from what looks like a lot of unsupported script, it appears you would have to decompose value2 into it's 3 components as arguments to making a new lookup as Nagaraj's function does from line 13 through 22 -- though you could just do it in place [{ id: value2[0].id, ...}]

  • CRM Reports Profile Picture
    37 on at
    RE: lookup field value dispaly [object Object value]

    Hi Nagaraj,

    The record is saved through code. I tried to update my code with the setvalue but still no luck in it. I have a belief that, this could be because of the json stringfy  code, what is your idea?

    My code is something like this:

    $.ajax({

           type: 'POST',

           contentType: "application/json; charset=utf-8",

           url: url,

           dataType: 'json',

           data: window.JSON.stringify({

               col_copiedbookingid: value1,

               col_copiedtripId: value2

           }),

  • Suggested answer
    LeoAlt Profile Picture
    16,331 Moderator on at
    RE: lookup field value dispaly [object Object value]

    Hi partner,

    I've seen all the code you provided and there is no assignment(set value to field) function code.

    You should focus on the setting value steps in your code and please refer to the following format when setting value to lookup field.

    var functionName = function (executionContext) {
    
        // Access the field on the form
        var field = executionContext.getFormContext().getAttribute("parentaccountid");
    
        // Verify it does exist on the form
        if (field != null) {
    
            // Get its field value; Returns the Lookup object
            var value = field.getValue();
    
            // To get the Id, Name and Entity Name (account/contact)
            var reocord_id = field.getValue()[0].id;
            var record_name = field.getValue()[0].name;
            var record_entityName = field.getValue()[0].entityType;
    
            // Set its field value
            field.setValue([{
                id: reocord_id,
                name: record_name,
                entityType: record_entityName
            }]);
        }
    }

    As we can see in above code, we should set three parameters to lookup fields: id, label,entity logic name.

    Please provide the code like "Xrm.Page.getAttribute("fieldname").setValue()" or "Context.getAttribute("fieldname").setValue()" which are used to set values to fields.

    Best Regards,

    Leo

  • Nagaraj_Matta Profile Picture
    55 on at
    RE: lookup field value dispaly [object Object value]

    Hi Greetings !

    Whatever the code you have posted it's somewhat clear but not fully understandable .

    for example fillDepartureDropdown this function you have called the same function inside it again.

    to populate the copied data usually we pass through arguements then we read them in the onload event and populate the respective fields.

    but in your code it seems you are updating some dropdown (html object) , and you are reading that through html tag .

    are you using any custom html as well?

    so let me understand this

    1..once you click on copy button, whether new copied  order record is saved through code, or user manually need to save the record.?

    2.if it's not saved through code, so can we save the record through code , or user needs to update any data before saving the record.

  • CRM Reports Profile Picture
    37 on at
    RE: lookup field value dispaly [object Object value]

    Hi Nagaraj,

    Thanks for your explanation. Now, I can understand it! why it needs a refresh to display the correct value.

    But can you guide me how and where exactly this code need to be updated in my code to fetch it correctly.

    Thanks in advance.

  • Nagaraj_Matta Profile Picture
    55 on at
    RE: lookup field value dispaly [object Object value]

    Hi Greetings!

    basically lookup has three properties and when we set the value through javascript we follow below method , to set any lookup field.

    In your case you are not populating the name property that the reason it's not able to display the record name, the moment you save the record it's going to CRM DB and fetching the name based on GUID you provided and populating the name ( text representation of the record) in that field.

    var lookUpObj = [];

       lookUpObj[0] = {};

       lookUpObj[0].id = recordId;

       lookUpObj[0].entityType = entitySchemaName;

       lookUpObj[0].name = recordName;

    Xrm.Page.getAttribute(lookUpSchemaName).setValue(lookUpObj);

    Thanks,

    Nagaraju

  • CRM Reports Profile Picture
    37 on at
    RE: lookup field value dispaly [object Object value]

    departureId is the field which has lookup value, I am new to this and not sure whether you need any more code on this. The js itself is a big code, i am not sure which exact code you require for this.

    function OpenNewBooking() {

       //debugger;

       var serverurl = Xrm.Page.context.getClientUrl();

       var query = "col_bookingSet?$select=col_bookingid2&$filter=col_bookingId eq guid'" + entityid + "'";

       query = serverurl + "/xrmservices/2011/OrganizationData.svc/" + query;

       var results;

       $.ajax({

           type: "GET",

           contentType: "application/json; charset=utf-8",

           datatype: "json",

           async: false,

           url: query,

           beforeSend: function (XMLHttpRequest) {

               XMLHttpRequest.setRequestHeader("Accept", "application/json");

           },

           success: function (data, textStatus, XmlHttpRequest) {

               //debugger;

               results = data.d.results;

               var bookingId = results[0].col_bookingid2.Id;

               Xrm.Utility.openEntityForm("col_booking", bookingId);

               $("#mainCopyContent").show();

               $("#confirmCopyContent").hide();

               //closeWindow();

           }

       });

    --------------

    function fillDepartureDropdown() {

       //debugger;

       var args = window.getDialogArguments();

       serverurl = args["serverurl"];

       currentdepartureid = args["iden"];

       entityid = args["entityid"];

       _cancel = args["_cancel"];

       var date = new Date();

       var departureFetchXML = '<fetch version="1.0" output-format="xml-platform" mapping="logical" distinct="false" >' +

           '  <entity name="col_departure" >' +

           '    <attribute name="col_name" />' +

           '    <attribute name="col_departureid" />' +

           '    <attribute name="col_departurenumber" />' +

           '    <attribute name="col_startdate" />' +

           '   <attribute name="col_registered" />' +

           '   <attribute name="col_participantsallowed" />' +

           '   <order attribute="col_name" descending="false" />' +

           '    <filter type="and" >' +

           '      <condition attribute="col_startdate" operator="ge" value="' + date.getFullYear() + '-' + date.getMonth() + '-' + date.getDay() + '" />' +

           '    </filter>' +

           '  </entity>' +

           '</fetch>';

       var encodedFetchXML = encodeURIComponent(departureFetchXML);

       var uri = "col_departures?fetchXml=" + encodedFetchXML;

       SendHttpRequest(uri, null, 'GET', function (e) {

           //debugger;

           var data = JSON.parse(this.response)

           if (this.status == 200) {

               FillDepartureDropDown(data.value);

           }

       });

    }

    Kindly check and let me know if there could be something else you wanted to refer to.

    Thanks

  • Suggested answer
    a33ik Profile Picture
    84,331 Most Valuable Professional on at
    RE: lookup field value dispaly [object Object value]

    Hello,

    I don't see in your code any place where you work with lookups. If you work with lookups can you please post this code here?

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Andrés Arias – Community Spotlight

We are honored to recognize Andrés Arias as our Community Spotlight honoree for…

Leaderboard > Customer experience | Sales, Customer Insights, CRM

#1
Rishabh Kanaskar Profile Picture

Rishabh Kanaskar 258

#2
Daniyal Khaleel Profile Picture

Daniyal Khaleel 180

#3
Tom_Gioielli Profile Picture

Tom_Gioielli 92 Super User 2025 Season 2

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans