locked
CRM 2015: onChenge event seems to fire after record closes causing an error RRS feed

  • Question

  • I have inherited some JavaScript which is operating on a custom activity.

    During the OnSave event I have something like this:

    if ((Xrm.Page.data.entity.getEntityName() === 'phonecall') || (Xrm.Page.data.entity.getEntityName() === 'new_customentity')) {
                Xrm.Page.getAttribute('actualend').setValue(new Date());
                // Calculate duration.
                Xrm.Page.getAttribute('actualend').fireOnChange();
            }

    When I mark the record as complete I get this error: "Cannot update Closed or Cancelled Activity".

    It looks like the system is closing the record before I update the actualend field.

    FYI: The fireonchange function has some code to calculate the duration of the activity and also set the value of a field with the result.

    This code is upgraded from 2011.

    Am I correct in thinking that the record is closing before onsave event?  If that is true, how can I update fields during the completion process if I can't use the onsave event?

    It is always passible that this is all a red herring and I have another problem!  There is rather a lot of code.  Certainly commenting out the code above means the error goes away.

    Tuesday, October 6, 2015 1:41 PM

All replies

  • Hi Gordon,

    You can check the OnSave  event  context  value in Javascript  and  perform your action before the onSave event is completed.

    Check below blog.

    http://worldofdynamics.blogspot.in/2011/08/dynamics-crm-2011-perform-jscript.html

    Hope it helps.


    If you find this post helpful then please "Vote as Helpful" and "Mark As Answer". Thanks and Regards, Mohammad Yusuf Ansari http://microxrm.blogspot.in

    Tuesday, October 6, 2015 1:56 PM
  • The whole thing is currently wrapped in this:

    var saveMode = executionObj.getEventArgs().getSaveMode();
    
        if (saveMode === SAVE_MODE_SAVEASCOMPLETED) {
    
            ....

    The constant being 58.

    I will have a close read of the article to see if it will help.

    Does this mean that the onSave event fires more than once then and that I filtering which code is run based on something like a pipeline stage?

    Tuesday, October 6, 2015 2:03 PM
  • I see.  It does 58 and then 2 and then 2 again so it is called three times.

    That has improved my understanding somewhat.  Seems like it should update without error but I am guessing something is happening out of sequence.

    Maybe I am triggering an event which is triggering another save!?



    Tuesday, October 6, 2015 2:31 PM
  • Hi,

    Value 58 is showing as per below table. Now you can validate it.

    Entity
    Event Mode
    Value
    Activities
    Save as Completed
    58

    Regards,


    If you find this post helpful then please "Vote as Helpful" and "Mark As Answer". Thanks and Regards, Mohammad Yusuf Ansari http://microxrm.blogspot.in

    Tuesday, October 6, 2015 2:37 PM
  • When the "actualend" code segment is commented, the page executes the onSave once within Mode 58.

    When the code s present, the initial save is followed by two further save attempts (Mode 2) and it is here that I see two "cannot update" errors because it would seem that the record is completed by this stage.

    I believe the record correctly saves during the Mode 58 call.  I can't find a way to ignore the Mode 2 calls without causing problems elsewhere.

    If I had written this module I think that I would have done the "actualend" code in a Plugin which would avoid this issue.  I am tempted to do this as I can't see a way to stop the errors (check if the record is already closed for example).  Is the record state stored in the context?

    The strange thing is that the "actualend" code is passed over in the Mode 2 calls.

    There are rather a lot of setSubmitMode lines knocking around in the onSave function which might have an impact.

    Tuesday, October 6, 2015 4:18 PM
  • Why can't you simply check if the Actualend is present or not ? Set the value only if it is empty.. Don't you think it will solve the problem ?

    Imposible it self is having Posible.. Dont leave it until you get it..

    Tuesday, October 6, 2015 4:22 PM