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 CRM (Archived)

Unsaved Changes appear even after form is saved.

(0) ShareShare
ReportReport
Posted on by 277

Hi 

I have wrote a series o JScripts a requirement. 

Firstly: When two records are selected and ribbon button is clicked, it will open a new record of another entity populating the selected record IDs in couple of lookup fields.

function OpenBlankUI(selectedIds) {

    var windowOptions = {
        openInNewWindow: true
    };

    var parameters = {};
    parameters["new_name"] = "Dupe Opportunity";
    //debugger;

    var opp1OppName = "";
    var opp2OppName = "";
    var loadForm = false;

  
        //first opportunity
        parameters["new_opportunity1"] = selectedIds[0];
     

        ////Second Opportunity
        parameters["new_opportunity2"] = selectedIds[1];
        
        Xrm.Utility.openEntityForm("new_oppdupe", null, parameters, windowOptions);
  
}

Secondly: I have added an OnLoad function to populate few fields. 

function OnOpportunity_FormLoad() {

    if (Xrm.Page.ui.getFormType() == "1" || Xrm.Page.ui.getFormType() == 1) {
        var urlParams = Xrm.Page.context.getQueryStringParameters();

        var windowOptions = {
            openInNewWindow: true
        };

        var parameters = {};
        parameters["new_name"] = "Net Opportunity";
        //debugger;

        var oppOppName1 = "";
        var oppOppName2 = "";
        var loadForm = false;

        SDK.REST.retrieveRecord(
                            urlParams.new_opportunity1,
                            "Opportunity",
                            null, null,
                            function (opportunity) {

                                setOpportunityFormFields(opportunity, true);

                            },
                            function () {
                                alert("ERROR: new_opportunity1 ");
                            }
                            );

        SDK.REST.retrieveRecord(
                           urlParams.new_opportunity1,
                          "Opportunity",
                          null, null,
                          function (opportunity) {

                              setOpportunityFormFields(opportunity, false);

                          },
                          function () {
                              alert("ERROR: new_opportunity1 ");
                          }
                          );

        //Xrm.Page.getAttribute(arg).setSubmitMode()

        Xrm.Page.data.entity.attributes.forEach(
    function (attribute, index) {
        Xrm.Page.getAttribute(attribute.getName()).setSubmitMode("always");
    });

    }
}


Finally:  function setOpportunityFormFields

function setOpportunityFormFields(opportunity, isOpportunity1) {
    if (isOpportunity1) {
        var lookup = new Array();
        lookup[0] = new Object();
        lookup[0].id = opportunity.OpportunityId;
        lookup[0].name = opportunity.Name;
        lookup[0].entityType = "opportunity";
        Xrm.Page.getAttribute("new_opportunity1").setValue(lookup);

        //text fields
        Xrm.Page.getAttribute("new_street1").setValue(opportunity.new_street1);
        Xrm.Page.getAttribute("new_city").setValue(opportunity.new_city);

  //OptionSet fields
        if (opportunity.new_AddressStatus != null) {
            Xrm.Page.getAttribute("new_addressstatus").setValue(opportunity.new_AddressStatus.Value);     //OptionSet  
        }

//lookup fields
        if (opportunity.CustomerId != null && opportunity.CustomerId.Id != null) {
            lookup = new Array();
            lookup[0] = new Object();
            lookup[0].id = opportunity.CustomerId.Id;
            lookup[0].name = opportunity.CustomerId.Name;
            lookup[0].entityType = "account";
            Xrm.Page.getAttribute("new_customerid").setValue(lookup);
        }

        if (opportunity.new_oppprimarycontactsid != null && opportunity.new_oppprimarycontactsid.Id != null) {
            lookup = new Array();
            lookup[0] = new Object();
            lookup[0].id = opportunity.new_oppprimarycontactsid.Id;
            lookup[0].name = opportunity.new_oppprimarycontactsid.Name;
            lookup[0].entityType = "contact";
            Xrm.Page.getAttribute("new_oppprimarycontactsid").setValue(lookup);
        }
}

else
{
        var lookup = new Array();
        lookup[0] = new Object();
        lookup[0].id = opportunity.OpportunityId;
        lookup[0].name = opportunity.Name;
        lookup[0].entityType = "opportunity";
        Xrm.Page.getAttribute("new_secondaryopportunity").setValue(lookup);

        //text fields                               
        Xrm.Page.getAttribute("new_street11").setValue(opportunity.new_street1);
        Xrm.Page.getAttribute("new_city1").setValue(opportunity.new_city);

 if (opportunity.CustomerId != null && opportunity.CustomerId.Id != null) {
            lookup = new Array();
            lookup[0] = new Object();
            lookup[0].id = opportunity.CustomerId.Id;
            lookup[0].name = opportunity.CustomerId.Name;
            lookup[0].entityType = "account";
            Xrm.Page.getAttribute("new_customerid1").setValue(lookup);
        }

        if (opportunity.new_oppprimarycontactsid != null && opportunity.new_oppprimarycontactsid.Id != null) {
            lookup = new Array();
            lookup[0] = new Object();
            lookup[0].id = opportunity.new_oppprimarycontactsid.Id;
            lookup[0].name = opportunity.new_oppprimarycontactsid.Name;
            lookup[0].entityType = "contact";
            Xrm.Page.getAttribute("new_oppprimarycontactsid1").setValue(lookup);
        }
}
}


