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)

Mark complete won't fire again after preventDefault called

(0) ShareShare
ReportReport
Posted on by 15

I have a situation where if the user is marking an activity as complete, certain fields become mandatory. So on Save, I check the save mode, and then if Mark Complete, and if fields aren't filled in, alert the user and prevent the save. This is all fine, however, if the user clicks Mark Complete a second time, no javascript is fired (and no save is performed). Clicking Save does work though?? You can try it out, on say, the Task activity.

Here is the code to attahc to Form OnSave:

function CheckMandatory(executionObj)
{
  if (executionObj.getEventArgs().getSaveMode() == 58) {
    if (Xrm.Page.getAttribute("description").getValue() == null) {
      alert("Description is mandatory when completing activity");
      executionObj.getEventArgs().preventDefault();
      return false;
    }
  }
}

*This post is locked for comments

I have the same question (0)
  • Verified answer
    Tobias Koller Profile Picture
    on at


    hi,

    i just debugged the behaviour and could reproduce this under CRM 2016.

    For WHATEVER reason they have a logic that prevent you from click the button again.

    Internally they have this code:

    if (!Mscrm.CommandBarActions.$O) {
            Mscrm.CommandBarActions.$O = true;
            var $v_5 = new Xrm.SaveOptions;
            $v_5.useSchedulingEngine = false;
            Xrm.Page.data.save($v_5).then($v_0, $v_1); //here is the call to your save-method
        }



    You can see that they variable Mscrm.CommandBarActions.$O is immediatelly set to true after check if it is false.
    Only if this variable is false you can call the save-method again. Internally they set this variable only to true, if you don't Abort the save-process (wtf ;))

    So what I just did is to set this variable to false again.

    function CheckMandatory(executionObj)
    {
      if (executionObj.getEventArgs().getSaveMode() == 58) {
        if (Xrm.Page.getAttribute("description").getValue() == null) {
          alert("Description is mandatory when completing activity");
          executionObj.getEventArgs().preventDefault();
          Mscrm.CommandBarActions.$O = false;
          return false;
        }
      }
    }

    Please note:
    this is just a workaround and not really supported. But if you need to get this work this is a quick solution.

    It could also be possible that in your case this variable is named differently (but I guess its the same.)

    Best regards
    Tobias

  • Adrian Launchbury Profile Picture
    15 on at

    Nice one. I was writing as plugins, but prefer to keep client side, so I'll use this, thank-you very much.

  • skhan Profile Picture
    120 on at

    Thanks it worked for me. I believe the issue is occurring everywhere a PreventDefault function is used.

    I was having issue on Lead entity.

    1. Disqualify Lead--> Validations-->PreventDefault

    2. Click on Reactivate Lead (it was not working)

  • Suggested answer
    syscon Profile Picture
    25 on at

    The internal field ($0) seems to change often based on update etc so best avoided.

    Ive found the thing that works best for me is to set a error notification on a field (and remove it after x seconds/ms) which achieves the same effect as prevent default.

    code snip:

    function validateSave(context) {
        var args = context.getEventArgs();
        var isMarkAsComplete = args.getSaveMode() == 58;
    
    
        var validateionNotificationId = "E0D0F9E4-FD50-4DE1-A648-2862FDAF05D1";
        var dueDateControl = Xrm.Page.getControl("scheduledend");
        Xrm.Page.ui.clearFormNotification(validateionNotificationId);
    
        if (isMarkAsComplete) {
    
            var outcome = Xrm.Page.getAttribute("t_actionoutcome").getValue();
            if (outcome == null) {
    
                var message = "Record has not been saved: When marking a phone call record as complete, you must enter an action outcome.";
                Xrm.Page.ui.setFormNotification(message, "WARNING", validateionNotificationId);
    
                //args.preventDefault();
                //Mscrm.CommandBarActions.$Q  = false;
    
                //add validation error to block save, then remove afew afew secs.
                //Workaround for issue where preventdefault causes complete button on ribbon to be disabled (as it doesnt go through the promise.reject, threfore doesnt tidy up)
                //community.dynamics.com/.../190187
    
                var actionoutcomeControl = Xrm.Page.getControl("t_actionoutcome");
                actionoutcomeControl.setNotification("Action outcome is Required for completion", validateionNotificationId);
    
                //removal of notification is required else validate wont be called again (execution pipeline validates first)
                setTimeout(function () {
                             actionoutcomeControl.clearNotification(validateionNotificationId);
                }, 1000);
    
                return false;
            }
        }
    }


  • skhan Profile Picture
    120 on at

    CRM Version `8.2.5.4`

    Anyone having the issue, please change the line to `Mscrm.CommandBarActions.$R=false;`

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