locked
Calling a Javascript method from HTML web resource is not working RRS feed

  • Question

  • Hi Techies,

    I have a html web resource , I am trying to call javascript function from that, But some how it is not recognizing the JS function.

    I tried using

    window.parent.XrmServiceToolkit.Soap.Fetch(fetchXml);

    and

    parent.XrmServiceToolkit.Soap.Fetch(fetchXml);

    and window.parent.Xrm.page.XrmServiceToolkit.Soap.Fetch(fetchXml);

    but nothing is working...

    Can some body please suggest on this.

    Thanks in Advance,

    Sreeni 


    Sreeni Pavalla

    Friday, January 17, 2014 6:54 AM

Answers

  • Hi,

    Where do you put your HTML web resources? in the form body or in form navigation? Since you use window.parent.Xrm, I guess your HTML is placed in form navigation.

    XrmServiceToolkit is an helper class your write in JS file? If so you should include that script in HTML. It should work.

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

    Hope it helps.

    Friday, January 17, 2014 7:16 AM
  • Hi Linh,

    Thanks alot for your patience and help man.

    I have changed the reference path like below 

    from

    <SCRIPT type=text/javascript src="/WebResources/csx_XrmServiceUtil.js"></SCRIPT>

    to

    <SCRIPT type=text/javascript src="/csx_XrmServiceUtil.js"></SCRIPT>

    and accessing it

    from

    var retrievedContacts = XrmServiceToolkit.Soap.Fetch(fetchXml);

    to

    var retrievedContacts = parent.XrmServiceToolkit.Soap.Fetch(fetchXml);
    			


    then it worked as needed.

    Thank you,

    Sreeni

     

    Sreeni Pavalla

    Friday, January 17, 2014 2:09 PM

