HTML Web Resources in Dynamics CRM [2]

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!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Create a website or blog at WordPress.com

Up ↑

%d bloggers like this: