Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics 365 | Integration, Dataverse...
Answered

Showing an alert and closing the window

(0) ShareShare
ReportReport
Posted on by 1,019

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.

  • Verified answer
    RaviKashyap Profile Picture
    RaviKashyap 55,410 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
    yify 1,019 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
    RaviKashyap 55,410 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
    yify 1,019 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
    LeoAlt 16,331 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
    yify 1,019 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

Congratulations 2024 Spotlight Honorees

Kudos to all of our 2024 community stars! 🎉

Meet the Top 10 leaders for December

Congratulations to our December super stars! 🥳

Start Your Super User Journey

Join the ranks of our community heros! 🦹

Leaderboard

#1
André Arnaud de Calavon Profile Picture

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

#2
Martin Dráb Profile Picture

Martin Dráb 230,458 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Product updates

Dynamics 365 release plans