All replies

  • Hi,

    Where do you put your HTML web resources? in the form body or in form navigation? Since you use window.parent.Xrm, I guess your HTML is placed in form navigation.

    XrmServiceToolkit is an helper class your write in JS file? If so you should include that script in HTML. It should work.

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

    Hope it helps.

    Friday, January 17, 2014 7:16 AM
  • hi , 

    i have done that .

    <HTML xmlns="http://www.w3.org/1999/xhtml"><HEAD><TITLE>My HTML Web Resource</TITLE>
    <SCRIPT type=text/javascript src="ClientGlobalContext.js.aspx"></SCRIPT>
    
    <SCRIPT type=text/javascript src="/WebResources/csx_XrmServiceUtil.js"></SCRIPT>
    <SCRIPT type=text/javascript src="sp_jquery_1.9.1.js"></SCRIPT>
    <SCRIPT type=text/javascript src="sp_JSON.js"></SCRIPT>
    
    <SCRIPT type=text/javascript>
     
        function GetSearchData() {
    	   
           var fetchXml,
            layoutXml,
            contactRoleFilter,
            railLocationFilter,
            carrierFilter,
            jobRoleFilter,
            departmentField = window.parent.CEI.Field("csx_department"),
            roleField = window.parent.CEI.LookupField("csx_jobrole"),
            initialsField = window.parent.CEI.Field("csx_carrierinitials"),
            milepostField = window.parent.CEI.Field("csx_milepost"),
    		yardnameField = window.parent.CEI.Field("csx_yardname"),
    		contactnameField = window.parent.CEI.Field("csx_contactname"),
            divisionField = window.parent.CEI.Field("csx_division"),
            cityField = window.parent.CEI.Field("csx_address1_city"),
            stateField = window.parent.CEI.Field("csx_address1_state"),
            tpjunctionField = window.parent.CEI.Field("csx_tpjunction");
    
        // Build the filters
        contactRoleFilter =
            "<filter type='and'>" +
                ((roleField.GetID() !== '') ? "<condition attribute='csx_jobroleid' operator='eq' value='" + roleField.GetID() + "'/>" : "") +
            "</filter>";
    
        railLocationFilter =
            "<filter type='and'>" +
                (stateField.IsNotNull() ? "<condition attribute='csx_address1_state' operator='eq' value='" + stateField.GetValue() + "'/>" : "") +
                (divisionField.IsNotNull() ? "<condition attribute='csx_division' operator='like' value='" + divisionField.GetValue() + "%'/>" : "") +
                (cityField.IsNotNull() ? "<condition attribute='csx_address1_city' operator='like' value='" + cityField.GetValue() + "%'/>" : "") +
                (tpjunctionField.IsNotNull() ? "<condition attribute='csx_tpjunction' operator='like' value='" + tpjunctionField.GetValue() + "%'/>" : "") +
                (milepostField.IsNotNull() ? "<condition attribute='csx_milepost' operator='like' value='" + milepostField.GetValue() + "%'/>" : "") +
    			(yardnameField.IsNotNull() ? "<condition attribute='csx_milepostname' operator='like' value='%25" + yardnameField.GetValue() + "%'/>" : "") +
            "</filter>";
    
        carrierFilter =
            "<filter type='and'>" +
                (initialsField.IsNotNull() ? "<condition attribute='csx_carrierinitials' operator='like' value='" + initialsField.GetValue() + "'/>" : "") +
            "</filter>";
    
        jobRoleFilter =
            "<filter type='and'>" +
                (departmentField.IsNotNull() ? "<condition attribute='csx_department' operator='eq' value='" + departmentField.GetValue() + "'/>" : "") +
            "</filter>";
    		
    	contactFilter =
            "<filter type='and'>" +
    			"<condition attribute='csx_contacttype' operator='eq' value='2'/>" +
                (contactnameField.IsNotNull() ? "<condition attribute='csx_firstname_lastname' operator='like' value='%25" + contactnameField.GetValue() + "%'/>" : "") +
            "</filter>";
    
        // Build the fetch XML
        fetchXml =
            "<fetch version='1.0'  distinct='true'>" +
                "<entity name='contact'>" +
                    "<attribute name='contactid' />" +
                    "<attribute name='address1_city'/>" +
                    "<attribute name='address1_stateorprovince'/>" +
                    "<attribute name='csx_facilities_management_notes'/>" +
                    "<attribute name='csx_pagernumber'/>" +
                    "<attribute name='csx_rnx'/>" +
                    "<attribute name='emailaddress1'/>" +
                    "<attribute name='fullname'/>" +
                    "<attribute name='mobilephone'/>" +
                    "<attribute name='telephone1'/>" +
                    "<attribute name='ownerid'/>" +
                    "<link-entity name='csx_contactrole' from='csx_contactid' to='contactid' alias='contactrolelink' link-type='outer'>" +
                        contactRoleFilter +
                        "<link-entity name='csx_raillocation' from='csx_raillocationid' to='csx_rl_contactroleid'  alias='raillocationlink'>" +
                            "<attribute name='csx_name' />" +
    						"<attribute name='csx_milepostname' />" +
    						"<attribute name='csx_milepost' />" +
    						"<attribute name='csx_rnx' />" +
    						"<attribute name='csx_mainphonenumber' />" +
                            railLocationFilter +
                            "<link-entity name='csx_carrier' from='csx_carrierid' to='csx_carrierid' alias='carrierlink'>" +
                                carrierFilter +
                            "</link-entity>" +
                        "</link-entity>" +
                        "<link-entity name='csx_jobrole' from='csx_jobroleid' to='csx_jobroleid' alias='jobrolelink'>" +
                            "<attribute name='csx_name'/>" +
                            jobRoleFilter +
                        "</link-entity>" +
                    "</link-entity>" +
    				contactFilter + 
                "</entity>" +
            "</fetch>";
    	debugger;	
    		var retrievedContacts = XrmServiceToolkit.Soap.Fetch(fetchXml);
    			 alert(retrievedContacts.length);
        }
     function ClearFields() {
     
            window.parent.Xrm.Page.ui.controls.forEach(function(control, index) {
    		control.getAttribute().setValue(null);
    		
    	});
        }
    </SCRIPT>
    
    <STYLE type=text/css>
            .csxtable
            {
                width: 30%;
    			height : 30px;
            }
            
    		.csxtd
            {
                width: 150px;
               	align : centre;
           
            }
            .csxbutton
            {
                width : 80px;
    			background-color : #f6f8fa;
    			height : 25px;
    			textAlign : center;
    			overflow : hidden;
    			fontFamily : Tahoma;
    			border-width:1px;
    			border-style:solid;
    			
            }
        </STYLE>
    
    <META charset=utf-8></HEAD>
    <BODY>
    <TABLE width="100%">
    <TBODY>
    <TR>
    <TD>
    <TABLE width="40%">
    <TBODY></TBODY></TABLE></TD></TR></TBODY></TABLE>
    <TABLE class=csxtable>
    <TBODY>
    <TR>
    <TD class=csxtd><INPUT class=csxbutton onclick=GetSearchData() value=Search type=button> </TD>
    <TD class=csxtd><INPUT class=csxbutton onclick=ClearFields() value=Clear type=button> </TD></TR></TBODY></TABLE><TD></TD><TD>
    <TABLE width="40%">
    <TBODY></TBODY></TABLE></TD></TR></TABLE></BODY></HTML>


    Sreeni Pavalla

    Friday, January 17, 2014 7:54 AM
  • Hi,

    I see your code and your object is null so I guess there're some possibilities:

    1. The context between your html file and your js file. Do they in the same folder level? try to check if they aren't: ex: WebResource/Page/YourPage.html and WebResources/csx_XrmServiceUtil.js.

    2. When debugging, can you get jquery object?

    3. Try to move your code. 

    +Put src <script> in your <head> element.

    +Other definition <script> is in <body> element.

    Hope it helps.


    • Edited by Linh Giang Friday, January 17, 2014 8:07 AM
    Friday, January 17, 2014 8:06 AM
  • Hi Linh,

    Thanks for your reply. Yes they are under same folder.

    Do we need to refer Jquery and Json as well?

    Thank you,

    Sreeni Pavalla


    Sreeni Pavalla

    Friday, January 17, 2014 8:54 AM
  • Hi Sreeni,

    We don't need to. As the main problem here is only that you can't load the reference script so you should try to modify your HTML page.

    You can use jQuery object as a test to check if the reference script file can be loaded successfully.

    It can be done with a little tweak, you can search some simple html page and use it content as template.

    Hope it helps.


    • Edited by Linh Giang Friday, January 17, 2014 9:03 AM
    Friday, January 17, 2014 9:02 AM
  • Hi Linh,

    I am not sure how we can test a JQuery object in this context? i have verified again the two files folder structre(JS and HTML) , they both are same.

    Is there any other way that we can debug and find out the solution?

    Thank you,

    Sreeni


    Sreeni Pavalla

    Friday, January 17, 2014 10:01 AM
  • Hi Sreeni,

    Sorry for not being clear. You can check if your script file is loaded or not by modify your HTML page like writing some code:

    $(document).ready(function()) { // check jQuery object alert(1); alert(typeof(XrmServiceToolkit)); // check your object }

    ...

    Hopefully you can make it work soon.

    Good luck.

    Friday, January 17, 2014 11:03 AM
  • Hi Linh,

    Thanks alot for your patience and help man.

    I have changed the reference path like below 

    from

    <SCRIPT type=text/javascript src="/WebResources/csx_XrmServiceUtil.js"></SCRIPT>

    to

    <SCRIPT type=text/javascript src="/csx_XrmServiceUtil.js"></SCRIPT>

    and accessing it

    from

    var retrievedContacts = XrmServiceToolkit.Soap.Fetch(fetchXml);

    to

    var retrievedContacts = parent.XrmServiceToolkit.Soap.Fetch(fetchXml);
    			


    then it worked as needed.

    Thank you,

    Sreeni

     

    Sreeni Pavalla

    Friday, January 17, 2014 2:09 PM
  • Hi,

    I've had the same problem, and the solution is the follow:

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

    It's incredible!!!. Don't have to indicate the file extension.

    Good luck !

    Wednesday, July 22, 2015 11:36 AM
  • Hi,

    I've had the same problem, and the solution is the follow:

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

    It's incredible!!!. Don't have to indicate the file extension.

    Good luck !

    that's because you created the webresource without the extension in the name

    My blog: www.crmanswers.net - CRM Theme Generator

    Wednesday, July 22, 2015 11:40 AM