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

Notifications

Announcements

No record found.

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
    4 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,373 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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

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

#1
Martin Dráb Profile Picture

Martin Dráb 45 Most Valuable Professional

#2
iampranjal Profile Picture

iampranjal 36

#3
Satyam Prakash Profile Picture

Satyam Prakash 31

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans