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)

Trouble getting JS web resource and HTML web resource to work together

(0) ShareShare
ReportReport
Posted on by

Hi,

I'm trying to accomplish a relatively simple task as a test and a learning exercise. I have a jscript web resource and an html web resource. I have a function in the jscript web resource that runs onload:

function getEventCatId() {
var eventCatId, eventCatName, lookupFieldObject;

lookupFieldObject = Xrm.Page.data.entity.attributes.get("new_eventcategoryid");

if (lookupFieldObject.getValue() != null) {
eventCatId = lookupFieldObject.getValue()[0].id;
eventCatName = lookupFieldObject.getValue()[0].name;
}
alert(eventCatId);

};

I've included the alert at the end to ensure that the data I desire is actually found, and when I try it out, it is successful. 

My html web resource just includes a button that is supposed to show the same alert as above onClick. In my html page I included my js web resource as a "src" and using the console, it appears there is no issue with the html finding the js web resource. I've tried two methods to get my desired alert -- 1.) <td><button onclick="alert(eventCatId)">Click here</button></td></tr>, and 2.) <td><button onclick="getEventCatId()">Click here</button></td></tr>.

When using method number 1, I get the following error in the console: "Uncaught ReferenceError: eventCatId is not defined," and when I use method number 2, I get the following error in the console: "Uncaught TypeError: Cannot read property 'entity' of null" and it points me to this line: "lookupFieldObject = Xrm.Page.data.entity.attributes.get("new_eventcategoryid");"

I know that the js web resource is able to successfully run the code because the alert shows on load of the page. So, I'm confused why the html web resource fails. Any ideas on how to fix this? Thanks for any help!

*This post is locked for comments

I have the same question (0)
  • Suggested answer
    Joe Gill Profile Picture
    on at

    Hi,

    It looks like the javscript file path in your html resource is incorrect. Its a common problem

    Here is an old psot but it is still relevant

    blogs.msdn.microsoft.com/.../crm-2011-web-resources-simulating-directories-and-the-importance-of-relative-paths

    Joe

  • fishyjj Profile Picture
    on at

    Thanks for your reply. Based on that article, I believe I am correctly referencing my js file. The line in my code is as follows: <script src="new_Jscript_Test_One" type="text/javascript"></script>

    The logical name of my js web resource is new_Jscript_Test_One. My html web resource's name is new_ForTesting. Does it look like I'm doing something wrong here?

    Thanks again for any help.

  • Verified answer
    tpeschat Profile Picture
    4,930 on at

    Have you tried adding

    var Xrm = parent.Xrm;

    To the Web resource?

    Hth Thomas

  • fishyjj Profile Picture
    on at

    That worked! Thanks very much for the help. Why was that addition necessary?

  • tpeschat Profile Picture
    4,930 on at

    Well I can't explain why it is necessary, I just figured out (using the F12 developer tools) I need to at this code to web resources to work, while for js code on load it isn't necessary.

    Glad it works now

    Thomas

  • fishyjj Profile Picture
    on at

    Hi --  so I found that if I take my alert out of the js web resource, and add to the HTML web resource, I do not successfully get the data. The guid I'm looking for is "undefined" -- how do I bridge the gap between using the JS web resource to get the data, and being able to use it in the HTML web resource?

    My code in my html is as follows:

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

    <script src="../WebResources/new_jquery_1.9.1.min" type="text/javascript"></script>

    <script src="new_Jscirpt_Test_One" type="text/javascript"></script>

    <script type="text/javascript">

    function getEventQuestions () {

    var eventCatId = getEventCatId();

    retrieveMultiple("new_registrationquestionsSet", "?$select=new_name&$filter=new_regquestionsid eq (guid'" + eventCatId + "')");

    }

    function retrieveMultiple(odataSetName, filter) {

    var serverUrl = Xrm.Page.context.getClientUrl();

    var ODATA_ENDPOINT = "/XRMServices/2011/OrganizationData.svc";

    var odataUri = serverUrl + ODATA_ENDPOINT + "/" + odataSetName;

    if (filter) {

    odataUri += filter;

    }

    testSuccess(odataUri);

    }

    function testSuccess (odataUri) {

    alert(odataUri);

    }

    </script>

    When the alert shows with the odataUri, the 'guid' portion of the query string returns undefined. My js script is as follows:

    function getEventCatId() {

    var eventCatId, lookupFieldObject;

    lookupFieldObject = parent.Xrm.Page.data.entity.attributes.get("new_eventcategoryid");

    if (lookupFieldObject.getValue() != null) {

    eventCatId = lookupFieldObject.getValue()[0].id;

    }

    };

    Thanks for any help.

  • Tim Partridge Profile Picture
    25 on at

    var Xrm = parent.Xrm is necessary because if you inspect the DOM (press F12) you will see two #document elements. One is for the main page and another is for the web resource. That is because you can have an HTML document embedded within another HTML document (no need for iframes). The Xrm object is scoped to the parent #document, not the child #document. So that line defines the Xrm object within the child #document to reference the parent scope.

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