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

Announcements

No record found.

News and Announcements icon
Community site session details

Community site session details

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

Xrm.Navigation.openAlertDialog keeps popping up infinitely

(6) ShareShare
ReportReport
Posted on by 20
I have 2 functions -
 
1. checkTemplate
2. onSaveBlockSaveIfNotOfficial
 
Using a Global flag variable 
 
The checkTemplate is being called on the OnChange event of the Template in Dynamics 365 where it retrieves the Template ID, Template Name based on the Template selected.
 
The onSaveBlockSaveIfNotOfficial is the function called on the OnSave event of the form and if the Template name has the word "Official" in it then it returns the result, and the user is able to save their email.
If the Template does not have the word 'Official' then it shows an alert which is Xrm.Navigation.openAlertDialog. and it keeps popping up in a loop.
 
Below is my code
 
//global flag variable
var templateHasOfficial = false;
 
//call on change of Template
function checkTemplate(executionContext) {
    var formContext = executionContext.getFormContext();
   
    var templateId = formContext.getAttribute("msdynmkt_templateid").getValue()[0].id;
    var templateIdNew = templateId.replace("{", "").replace("}", "");
    //Check if the ID or value exists
    if (!templateIdNew) return;

try {
    const result = Xrm.WebApi.retrieveRecord("msdynmkt_emailtemplate", templateIdNew, "?$select=msdynmkt_name");
    templateHasOfficial = result.msdynmkt_name && result.msdynmkt_name.toUpperCase().includes("Official");
} catch (error) {
    console.error("Error retrieving template:", error.message);
    templateHasOfficial = false;
};

}

//call OnSave of the form
function onSaveBlockSaveIfNotOfficial(executionContext) {
    var eventArgs = executionContext.getEventArgs();
    if (!templateHasOfficial) {
        eventArgs.preventDefault();

      
       Xrm.Navigation.openAlertDialog({
            text: "Please select a valid Official Email Template.",
            title: "Invalid Template",
            confirmButtonLabel: "OK"
        });
    }
    
}
 
Categories:
I have the same question (0)
  • Dadasaheb Deole Profile Picture
    5 on at
    Xrm.Nevigation,openAlertDialog keeps popping up infinitely
     

    const result = Xrm.WebApi.retrieveRecord(...);

     

    returns a Promise, but you're using it like a synchronous function. This means the code below it runs before the API call completes, so templateHasOfficial always stays false, even if the selected template contains the word "Official".

    As a result, the onSaveBlockSaveIfNotOfficial function always shows the alert and prevents saving — even if the user selects the correct template, which causes the alert to appear in a loop.

     Xrm.WebApi.retrieveRecord("msdynmkt_emailtemplate", templateId, "?$select=msdynmkt_name").then(
            function success(result) {
                var templateName = result.msdynmkt_name;
                templateHasOfficial = templateName && templateName.toUpperCase().includes("OFFICIAL");
            },
            function (error) {
                console.error("Error retrieving template:", error.message);
                templateHasOfficial = false;
            }
        );

     

     

    If this was helpful, please mark it as verified.

  • Verified answer
    Muhammad Shahzad Shafique Profile Picture
    2,375 Most Valuable Professional on at
    function checkTemplate(executionContext) {
        var formContext = executionContext.getFormContext();
    
        var templateLookup = formContext.getAttribute("msdynmkt_templateid").getValue();
        if (!templateLookup || templateLookup.length === 0) return;
    
        var templateId = templateLookup[0].id.replace("{", "").replace("}", "");
    
        Xrm.WebApi.retrieveRecord("msdynmkt_emailtemplate", templateId, "?$select=msdynmkt_name").then(
            function (result) {
                templateHasOfficial = result.msdynmkt_name && result.msdynmkt_name.toUpperCase().includes("OFFICIAL");
            },
            function (error) {
                console.error("Error retrieving template:", error.message);
                templateHasOfficial = false;
            }
        );
    }
    
    onSaveBlockSaveIfNotOfficial – Called on OnSave of the Form
     
    function onSaveBlockSaveIfNotOfficial(executionContext) {
        var eventArgs = executionContext.getEventArgs();
    
        if (!templateHasOfficial) {
            eventArgs.preventDefault();
    
            Xrm.Navigation.openAlertDialog({
                text: "Please select a valid Official Email Template.",
                title: "Invalid Template",
                confirmButtonLabel: "OK"
            });
        }
    }
    
     

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

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Stars!

Meet the Microsoft Dynamics 365 Contact Center Champions

We are thrilled to have these Champions in our Community!

Congratulations to the March Top 10 Community Leaders

These are the community rock stars!

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

#1
11manish Profile Picture

11manish 172

#2
ManoVerse Profile Picture

ManoVerse 58 Super User 2026 Season 1

#3
Niki Patel Profile Picture

Niki Patel 42

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans