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

Announcements

No record found.

News and Announcements icon
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)
  • Suggested answer
    a33ik Profile Picture
    84,331 Most Valuable Professional on at

    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?

  • CRM Reports Profile Picture
    37 on at

    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

  • Nagaraj_Matta Profile Picture
    55 on at

    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

    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

    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.

  • Suggested answer
    LeoAlt Profile Picture
    16,331 Moderator on at

    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

  • CRM Reports Profile Picture
    37 on at

    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

           }),

  • Community Member Profile Picture
    on at

    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, ...}]

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

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Stars!

Meet the Microsoft Dynamics 365 Contact Center Champions

We are thrilled to have these Champions in our Community!

Congratulations to the March Top 10 Community Leaders

These are the community rock stars!

Leaderboard > Customer experience | Sales, Customer Insights, CRM

#1
ManoVerse Profile Picture

ManoVerse 184 Super User 2026 Season 1

#2
11manish Profile Picture

11manish 125

#3
CU11031447-0 Profile Picture

CU11031447-0 100

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans