Skip to main content

Notifications

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,472
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:
  • Verified answer
    Daivat Vartak (v-9davar) Profile Picture
    7,336 Super User 2025 Season 1 on at
    Form save on click of button
    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
  • André Arnaud de Calavon Profile Picture
    294,941 Super User 2025 Season 1 on at
    Form save on click of button
    Moved the question to the Dynamics 365 Sales forum.
  • sandeepc Profile Picture
    5,472 on at
    Form save on click of button
    Sales @WaeedAyyad
  • Waed Ayyad Profile Picture
    8,450 Super User 2025 Season 1 on at
    Form save on click of button
    In which MS D365 are you working?

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

Announcing the Engage with the Community forum!

This forum is your space to connect, share, and grow!

🌸 Community Spring Festival 2025 Challenge Winners! 🌸

Congratulations to all our community participants!

Adis Hodzic – Community Spotlight

We are honored to recognize Adis Hodzic as our May 2025 Community…

Leaderboard > Customer experience | Sales, Customer Insights, CRM

#1
Daivat Vartak (v-9davar) Profile Picture

Daivat Vartak (v-9d... 225 Super User 2025 Season 1

#2
Vahid Ghafarpour Profile Picture

Vahid Ghafarpour 136 Super User 2025 Season 1

#3
Eugen Podkorytov Profile Picture

Eugen Podkorytov 106

Overall leaderboard

Product updates

Dynamics 365 release plans