You are currently browsing the tag archive for the ‘Web Resources’ tag.

Welcome back. This is a follow-up to a previously posted article which was showing how to get to some basic data from the entity hosting our web resource. The article is HERE.

In this part we take a step further. We have the web resource loaded on the Account entity, and we need to get to the related Cases. For this post, we’re only going to be looking at getting a count of related Cases, as well as some basic data.

For the sake of simplicity, we’re going to include JavaScript and CSS in the HTML resource. You should always separate the resources though.

Let’s start with a simple HTML page structure:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
    <head>
        <title>Context Info</title>
        <style type="text/css">
            body {
                background-color: #FFFFFF;
            }

        </style>

    </head>
    <body onload="OnLoad()">

    </body>
</html>

As you can see, in the onload of the body we’re calling a function called OnLoad(). Now let’s look at this function, and see what it does.

Right after we’re closing the style tag, add a new script tag. This will hold our function.

</style>

<script type="text/javascript">

</script>

Now let’s dig into our function:

function OnLoad() {

}

First off, let’s get the service URL built:

var _serverURL = window.parent.Xrm.Page.context.getServerUrl() +
    "/xrmservices/2011/organizationdata.svc";

Next, let’s build the XMLHttpRequest:

var AddressReq = new XMLHttpRequest();
                AddressReq.open("GET", _serverURL + "/IncidentSet?$filter=CustomerId/Id eq (guid’" + window.parent.Xrm.Page.data.entity.getId() + "’)", false);
                AddressReq.setRequestHeader("Accept", "application/json");
                AddressReq.setRequestHeader("Content-Type", "application/json; charset=utf-8");
                AddressReq.send();

Pay attention to the fact that we’re looking for IncidentSet to retrieve Cases, and we’re filtering by CustomerId/Id to be the same as the current Account record.

Once we get the response, let’s process the response. We’re only going to output a very simple response in out HTML web resource. Later we can start looking at using this data for something more advanced.

if (AddressReq.readyState == 4 /* complete */) {
    if (AddressReq.status == 200) {
        var retrieved = this.JSON.parse(AddressReq.responseText).d;
        var Results = retrieved.results;
        document.write("There is a total of " + Results.length + " Cases associated:" + "<br />");
        for (var i = 0; i < Results.length; i++) {
            document.write(Results[i].TicketNumber + " : " + Results[i].Title + " : " +
                Results[i].IncidentId + " : " + Results[i].Description + " : " +
                Results[i].StateCode.Value + " : " + Results[i].StatusCode.Value + "<br />");
        }
    }
}

Now put it all together, and your final result on the page should be similar to this:

image

So now I see my two related Cases listed on the Account form, along with some details.

Next time we’ll look at using this data in a more meaningful manner.

Enjoy!

Advertisements
MVP Reconnect

Check out my course [Video]

Dynamics 365 Customer Engagement Administration

Dynamics 365 Customer Engagement Administration

Check out my course [Video]

Configuring and Extending Dynamics 365 Customer Engagement

Configuring and Extending Dynamics 365 Customer Engagement

Check out my course [Video]

Getting Started with Dynamics 365 Customer Engagement

Reviewed Book

Implementing Microsoft Dynamics 365 for Finance and Operations

Implementing Microsoft Dynamics 365 for Finance and Operations

Reviewed Book

Microsoft Dynamics 365 Extensions Cookbook

Microsoft Dynamics 365 Extensions Cookbook

Check out my Book

Microsoft Dynamics CRM 2016 Customization - Second Edition

Microsoft Dynamics CRM 2016 Customization - Second Edition

Check out my Book

Microsoft Dynamics CRM Customization Essentials

Microsoft Dynamics CRM Customization Essentials

Check out my Book

Microsoft Dynamics CRM 2011 Scripting Cookbook

Microsoft Dynamics CRM 2011 Scripting Cookbook

Reviewed Book

Microsoft Dynamics CRM 2011: Dashboards Cookbook

Microsoft Dynamics CRM 2011: Dashboards Cookbook

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Join 608 other followers

Follow Dynamics 365 Wizardry on WordPress.com