How to suppress "Do you want to close this window? "

Dec 4, 2012 at 6:04 AM

Hi All,

I have a hosted application "Account", for opening CRM Account record in Agent Desktop. Whenever I click on "Save & Close" button from CRM ribbon, it gives pop up as below:

---------------------------Windows Internet Explorer---------------------------

The webpage you are viewing is trying to close the window.

Do you want to close this window?

Yes   No 

-------------------------------------------------------------------------------------

This is happening for every button which closes window. (using  window.close() / Xrm.Page.ui.close() etc.)

If I access the same record from browser(not using Agent Desktop), clicking on "Save & Close", simply saves and closes the record, without prompting.

How can we suppress/bypass this message, so user won't get prompt for it?

Environment - CCA R2, MS CRM 2011 On-premise, IE8/IE9.

Feb 12, 2013 at 6:44 PM
The reason why you get the prompt when just trying to close the window is because the CRM Form has the isDirty flag = true. That means something within the form data has changed when it was opened (either manually or through code). The reason why you don't get the "Do you want to close this window" message is because the Save & Close button saves the data (which resets the isDirty flag) and then the page can close.

Do window.close, Xrm.Page.ui.close() or anything else will trigger the message.

What you can do in CRM/CCA is add a function called CCACRMClose() in a JScript library in CRM. Add the library to the form but don't wire up any events. Then using a WebApplicationAdapter you can use the DOM Model to execute the custom CCACRMClose() function. The CCACRMClose() function just needs a single line Xrm.Page.data.entity.save("saveandclose"); You could also use a workflow automation that clicks the Save & Close button for a record in order to close it out.

Now you just need to wire a FireAction event to your control when the Tab is Closed or through some other event in CCA.

Jeremy
Feb 13, 2013 at 1:41 PM
Edited Feb 13, 2013 at 1:43 PM
Hi Jeremy,
Thanks for your reply.

I checked for isDirty flag on my page. My page is not getting dirty. If I change any value of fields on form (do form dirty intentionally) and try to close that form using Xrm.Page.ui.close() it asks the following:

There are unsaved changes in this window.

What do you want to do?

0 Save my chnages.
0 Discard my changes.

To keep working on this page, click Cancel.

OK Cancel

If I click OK selecting "Discard my changes", while using CCA Agent Desktop, it again pops up the same message "The webpage you are viewing is trying to close the window." But if I do the same from browser it gives only first prompt for saving/discarding changes, and closes the page, without this prompt.

Please provide some help on getting DOM in WebApplicationAdapter.

I tried with an adapter using WebApplicationAdapter and IDesktopUserActionsConsumer but getting following error on "Browser".

Cannot access a disposed object.
Object name: 'WebBrowserExtended'.

Please help.
Thanks.

Regards,
Devendra V.
Feb 13, 2013 at 3:23 PM
Devendra,

Xrm.Page.ui.close() triggers a slightly different method than hitting the X which triggers a window.close().

Are you sure that there isn't any OnLoad() javascript setting any values when it opens?

I included 2 functions from my WebApplicationAdapter, both are triggered by FireAction events from other controls in CCA. The 1st function traverses the DOM to find the "Notes" section in CRM and adds a new note for the text passed to the function. The 2nd function traverses the dom and then executes a JScript function that is part of my form in CRM.
private void UpdateCallNotes(string data)
        {
            try
            {
                //Get Reference To Notes IFRAME
                mshtml.HTMLDocument _doc = (mshtml.HTMLDocument)Browser.WebBrowser.Document;
                IHTMLWindow2 _contentIFRAME = (IHTMLWindow2)_doc.frames.item(0);
                mshtml.HTMLDocument _contentIFRAMEDoc = (mshtml.HTMLDocument)_contentIFRAME.document;
                IHTMLWindow2 _notesIFRAME = (IHTMLWindow2)_contentIFRAMEDoc.frames.item(0);
                mshtml.HTMLDocument _notesIFRAMEDoc = (mshtml.HTMLDocument)_notesIFRAME.document;

                //Get Reference To Anchor Element
                mshtml.HTMLAnchorElement _addNote = (mshtml.HTMLAnchorElement)_notesIFRAMEDoc.getElementById("newNoteButton");
                _addNote.click();

                //Now Get Reference To Note Data Text Area
                //mshtml.HTMLTextAreaElement _txtArea = (mshtml.HTMLTextAreaElement)_notesIFRAMEDoc.getElementsByTagName
                mshtml.IHTMLElementCollection _txtAreaCollection = (mshtml.IHTMLElementCollection)_notesIFRAMEDoc.getElementsByTagName("textarea");
                if (_txtAreaCollection.length > 0)
                {
                    mshtml.IHTMLTextAreaElement _txtArea = (mshtml.IHTMLTextAreaElement)_txtAreaCollection.item(0,0);
                    _txtArea.value = data;
                    _txtArea = null;
                }

                //Dispose of Elements
                _addNote = null;
                _notesIFRAMEDoc = null;
                _notesIFRAME = null;
                _contentIFRAMEDoc = null;
                _contentIFRAME = null;
                _doc = null;
            }
            catch
            { }
            finally { }
        }


private void SetRegarding(string data)
        {
            mshtml.HTMLDocument _doc = null;
            IHTMLWindow2 _win2 = null;
            mshtml.HTMLDocument _contentIFRAME = null;
            object _result = null;
            try
            {
                _doc = (mshtml.HTMLDocument)Browser.WebBrowser.Document;
                if (_doc != null)
                {
                    _win2 = (IHTMLWindow2)_doc.frames.item(0);
                    if (_win2 != null)
                    {
                        //_contentIFRAME = (mshtml.HTMLDocument)_win2.document;
                        _result = _win2.execScript("CCA_SetRegarding('" + data + "','incident','Current Request' );", "JScript");
                    }
                }
            }
            catch
            {
                //Do Nothing
            }
            finally
            {
                //Dispose Objects
                _result = null;
                 _contentIFRAME = null;
                 _win2 = null;
                _doc = null;
            }
        }
Hopefully this helps. You could also "detach" or override the close event from window so it doesn't trigger the typical IE validation which is where the "Are you sure you want to leave this page" comes from. However, that would be unsupported code, so I would go with the Save&Close method first.

Jeremy
Feb 18, 2013 at 1:08 PM
Edited Feb 18, 2013 at 1:14 PM
Hi Jeremy,

There is no OnLoad script on my form.
I am getting that pop up on Save & Close button click as well.
Trying to use the adapter method suggested by you but not able to traverse DOM.
I tried to follow similar approach which suppresses the Pop up blocker message mentioned

here

but could not succeed.

Can you please tell how to get the Save & Close button in DOM?

Regards,
Devendra V.
Mar 21, 2013 at 5:34 PM
Guyz, Any workaround for this please ?

I'm also getting the same behavior.. When user clicks on "Save & Close" , why it is asking again like "Do you want to close this window ?",

And this is happening only if i'm opening any crm page in cca.

Matt, Can you please give us some suggestion..


Regards,
Srikanth