Monday, 8 July 2013

Open an HTML web resource page in a modal dialog, return values from it and use them in the caller Entity form via Javascript / jQuery (CRM 2011)

Sometimes it could be useful to open an external page as a modal window from an entity form and return some values to the caller entity form.
In the following example we show a simple HTML page with an option set and a text box; via javascript, we manage the click on an 'Ok' button to return both the entered text and the chosen option value.
The HTML page could be opened from a ribbon button or in the way that better fit our needs. We show the code snippet to open the modal dialog, take and process the return.



HTML Code:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
       <head>
           <title>HTML Web Resource</title>
           <script type="text/javascript"
                      src="../Scripts/jquery_1.9.0.js"></script>
           <script type="text/javascript" language="javascript">

              $(document).ready(function () {
                  var selectedOption;

                  $('#selectmenu').change(function () {
                     // assign the value to variable
                     selectedOption = $('#selectmenu :selected').val();
                  });

                  $('#btnOk').click(function () {
                     var result = {};
                     result.EnteredText = $('#textbox').val();
                     result.SelectedOption = selectedOption;

                     window.returnValue = result;
                     window.close();
                });
            });
           </script>
       </head>
       <body>
        <div>
             <select id="selectmenu">
             <option value="" selected="selected">
                    Select an option
                </option>
                <option value="option1">Option One</option>
                <option value="option2">Option Two</option>
                <option value="option1N">Option N</option>
            </select>
            <br />
            <label>Enter a text: </label>
            <input id="textbox" type="text" />
            <br />
            <input id="btnOk" type="button" value="Ok"/>
        </div>
       </body>
</html>


Code snippet (open dialog):

var retObj = window.showModalDialog('HTML_WebResource_url', null, options);
//Check if the return object is null
if (retObj == null) {
       alert('Error in return');
       return;
}

//Check if returned property 'EnteredText' value is not null

if (retObj.EnteredText) {

    //Use in the form

    Xrm.Page.getAttribute('attribute1_name').setValue(retObj.EnteredText);

}

 
//Check if returned property 'SelectedOption' value is not null
if (retObj.SelectedOption) {
    //Use in the form
    Xrm.Page.getAttribute('attribute2_name').setValue(retObj.SelectedOption);
}
 

Hope it can be useful
Happy CRM coding


Rate this posting:
{[['']]}

4 comments:

  1. HI
    How can we run this HTML code dialog
    I just added the HTML webresource and I just copied the Webresource URL and pasted on the jscript function
    But nothing coming up, just added an alert and it is alerting , after that it is giving nothing
    Could you please tell me how it works

    Thanks In Advance

    ReplyDelete
    Replies
    1. Hi Siva.
      Depends on what you expect to return from the HTML page.
      In my case I return 2 values that become available to the caller.
      window.showModalDialog() is a synchronous function, means that the code breaks in its point and continue only when the dialog is closed; of course, if you close with the "x" you will have null as a returned value.
      Hope it helps.

      Delete
  2. Hi,

    I have a similar requirement where in I am opening a phone call page from custom ribbon button in a modal dialog. When I save/close the phone call I need to refresh the lead form. How can we achieve this?

    Thanks in advance!

    ReplyDelete
  3. This is really great work. Thank you for sharing such a useful information here in the blog.
    Graphing functions MathCad Homework Help

    ReplyDelete