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

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Getting Parent Entity in Code

(0) ShareShare
ReportReport
Posted on by

Situation

I have an entity (party) that is a child (parental relationship) to the matter entity.  I am a beginner at coding Javascript and now realize that when I am accessing the entity behind the party form, I am accessing the form contents and not necessarily all the fields for the entity. 

So, on the party form I can't get the ID of the parent matter unless I place the matter lookup field on the form.

Is there a way in code to obtain the parent MatterID without placing unneeded fields on the form?

Thanks,
Glen

P.S. - This is how it works IF the matterID field is somewhere on the form.

var sourceMatter = parent.Xrm.Page.ui.controls.get("matterID").getAttribute().getValue();

*This post is locked for comments

I have the same question (0)
  • Suggested answer
    jlattimer Profile Picture
    24,562 on at

    The easiest way if just to place the fields you need on the form. If nobody needs to see/access them, put them in a separate section and turn off the the option to make it visible - this way the fields technically still render on the form and can be access via JavaScript but users won't be bothered with extra information. 

    But if this really isn't an option, you can make a call to the OData endpoint and retrieve values from the record (or other records).

    This example looks up the Parent Customer of a Contact - running from a Contact record - hope it helps

    var serverUrl = Xrm.Page.context.getClientUrl();
    var oDataSelect = serverUrl + "/XRMServices/2011/OrganizationData.svc/ContactSet?$select=ParentCustomerId&$filter=ContactId eq guid'" + Xrm.Page.data.entity.getId() + "'";
    
    var retrieveReq = new XMLHttpRequest();
    retrieveReq.open("GET", oDataSelect, false);
    retrieveReq.setRequestHeader("Accept", "application/json");
    retrieveReq.setRequestHeader("Content-Type", "application/json;charset=utf-8");
    retrieveReq.onreadystatechange = function () {
    	if (retrieveReq.readyState == 4) {
    		if (retrieveReq.status == 200) {
    			var retrieved = JSON.parse(retrieveReq.responseText).d;
    			alert(retrieved.results[0].ParentCustomerId.Id);
    		}
    	};
    }
    retrieveReq.send();
    
  • Glen W. Profile Picture
    on at

    Jason,

    Thanks for your response.

    I thought as much.  I placed the id field as you described and got everything working with one little problem:

    The form comes up and populates, but the data in my web resource "flashes" once.  It displays the data then in clears and reappears in less that a second.  It's very quick, but very annoying.  Any ideas on why that's happening?  I've included my code below.

    Thanks,

    Glen

    <!DOCTYPE html>

    <html xmlns="www.w3.org/.../xhtml">

    <head>

       <title>Display Name Resolution</title>

       <script src="/WebResources/ClientGlobalContext.js.aspx"></script>

       <script type="text/javascript" src="mm_jquery1.4.1.min.js"></script>

       <script type="text/javascript" src="mm_json2.js"></script>

       <meta charset="utf-8" />

    </head>

    <body style="MARGIN: 0px;" contenteditable="true" onload="onload()">

       <div id="CMDisplay"></div>

       <script>

           var ODataPath;

           var serverUrl;

           var background = "transparent";

           var labelFont = "Arial";

           var dataFont = "Arial";

           var labelFontSize = "11px";

           var dataFontSize = "11px";

           var labelBold = "no";

           var dataBold = "no";

           var matterEntity;

           var clientEntity;

           function onload() {

               init();

           }

           function init() {

               serverUrl = parent.Xrm.Page.context.getServerUrl();

               ODataPath = serverUrl + "/XRMServices/2011/OrganizationData.svc";

               GetParameters();

               var sourceMatterField = parent.Xrm.Page.ui.controls.get("mm_matterid").getAttribute().getValue();

               if (sourceMatterField != null) {

                   sourceMatterId = sourceMatterField[0].id.replace('{', '').replace('}', '');

                   if (sourceMatterField[0].entityType.toLowerCase() == "cpdc_matter")

                       retrieveMatter(sourceMatterId);

               }

           }

           function DisplayResults() {

               // Set formatting

               CMDisplay.style.padding = "2px";

               CMDisplay.style.verticalAlign = "middle";

               CMDisplay.style.height = "100%";

               //background

               if (background != null)

                   CMDisplay.style.backgroundColor = background;

               // Client Label

               var html = '<table><tr><td id="clientLabel" style="font-Family: ' + labelFont + '; font-Size: ' + labelFontSize + '; font-Weight: ' + labelBold + ';">';

               html += 'Client:</td>';

               // Client Data

               html += '<td id="clientInfo" style="font-Family: ' + dataFont + '; font-Size: ' + dataFontSize + '; font-Weight: ' + dataBold + ';">';

               html += clientEntity["cpdc_name"] + " - " + clientEntity["cpdc_clientname"];

               html += '</td></tr>'

               // Matter Label

               html += '<tr><td id="matterLabel" style="font-Family: ' + labelFont + '; font-Size: ' + labelFontSize + '; font-Weight: ' + labelBold + ';">';

               html += 'Matter:</td>';

               // Client Data

               html += '<td id="matterInfo" style="font-Family: ' + dataFont + '; font-Size: ' + dataFontSize + '; font-Weight: ' + dataBold + ';">';

               html += matterEntity["cpdc_matternum"] + " - " + matterEntity["cpdc_name"];

               html += '</td></tr></table>'

               CMDisplay.innerHTML = html;

           }

           function retrieveMatter(sourceMatterId) {

               var retrieveMatterReq = new XMLHttpRequest();

               retrieveMatterReq.open("GET", ODataPath + "/cpdc_matterSet(guid'" + sourceMatterId + "')", true);

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

               retrieveMatterReq.setRequestHeader("Content-Type", "application/json; charset=utf-8");

               retrieveMatterReq.onreadystatechange = function () {

                   retrieveMatterReqCallBack(this);

               };

               retrieveMatterReq.send();

           }

           function retrieveMatterReqCallBack(retrieveMatterReq) {

               if (retrieveMatterReq.readyState == 4 /* complete */) {

                   if (retrieveMatterReq.status == 200) {

                       //Success

                       matterEntity = JSON.parse(retrieveMatterReq.responseText).d;

                       retrieveClient(matterEntity["cpdc_clientnumberid"].Id);

                   }

                   else {

                       throw new Error("Error retrieving Record");

                   }

               }

           }

           function retrieveClient(sourceClientId) {

               var retrieveClientReq = new XMLHttpRequest();

               retrieveClientReq.open("GET", ODataPath + "/cpdc_clientinfoSet(guid'" + sourceClientId + "')", true);

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

               retrieveClientReq.setRequestHeader("Content-Type", "application/json; charset=utf-8");

               retrieveClientReq.onreadystatechange = function () {

                   retrieveClientReqCallBack(this);

               };

               retrieveClientReq.send();

           }

           function retrieveClientReqCallBack(retrieveClientReq) {

               if (retrieveClientReq.readyState == 4 /* complete */) {

                   if (retrieveClientReq.status == 200) {

                       //Success

                       clientEntity = JSON.parse(retrieveClientReq.responseText).d;

                       DisplayResults();

                   }

                   else {

                       throw new Error("Error retrieving Record");

                   }

               }

           }

           function GetParameters() {

               var querystring = unescape(window.location.search.replace('?', '').replace('data=', ''));

               var params = querystring.split('&');

               for (var i = 0; i < params.length; i++) {

                   var param = params[i].split('=');

                   switch (param[0]) {

                       case "background":

                           background = param[1];

                           break;

                       case "labelFont":

                           labelFont = param[1];

                           break;

                       case "dataFont":

                           dataFont = param[1];

                           break;

                       case "labelFontSize":

                           labelFontSize = param[1];

                           break;

                       case "dataFontSize":

                           dataFontSize = param[1];

                           break;

                       case "labelBold":

                           switch (param[1]) {

                               case "1":

                               case "true":

                               case "True":

                               case "yes":

                               case "Yes":

                                   labelBold = "bold";

                                   break;

                               default:

                                   labelBold = "normal";

                                   break;

                           }

                       case "dataBold":

                           switch (param[1]) {

                               case "1":

                               case "true":

                               case "True":

                               case "yes":

                               case "Yes":

                                   dataBold = "bold";

                                   break;

                               default:

                                   dataBold = "normal";

                                   break;

                           }

                   }

               }

           }

       </script>

       <script type="text/javascript" src="/MillerMartin/_common/global.ashx?ver=-1332998345"></script>

       <script type="text/javascript" src="/MillerMartin/_common/windowinformation/windowinformation.js.aspx?lcid=1033&ver=-1332998345"></script>

       <script type="text/javascript" src="/MillerMartin/_common/entityproperties/entitypropertiesutil.js.aspx?tstamp=453040634&ver=-1332998345"></script>

    </body>

    </html>

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…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans