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 :
Customer experience | Sales, Customer Insights,...
Answered

How to add a dialog prompt using JS to redirect to Documents tab?

(0) ShareShare
ReportReport
Posted on by 285

Hi Everyone,

We are using D365 Sales Online and recently got a requirement to create dialogs/prompts for a custom entity.

But, sInce Dialogs are deprecated, I am trying to figure out a way to create something similar using JavaScript? 

Below are the Requirements: 

Whenever Order (custom entity) record is created user should receive a dialog/prompt to confirm the order before saving the record. 
 - Prompt should have two options "Ok" and "Cancel"
           - If the user hits "OK" the script should redirect the user to the related documents tab
           - If the user hits "Cancel" record should "not be saved"

I tried the below code for the prompt but even if I hit cancel, it saves the record and a new order is being created. Also don't know how to add redirection to the documents tab.

function ConfirmBox() {    
    var confirmationText;
    if (confirm("Would you like to proceed?")) {
        confirmationText = "You pressed OK!";
    } else {
        confirmationText = "You pressed Cancel!";
    }
    //Using the alert notification to show the option selected
    alert(confirmationText);

Thank you in advance for the help

I have the same question (0)
  • Verified answer
    Wahaj Rashid Profile Picture
    11,321 on at

    Hi,

    Thank you for your query.

    You  can open the confirmation dialogue using Xrm.Navigation.openConfirmDialog(confirmStrings,confirmOptions).then(successCallback,errorCallback),as explained below:

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

    Upon cancelation (not confirmed), stop the from saving (preventDefault).

    You can register following function onSave of the form (do pass execution context):

    function onSave(executionContext) {
    
        var confirmStrings = { text:"Are you sure to save the record?.", title:"Confirmation Dialog" };
        var confirmOptions = { height: 200, width: 450 };
        Xrm.Navigation.openConfirmDialog(confirmStrings, confirmOptions).then(
        function (success) {    
            if (success.confirmed) {
    
                console.log("Dialog closed using OK button.");
    
            }
            else {
                
                console.log("Dialog closed using Cancel button or X.");
                // Stop Save!
                executionContext.getEventArgs().preventDefault();
            }
                
        });
    
    }
     

    Then, onLoad register following function (to show the focus on Documents tab), do pass executionContext:

    function onLoad(executionContext) {
    
    
        // get formContext
    
        var formContext = executionContext.getFormContext(); 
    
        var formType = formContext.ui.getFormType();
    
        if (formType == 2) { // If  update
            
            // Get Nav. Item
            var navItem = formContext.ui.navigation.items.get("navSPDocuments");
    
            // Set focus on Nav. Item Documents
            navItem.setFocus();
    
    
        }    
    
    }

    The above code shows the Document tab when form type is Update. You might need to add another flag to verify if this form is opened first time.

    Please note, above codes are for your reference, you need to tweak and test as per your needs.

  • nidnani Profile Picture
    285 on at

    Hi Wahaj,

    Thank you so much for the response. Really appreciate that.

    I tried the above code and SP one works fine but in Dialog, if I hit Cancel - the record is being saved and order is created. Is there a way that we can stop it from saving when Cancel is selected??

    If not, then:-

    1. Is there a way that I can change the Sharepoint Location folder for Lead from Lead Name to a custom Lead ID text field??

    2. anyway to display Lead related Share Point upload option on the custom entity order form?

    If I can do the above two SP custom changes then I can get away with the Cancel requirement.

    Thanks again for all the help!

  • Verified answer
    Wahaj Rashid Profile Picture
    11,321 on at

    Hi,

    I found the solution.

    You need to do the following steps:

    • Create a Two Options field and set its default value to No. Add this field to the form and make it invisible.
    • Next, register following function on save and pass execution context:

    function onSave(executionContext) {
    
        // Get Form Context
        var formContext = executionContext.getFormContext(); 
    
        
        // Allow save flag
        var allowSave = formContext.getAttribute("new_allowsave").getValue();
        
        if (!allowSave) {
        
            // DO NOT SAVE
           executionContext.getEventArgs().preventDefault();
            
        
            // SHOW CONFIRMATION
    
            var confirmStrings = { text:"This is a confirmation.", title:"Confirmation Dialog" };
            var confirmOptions = { height: 200, width: 450 };
            Xrm.Navigation.openConfirmDialog(confirmStrings, confirmOptions).then(
            function (success) {    
                if (success.confirmed) {
                    console.log("Dialog closed using OK button.");
                    
                    // Set save flag
                    formContext.getAttribute("new_allowsave").setValue(true);
                    
                    // SAVE
                    formContext.data.entity.save();
                }
                else {
    
                    console.log("Dialog closed using Cancel button or X.");
    
                    // DO NOTHING
                }
            });
        
        }
    
    }

    The key is to first stop the save and then show Confirmation Dialog. If we show the confirmation dialog first, the form is automatically saved.

    So let's stop the save first and once the user clicks OK, save the form programmatically.

    The two option fields help us to save the form when the user clicks OK (and to avoid deadlock).

  • nidnani Profile Picture
    285 on at

    Hi Wahaj!! You are amazing! That worked flawlessly. Thank you so much!! You are the best! 

    can you share some resources where I can study and learn more about JS customizations? Like creating dialogs, connecting to forms and all . Would move to learn more. 

    Also, If you don’t mind can you please provide solution for renaming the SharePoint folder name? I managed to remove the GUID but since lead names are common I want to create folders with Lead Name + Lead ID or just LeadId so document link doesn’t break if name changes.. . Let me know if that can be done?

    Thank again for all the help!!

  • Suggested answer
    Wahaj Rashid Profile Picture
    11,321 on at

    Thank you, glad it helped

    Let me answer your other questions:

    • For JavaScript, I would recommend you to go through official docs (client API reference) and go through each method (like try it once and you will come to know its usage), once you know the usage, you can connect the dots to implement your logic. Furthermore, you can look at the following book (section 6-H, JavaScript):

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

    https://carldesouza.com/dynamics-crm/

    • For renaming the SharePoint library, try the solutions posted by Mahender here:

    https://community.dynamics.com/365/sales/f/dynamics-365-for-sales-forum/416421/how-to-rename-sharepoint-folder-name-integrated-with-d365-online

    If you face any trouble implementing the above solutions, please discuss.

  • nidnani Profile Picture
    285 on at

    Hi Wahaj, thank you so much for the resources. Looking into it to learn more.

    I had a quick question about the previous requirement of loading SP documents. I want to trigger the SP page only if the Upload Option is selected as Yes(Upload option is two options field)

    Is there a way to achieve it?

  • Verified answer
    Wahaj Rashid Profile Picture
    11,321 on at

    Hi,

    Sure, you can get the Upload options value and show the Documents tab based on it, code should look something like this:

    function onChaneOfUpload(executionContext) {
    
        // Get Form Context
        var formContext = executionContext.getFormContext(); 
        
            
        // Get upload option value
        var uploadOption = formContext.getAttribute("").getValue();
        
        if (uploadOption) {
        
                var navItem = formContext.ui.navigation.items.get("navSPDocuments");
        
                // Set focus on Nav. Item Documents
                navItem.setFocus();
        
        }
    }

    You can register this function on change of Upload Option field or on load (as per you need).

  • nidnani Profile Picture
    285 on at

    Thanks a lot, Wahaj. That worked pretty fine. After studying your Java Scripts, I was able to create three of my own Java Scripts for different requirements. I love it now. Thanks again.

    Hope I am not asking too many questions, but while exploring more on JavaScripts I thought of linking the related entity documents page using JS.

    So for example; while creating the custom order when a user selects the Upload Contract option  - Yes, CRM redirects to the related Lead entity's SharePoint Documents subgrid instead of the Order entity's documents page.

    Is that possible? If not then can I send an email to the users whenever a new document is uploaded to the Sharepoint Documents Grid??? 

    If I can send an email with every document upload then it would be very simple

    Thank you in advance.

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 > Customer experience | Sales, Customer Insights, CRM

#1
Tom_Gioielli Profile Picture

Tom_Gioielli 170 Super User 2025 Season 2

#2
#ManoVerse Profile Picture

#ManoVerse 70

#3
Jimmy Passeti Profile Picture

Jimmy Passeti 50 Most Valuable Professional

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans