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

Form save on click of button

(4) ShareShare
ReportReport
Posted on by 5,514
Hi currently I have a button where on click of it a new form is opening .
Before opening a new form ..current form details should save and after navigating few values should be copied in new form ..
 
 
ExecutionContext.data.entity.save();
Const abcid= executionContext.data.entity.getId();
Const xyz= executionContext.getAttribute("crm-xyz").get value();
Xrm.navigate.open form();
 
How can I add getIsDirty() in this above code as now form is sometimes behaving weirdly(stays in same page or sometimes it ask to save again)
 
Note: requirement is to save a form and open new form..
 
 
 
 
Thanks 
Categories:
I have the same question (0)
  • Waed Ayyad Profile Picture
    9,039 Super User 2025 Season 2 on at
    In which MS D365 are you working?
  • sandeepc Profile Picture
    5,514 on at
    Sales @WaeedAyyad
  • André Arnaud de Calavon Profile Picture
    301,146 Super User 2025 Season 2 on at
    Moved the question to the Dynamics 365 Sales forum.
  • Verified answer
    Daivat Vartak (v-9davar) Profile Picture
    7,835 Super User 2025 Season 2 on at
    Hello sandeepc,
     

    You've already received a comprehensive solution for this scenario in our previous conversation. To reiterate the key points and provide a concise answer based on that discussion:

    The issue you're facing is due to the asynchronous nature of the ExecutionContext.data.entity.save() method. The Xrm.navigate.openForm() is likely being called before the save operation has completed.

    Here's the corrected approach using getIsDirty() and Promises to ensure the save operation finishes before navigation:

    function onButtonClick(executionContext) {
        var formContext = executionContext.getFormContext();
        if (formContext.data.entity.getIsDirty()) {
            formContext.data.entity.save().then(
                function successCallback(saveResult) {
                    var abcid = formContext.data.entity.getId();
                    var xyz = formContext.getAttribute("crm-xyz").getValue();
                    var formParameters = {};
                    if (abcid) {
                        formParameters["relatedRecordId"] = abcid;
                        formParameters["relatedRecordType"] = formContext.data.entity.getEntityName();
                    }
                    if (xyz) {
                        formParameters["new_xyzField"] = xyz; // Replace with actual schema name
                    }
                    var options = {
                        entityLogicalName: "your_new_entity_logical_name", // Replace with actual logical name
                        parameters: formParameters
                    };
                    Xrm.Navigation.openForm(options);
                },
                function errorCallback(error) {
                    console.log("Error during save: " + error.message);
                    // Handle error appropriately
                }
            );
        } else {
            var abcid = formContext.data.entity.getId();
            var xyz = formContext.getAttribute("crm-xyz").getValue();
            var formParameters = {};
            if (abcid) {
                formParameters["relatedRecordId"] = abcid;
                formParameters["relatedRecordType"] = formContext.data.entity.getEntityName();
            }
            if (xyz) {
                formParameters["new_xyzField"] = xyz; // Replace with actual schema name
            }
            var options = {
                entityLogicalName: "your_new_entity_logical_name", // Replace with actual logical name
                parameters: formParameters
            };
            Xrm.Navigation.openForm(options);
        }
    }

     

    Key Changes:

    1. formContext: Using the recommended formContext.

    2. formContext.data.entity.getIsDirty(): Checks for unsaved changes.

    3. Asynchronous save() with .then(): The Xrm.Navigation.openForm() is called within the successCallback function, ensuring it executes after the save operation completes successfully.

    4. Error Handling: Includes an errorCallback for the save() operation.

    5. Xrm.Navigation.openForm(): Using the modern navigation API.

    6. Passing Values: Demonstrates how to pass values to the new form using the parameters option.

    7.  

    Remember to replace the placeholder values ("your_new_entity_logical_name" and "new_xyzField") with the actual schema names relevant to your environment. This approach guarantees that the form is saved before navigation, resolving the inconsistent behavior you were observing.

     
    If my answer was helpful, please click Like, and if it solved your problem, please mark it as verified to help other community members find more. If you have further questions, please feel free to contact me.
     
    My response was crafted with AI assistance and tailored to provide detailed and actionable guidance for your Microsoft Dynamics 365 query.
     
    Regards,
    Daivat Vartak

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 70 Super User 2025 Season 2

#2
Gerardo Rentería García Profile Picture

Gerardo Rentería Ga... 33 Most Valuable Professional

#3
Daniyal Khaleel Profile Picture

Daniyal Khaleel 32 Most Valuable Professional

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans