web
You’re offline. This is a read only version of the page.
close
Skip to main content
Community site session details

Community site session details

Session Id :
Microsoft Dynamics 365 | Integration, Dataverse...
Answered

Showing an alert and closing the window

(0) ShareShare
ReportReport
Posted on by 1,023

Hi,

I have a requirement in UCI when user goes to Activities and clicks on the Email button  then we will need to show an alert and restrict the email page from opening.

I am thinking to paste the JS on email form load as   the user clicks on email button which opens the email form. I am new to coding, can you please help with the code for the above scenario.

Appreciate the help!

Thanks.

I have the same question (0)
  • Verified answer
    RaviKashyap Profile Picture
    55,410 Moderator on at
    RE: Showing an alert and closing the window

    The unsaved popup occurs if the form has any field populated. I guess out of box crm populates the From/ To fields...The only way around this is to clear these fields(or any other fields which is auto populated) within your on load script. something like below-

    formContext.getAttribute(<your_field>).setValue(null);

    With respect to user seeing the form, this can't be changed as the script you have will be called only when the form is loaded so user's will still see it.

    Hope this helps.

  • yify Profile Picture
    1,023 on at
    RE: Showing an alert and closing the window

    Thanks Ravi, that worked but after the alert I get the Unsaved changes pop up  where when I click on '×' or  users will still see the email form. Is there any way to stop the Unsaved changes pop up on the email form which opens when I click the email button from Activities?

    All I want is to show an alert  the user and dont allow the users to save the email record.

  • Verified answer
    RaviKashyap Profile Picture
    55,410 Moderator on at
    RE: Showing an alert and closing the window

    Hi James,

    Assuming that you have registered the method son onload event, you need to pass the context as well. Refer below blog-

    carldesouza.com/.../

    Also, please do not that the current code will restrict create mode from all places i.e. the new email record will not be created.

    Hope this helps.

  • yify Profile Picture
    1,023 on at
    RE: Showing an alert and closing the window

    Hi Leo, thanks so much for the code. when I used the same I am getting the error that says getFormContext of undefined in both the standard and unified interface. Please help

  • Verified answer
    LeoAlt Profile Picture
    16,331 Moderator on at
    RE: Showing an alert and closing the window

    Hi partner,

    According to your requirement, you could try the following code. When the new create form of email opens, there will be an alert and then close the email form.

    function alertAndClose(executionContext){
        var formContext=executionContext.getFormContext();
        var formType = formContext.ui.getFormType();
        if (formType == 1){//create mode
            var alertStrings = { confirmButtonLabel: "Yes", text: "This is an alert.", title: "Warning" };
            var alertOptions = { height: 120, width: 260 };
            Xrm.Navigation.openAlertDialog(alertStrings, alertOptions).then(
                function success(result) {
                    formContext.ui.close();//close the email form
                },
                function (error) {
                    console.log(error.message);
                }
            );
            
        } 
    }

    pastedimage1583113170930v1.png

    After click yes, the form will close. In this method, we used 2 functions provided by D365.

    Close form.

    https://docs.microsoft.com/en-us/powerapps/developer/model-driven-apps/clientapi/reference/formcontext-ui/close

    Show Alert.

    https://docs.microsoft.com/en-us/powerapps/developer/model-driven-apps/clientapi/reference/xrm-navigation/openalertdialog

    Hope it helps.

    Best Regards,

    Leo

  • yify Profile Picture
    1,023 on at
    Unified client interface - Closing the window

    Hi,

    I have a requirement in UCI when user goes to Activities and clicks on the Email button  then we will need to show an alert and restrict the email page from opening.

    I am thinking to paste the JS on email form load as   the user clicks on email button which opens the email form. I am new to coding, can you please help with the code for the above scenario.

    Appreciate the help!

    Thanks.

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…

Mansi Soni – Community Spotlight

We are honored to recognize Mansi Soni as our August 2025 Community…

Congratulations to the July Top 10 Community Leaders!

These are the community rock stars!

Leaderboard > Microsoft Dynamics 365 | Integration, Dataverse, and general topics

#1
DAnny3211 Profile Picture

DAnny3211 433

#2
Abhilash Warrier Profile Picture

Abhilash Warrier 137 Super User 2025 Season 2

#3
Martin Dráb Profile Picture

Martin Dráb 67 Most Valuable Professional

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans