locked
O data Many to Many Realtionship records RRS feed

  • Question

  • Event Registration entity has M:M relationship with Contact entity

    I am using O data query designer tool to make query...

    but event registration don't retrieve the related contacts records.

    function Event_onchange() {
     
    var eventRegistrationId = Xrm.Page.getAttribute("eventRegistration").getValue() != null ? Xrm.Page.getAttribute("eventRegistration").getValue()[0].id : null;   
                         var contacts=GetContacts(eventRegistrationId);
                          var resultcount=contacts[0].results.length;
    alert(resultcount);
    
        }
    }
    
    
    function GetContacts(eventRegistrationId) {
        var serverUrl = Xrm.Page.context.getServerUrl();
        var oDataUri = serverUrl + "/xrmservices/2011/OrganizationData.svc/new_eventregistrationSet?$select=new_new_eventregistration_contact/ContactId,new_new_eventregistration_contact/FullName&$expand=new_new_eventregistration_contact&$filter=new_eventregistrationId eq guid'"+eventRegistrationId+"'";
        var jSonArray = new Array();
    
        jQuery.ajax({
            type: "GET",
            contentType: "application/json; charset=utf-8",
            datatype: "json",
            url: oDataUri,
            async: false,
            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 (data && data.d != null) {
                    jSonArray.push(data.d);
                }
            },
            error: function (XmlHttpRequest, textStatus, errorThrown) {
                alert("Error :  has occured during retrieval of the Account details");
            }
        });
    
        return jSonArray;
    }


    Muhammad Sohail

    Thursday, January 30, 2014 2:53 PM

All replies

  • Something like this maybe:

    $.ajax({
        type: 'GET',
        contentType: 'application/json; charset=utf-8',
        datatype: 'json',
        url: Xrm.Page.context.getClientUrl() + "/XRMServices/2011/OrganizationData.svc/new_eventregistrationSet?" +
            "$select=new_new_eventregistration_contact/FullName&$expand=new_new_eventregistration_contact&" +
            "$filter=new_eventregistrationId eq guid'C3B92468-CA89-E311-8142-6C3BE5A85114'",
        beforeSend: function (XMLHttpRequest) {
            XMLHttpRequest.setRequestHeader('Accept', 'application/json');
        },
        async: true,
        success: function (data, textStatus, xhr) {
            var results = data.d.results;
            var new_new_eventregistration_contact_FullName = results[0].new_new_eventregistration_contact.results[0].FullName;
            alert(new_new_eventregistration_contact_FullName);
        },
        error: function (xhr, textStatus, errorThrown) {
            alert(textStatus + ' ' + errorThrown);
        }
    });


    Jason Lattimer
    My Blog -  Follow me on Twitter -  LinkedIn

    Friday, January 31, 2014 3:39 AM
    Moderator
  • Hi JLattimer thanks for reply

    I have still issue it give me only one records name, basically I have contact grid on event registration Entity and have M:M relation.

    I have 4 recodes of contact in grid but it Odata Jquery  return only one record.

    I have used loop also to check other records but it is getting still 1 records while in real it has 4 records of contact

    var results = data.d.results;
    for(var count=0; count<results[0].length; count++)
    {
            var new_new_eventregistration_contact_FullName = results[0].new_new_eventregistration_contact.results[0].FullName;
            alert(new_new_eventregistration_contact_FullName);
    }


    Muhammad Sohail


    • Edited by sohail450 Friday, February 7, 2014 7:37 AM
    Friday, February 7, 2014 7:36 AM
  • Hi Muhammad,

    pls check your index.

    you have to use [i] for many results. With [0] you can only get the first contact.

    Regards, Karan

    Friday, February 7, 2014 8:54 AM