I believe this is the blacksheep.

Everything works according to requirement except for unsaved changes appearing even after saved.

I tried it on CRM 2015 and worked fine, after upgraded to CRM 2016, I'm having issue with unsaved changes. When I comment out setOpportunityFormFields from the OnLoad script the form seem to be saving and doesn't show unsaved changes error.

Sorry for the long post, just wanted to be clear.

*This post is locked for comments

I have the same question (0)
  • a33ik Profile Picture
    84,331 Most Valuable Professional on at

    Hello,

    Do you have onload scripts/business rules in your new_oppdupe form?

  • Turbo Forms Profile Picture
    277 on at

    Hi Andrew

    function OnOpportunity_FormLoad() is ran OnLoad of form. the function is mention in my post. Thank you

  • Suggested answer
    a33ik Profile Picture
    84,331 Most Valuable Professional on at

    Hello,

    I don't see any reason why form still shows unsaved data in your code. I would suggest to set breakpoint to onload handler and check why it fills some fields. Small note - you can replace

    if (Xrm.Page.ui.getFormType() == "1" || Xrm.Page.ui.getFormType() == 1) {

    with just

    if (Xrm.Page.ui.getFormType() == 1) {

    in javascript reality "1" is equal to 1 so both checks will return true.

  • ashlega Profile Picture
    34,477 on at

    HI,

     instead of those getFormType conditions, could you try this:

    (Xrm.Page.data.entity.getId() == null || Xrm.Page.data.entity.getId() == ‘undefined’ || Xrm.Page.data.entity.getId() == ”)

    ?

  • Turbo Forms Profile Picture
    277 on at

    Hi Andrew, I have debugged the onload scriptseveral times. All the fields are being filled when getformtype = 1. If I save the form, the getformtype becomes 2 which is a saved form. Almost all the fields are dirty when I ran a script to check for dirty fields. When I refresh the updated form, the form type becomes 1 again.

    I had same issue on CRM 2015 and setsubmitmode("always") fixed it. Now when we moved to CRM 2016 this issue came back.

  • Turbo Forms Profile Picture
    277 on at

    Hi Alex

    I tried it, but unsaved changes issue continues.

  • Suggested answer
    Hemant Kumar Sahu Profile Picture
    1,829 on at

    Hi Turbo Forms,

    Check once , Do you have any Business Rules that sets any field value.

    There may be dependencies with other fields.

    Hemant

  • Suggested answer
    Turbo Forms Profile Picture
    277 on at

    I have worked it around by adding an if condition to setOpportunityFormFields function.

     if (Xrm.Page.ui.getFormType() != 1) {

           return;

       }

    As setsubmitmode is not submitting the dirty fields.

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 CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans