locked
Create table to show contacts related to an account RRS feed

  • Question

  • Hi

    Iam trying to create a dynamic table on account form to show all the related contacts but iam unable to see any contacts on account using this code.Any change sto be made??

    <html><head>
        <title>Contacts</title>
        <script src="Script_JQuery_1.7.1.min"></script>
        <script src="ClientGlobalContext.js.aspx"></script>
        <script language="javascript" type="text/javascript">
            function loadContacts() {
                var parentcustomerid = Xrm.Page.data.entity.getId();
    			var contactDetails = GetRecords(parentcustomerid);
                if (contactDetails != null && contactDetails.length > 0) {
    			var recordCount = contactDetails[0].results.length.toString();
                    var tableData = "";
                    for (var i = 0; i < recordCount; i++) {
                        var service = recordCount[i].fullname;
                        if (service != null) {
                            //dynamically add table data with Service Names
                            tableData = tableData + "<tr><td>" + service + "</td></tr>";
                        }
                    }
                    //Create HTML table
                    var table = "<table style='font-family:Segoe UI;font-weight:normal;font-size:13px;'><tr style='height:20px'><td style='text-decoration:underline;'>Contacts</td></tr>" + tableData + "</table>";
                    //show table data on the Account form
                    window.document.writeln(table);
                }
            }
            //get Contacts
    function GetRecords(parentcustomerid) {
        var serverUrl = Xrm.Page.context.getClientUrl();
        var oDataUri = serverUrl + "/xrmservices/2011/OrganizationData.svc/ContactSet?$filter=ParentCustomerId/Id eq guid'" + parentcustomerid + "'";
        var contactDetails = null;
    
        $.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 != null && data.d.results.length > 0) {
                            contacts = data.d.results;
                        }
            },
            error: function (XmlHttpRequest, textStatus, errorThrown) {
                alert("Error :  has occured during retrieval of the contacts");
            }
        });
    
        return contacts;
    }
        </script>
    <meta charset="utf-8"></head>
    <body>
    </body></html>
    Thank you
    Wednesday, June 24, 2015 10:47 AM

All replies

  • why you don't use a standard subgrid?

    My blog: www.crmanswers.net - CRM Theme Generator

    Wednesday, June 24, 2015 11:59 AM
  • Guido has a good point...just use a sub-grid.

    If for some reason that isn't what you want to do, I would debug the code and see if your AJAX success function is getting anything back? 

    You could also manually take the oDataUri JSON call and paste it into your browser address bar to see what's coming back there, could be a syntax problem in that URL.  I know the URLs are case sensitive, so a simple case issue can throw off your results.

    Wednesday, June 24, 2015 1:01 PM
  • Hello,

    If you really need custom code for it, and not a subgrid, you should try the SDK.REST.js library available in CRM (references json2.js). It handles the ajax query party pretty good.


    Cornel Croitoriu - Senior Software Developer

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

    Biz-Forward.comCroitoriu.NET

    Thursday, June 25, 2015 8:17 AM
  • Hi

    In your GetRecords function the oDataUri should look as follows.

    <CRMUri>/XRMServices/2011/OrganizationData.svc/AccountSet(guid'<id>')/contact_customer_accounts

    Thursday, June 25, 2015 11:12 AM