0
votes

I'm trying to create a simple function which, when ribbon button is pressed, sets entity attribute value to null. Now the problem I am facing is, that the changes I make to the entity are not saved, form reloads and returns previous value.

To the button event I pass 'Task' activity attribute 'actualend'. 'Actual End' field is disabled by default.

ClearField: function (field) {
    if (Xrm.Page.getAttribute(field) == null) return;

    Xrm.Page.ui.controls.get(field).setDisabled(false);
    Xrm.Page.getAttribute(field).setSubmitMode("always");
    Xrm.Page.getAttribute(field).setValue(null);

    if (Xrm.Page.data.entity.getIsDirty()) {
        Xrm.Page.data.entity.save(); //also tried addOnSave(function)
    }
}

Following debugger I was able to track that all changes are made correctly, except that on save() method they are 'discarded', then form reloads with previous value. This code works fine with CRM UR8 yet with CRM UR13 it does not.

Am I missing something?

2
the code works, the only change I made is to write the function as function ClearField(field) {Guido Preite
Have you set breakpoints and ensured that the save method is getting called?Daryl
@Daryl you are right, tried setting up a breakpoint on that method and nope, save() is ignored. Don't know how did I manage to miss that o_o; Thank You! Now to find out why it's not called~WoodyMc

2 Answers

0
votes

As Guido mentions in his comment the code looks good which leads me to think that one of your two if statements is failing.

The first one obviously will fail if it is set to null. A little bit less obvious is that it will fail as well as if the field is not actually on the form (even though it may be a valid attribute of the entity). So step 1, ensure that your field exists on the form.

The second one I'm not sure of... I don't think getIsDirty() is keeps track of programmatic changes, so even though your programmatically updating a field and setting it to always submit, it may be returning false. Regardless of how exactly it's working, the if statement really isn't needed. The Xrm.Page.data.entity.save function will only actually save if it has some value that has changed, so I'd remove your dirty check regardless.

0
votes

Eh, the problem with my issue all this time was that even though the field existed in the form, it never had an entity passed to it, therefore it was unable to save it. I've edited a ribbon button so to pass entity through CrmParameters and the issue was gone. Thank you both for supplying me with possible solutions regardless!