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)

Attribute Changed Via setValue Not Saved When Navigating Away From Form In CRM Online

(0) ShareShare
ReportReport
Posted on by

I have a web resource that takes input from the user and uses it to set the value of an attribute on the form. I have a simplified version of it below, where you can enter a value into a textbox and use the button to set the value on the attribute. I'm using this in a simplified test form, which in addition to the HTML web resource, also has the attribute on it, which is just a simple text field.

The problem is when the user is on the update form, if they use the web resource to set the attribute value, then instead of waiting for the form to auto-save, or manually saving the form, they navigate away from the page without editing any other fields, the changed value does not save. Before navigating away from the form, the icon in the lower right corner shows that there are unsaved changes, so CRM does know about the change, it just doesn't save it before navigating away. Also, if the user changes the value in the attribute's field directly, then the change saves before navigating away.

This is in CRM Online, version 1612 (8.2.0.774). The trouble is, we're moving away from CRM on-prem, where we have version 8.1.0.359. When I try the same code in that on-prem environment, the changed value does save.

Is there a way to change my code or form so that the changed value saves before the user navigates away in CRM online?

<!DOCTYPE html>

<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="utf-8" />
    <title></title>
    <script>
        function setFormField() {
            var attribute = parent.Xrm.Page.getAttribute("tre_amyfield1");
            if (attribute) {
                var value = document.getElementById("input").value;
                attribute.setValue(value);
            }
        }
    </script>
</head>
<body>
    <input type="text" id="input"/>
    <input type="button" onclick="setFormField();" value="Set Value"/>
</body>
</html>


Adding attribute.setSubmitMode("always") doesn't help, nor does adding attribute.fireOnChange(). If I add code to the OnSave event of the form, that code gets executed in our on-prem environment when the user navigates away, but not in the online environment.

*This post is locked for comments

I have the same question (0)
  • Suggested answer
    Nithya Gopinath Profile Picture
    17,078 on at

    Hi Amy,

    You can write the following Javascript code to save the record.

    function setFormField() {
    var attribute = parent.Xrm.Page.getAttribute("tre_amyfield1");
    if (attribute) {
    var value = document.getElementById("input").value;
    attribute.setValue(value);
    }
    parent.Xrm.Page.data.save();
    }

    Hope this helps.

  • Community Member Profile Picture
    on at

    Thanks, and I should have added that we'd like to avoid that, if possible, as it caused some other issues with our forms.

    First, we didn't like that it caused the form to refresh every time someone changed the value.

    Second, it seemed to cause another problem, where if the user stays on the form, changing any field values and saving a few times, the form starts acting strange, so that when the user focuses on a field to edit it, the field immediately loses focus and won't let the user edit. That wasn't in my simple test form, but in a more complex one, so I could debug it some more. But I did notice that it stopped happening when we stopped calling save() after setValue, so I was hoping we could just find a way without needing to call save().

  • Verified answer
    tw0sh3ds Profile Picture
    5,600 on at

    Hi,

    Firstly, I would start with support ticket to MS as it looks like a platform bug. What about changing the value of a field not from web resource but from some other form logic? If something like this is not working, then I can see many problems in many systems, so MS must be notified about something like that.

    Secondly, you should save the value not using form save (like in suggested answer) but using WebAPI - it will not refresh the form and if you do it asynchronously, it would be invisible for the user.

  • Community Member Profile Picture
    on at

    Yes, I can reproduce it when changing the value from within form logic rather than from a web resource. It's a bit artificial, because it has to be from a change that wouldn't otherwise let CRM know the attributes have changed. So for example, I can't make it happen if I set the attribute's value in the change event of another attribute that the user changes. However, if I change the value from within a tab state change handler, then I can reproduce it.

    So, I suppose we will have to go to MS with it. And possibly look into using the Web API as you suggest, if needed.

    Thanks!

  • Glenn Goodwin Profile Picture
    1,155 on at

    Hi,

    We also get exactly the same issue.  We have upgraded to Dynamics 365 (Version 1612 (8.2.1.176)) from CRM 2015 and the issue has started appearing.

    A user lost a morning of work as they were scrolling to the next record after updating the field with a Web Resource.

    Interested if you heard a resolution from Microsoft.

    Thanks
    Glenn

  • Community Member Profile Picture
    on at

    One of the other developers on my team worked through a support ticket with Microsoft, so I don't have all the details. It looks like Microsoft confirmed this is an issue they will fix in a future release, however I don't think they indicated when that would be. They also were not able to suggest any supported workaround.

  • Community Member Profile Picture
    on at

    Hi - Were you able to solve this issue?

  • Community Member Profile Picture
    on at

    We have a maybe related issue with Dynamics 365 version 1612 (8.2.2.112)

    We have a field where onchance does a setvalue to the following field in the form, but if you leave this field (and trigger the onchange) with TAB putting you into this next field as the onchange fires, ANY _clicking_ away from this field clears the set value again.

    If we do shift+TAB to leave the field (thus _not_ ending up in the changed field as the onchange fires) we do not see the issue.

  • Suggested answer
    Preeti Sharma Profile Picture
    2,678 on at

    Please try to replace method with following method

    function setFormField() {

               var attribute = parent.Xrm.Page.getAttribute("tre_amyfield1");

               if (attribute) {

                   var value = document.getElementById("input").value;

                   attribute.setValue(value);

    attribute.setSubmitMode("always");

               }

           }

  • Community Member Profile Picture
    on at

    Finn E. Theodorsen  I was experiencing a similar problem. Mine was with a date field that was below a lookup field on the form. We had an onChange on the lookup field that set a date in the field below. Problem was, that if you selected a value in the lookup and then pressed "TAB" to switch to the other field (the date field), the date would populate, but as soon as you clicked away the data in the date field disappeared. 

    I found two solutions to this problem. 

    Firstly;

    Have a hidden field in between the two fields, and on the onChange, do the following;

    //display and pull focus from date field
    Xrm.Page.getAttribute("hiddenField").setVisible(true);
    Xrm.Page.getControl("hiddenField").setFocus();
    
    //force the script to another thread to activate the focus above
    setTimeout(() => {
        //pupulate your date value
        Xrm.Page.getAttribute("dateField").setValue(myVal); 
        //hide the hidden field again
        Xrm.Page.getAttribute("hiddenField").setVisible(false);
    },20)]
    

    Now I know this is quite ugly but it gets the job done. 

    Secondly (and this one is a lot simpler);

    Rearrange your fields on the form so that the date field is not directly after the lookup field. This has the same effect as the code above.

    Hope this helps. 

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