Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

Running Javascript from an HTML WEb Resource

Posted on by Microsoft Employee

Hello,

I added a HTML webresource in a CRM Form. When I try to run the javascript from a simple button it does not run anything, not even an simple alert. 

What should I add to the code to be able to run the javascript from inside an html web resource?

Thanks for your help!

Angel

<!DOCTYPE html>
<html>
<body>

<img id="savings" src="test.png" style="padding-right:10px" >
<img id="creditcard" src="test1.png" style="padding-right:10px">
<img id="car" src="test2.png" style="padding-right:10px">
<img id="house" src="test3.png" style="padding-right:10px">


<button onclick="ProductImagesOnload()">Try it</button>

<script type="text/javascript">
function ProductImagesOnload() {

var grid = Xrm.Page.ui.controls.get('ProductsImages')._control;

alert(grid);

if (grid.get_innerControl() == null) {
setTimeout(subGridOnload, 1000);
return;
}
else if (grid.get_innerControl()._element.innerText.search("Loading") != -1) {
setTimeout(subGridOnload, 1000);
return;
}

var ids = grid.get_innerControl().get_allRecordIds();


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

var x = grid.get_innerControl().getCellValue("rst_productclassid", ids[i]);

var flag = 0 ;
var flag1 = 0 ;
var flag2 = 0 ;
var flag3 = 0 ;

if (flag = 0){
if(x == "Deposit"){

document.getElementById("savings").src = "test4.png";
flag = 1;
}
}
if (flag1 = 0){
if(x == "Credit"){

document.getElementById("creditcard").src = "test5.png";
flag1 = 1;
}
}


}

}
</script>

</body>
</html>

*This post is locked for comments

  • Suggested answer
    RE: Running Javascript from an HTML WEb Resource

    Hi Angel,

    There are few things I would suggest:

    1)Use parent.Xrm instead

    While accessing the controls or attributes present on the entity form from a Html webresource its  needed to use parent.

    For example:

    parent.Xrm.Page.getControl('name')

    - parent.Xrm.Page.ui.tabs.get('general')

    2) Try a bit modular structure

    Having a separate javascript web resource in your crm for handling the functionality of your onclick function ProductImagesOnload() would rather help in debugging.

    Below is a sample I tried my self and in my case, I did get the alert message

    HTML-

    <!DOCTYPE html>
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <title></title>
    </head>
    <body>
    <div>
    <button onclick="ProductImagesOnload()">Try it</button>
    </div>
    <script src="scripts/load.js"></script>
    </body>
    </html>

    Script-

    //load.js

    function ProductImagesOnload()
    {
    alert('Hello World!!!');
    }

     

    Hope this heps!!!

    ~TCD

  • ashlega Profile Picture
    ashlega 34,475 on at
    RE: Running Javascript from an HTML WEb Resource

    Yeah, that's the "unsupported" part:))

    Try to add some wait time.. assuming all the controls/methods are still available in that version of CRM, it's possible that your web resource is loaded before the grid.

    Have a look here for some ideas:

    community.dynamics.com/.../178907

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Running Javascript from an HTML WEb Resource

    It works! The only problem that I'm having right now is getting the lookup values from a column subgrid that is returning "null".

    Do you know how to get the values from a subgrid?

  • Verified answer
    ashlega Profile Picture
    ashlega 34,475 on at
    RE: Running Javascript from an HTML WEb Resource

    You just need to prefix "Xrm" - you don't need to prefix grid.

    This should work, for example:

    var grid = window.parent.Xrm.Page.ui.controls.get('ProductsImages')._control;

    Or you can define Xrm in the first line:

    var Xrm = window.parent.Xrm;

    and, then, leave the rest of your code as is

    Whether the rest of that code is going to work I'm not sure, since some of those things look "unsupported" (in the sense that they are not part of officially supported Xrm.. like directly accessing _control property etc)

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Running Javascript from an HTML WEb Resource

    The alert worked. Now how would I add the "window.parent" to grid.get_innerControl().get_allRecordIds();  ?

    since "grid" is a variable

  • Verified answer
    ashlega Profile Picture
    ashlega 34,475 on at
    RE: Running Javascript from an HTML WEb Resource

    Hi,

     there can be something else in that code, but you can't access Xrm from the webv resource directly - try to prefix any "Xrm" with window.parent.

     For instance, replace

         Xrm.Page.ui.controls.get('ProductsImages')

     with

         window.parent.Xrm.Page.ui.controls.get('ProductsImages')

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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,280 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,235 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans