Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

getEventArgs().preventDefault not working

Posted on by 1,771

Hi,

I replaced event.returnValue with getEventArgs().preventDefault() but it is not working.  Please find below the scripts and screen-shots for you reference.  It give error and save record.  Please help to find solution.

 

s11.png

function Form_onsave(executionOb)

{

if((Xrm.Page.getAttribute("new_accountid").getValue() != null ) &&

(Xrm.Page.getAttribute("new_name").getValue() == null ))

{

 var results = GetRelatedcontacts();

 if (results != 0)

   {

   alert("Technology details of this account is already in CRM. Please close this window and select existing record from view");

           Xrm.Page.getAttribute("new_name").setValue() =null;

           Xrm.Page.getControl("new_accountid").setFocus(true);

           executionObj.getEventArgs().preventDefault();

         }

   }

}

It display the alert message and give below error but save record.

 6283.s1.png

 

 

*This post is locked for comments

  • faramendi Profile Picture
    faramendi 100 on at
    RE: getEventArgs().preventDefault not working

    Hi!

    I am facing a similar problem!

    I pass the context as 1st parameter

    this is my test code

    function executeOnLoad(_context) {
    _context.getEventArgs().preventDefault();

    }

    context is populated but getEventArgs() returns null

    any idea??

    thanks

  • Suggested answer
    shivaram Profile Picture
    shivaram 3,315 on at
    RE: getEventArgs().preventDefault not working

    Hi Noushad,

    You set null value like 

      Xrm.Page.getAttribute("new_name").setValue() =null;

    It should be 

      Xrm.Page.getAttribute("new_name").setValue(null);

     


    And your context name is different.

    On Form_Save function you pass context name is executionOb

    And while Prevent you gave executionObj.

    So final code should be

    function Form_onsave(executionObj)

    {

    if((Xrm.Page.getAttribute("fieldname").getValue() != null ) &&

    (Xrm.Page.getAttribute("fieldname").getValue() != null ))

    {
    var result=getContactDetails();
    if(result!=null){


    alert("Technology details of this account is already in CRM. Please close this window and select existing record from view");

    Xrm.Page.getAttribute("fieldname").setValue(null);

    Xrm.Page.getControl("fieldname").setFocus(true);

    executionObj.getEventArgs().preventDefault();

    }

    }

    }

    Hope it helps you

    Thanks

  • Suggested answer
    Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: getEventArgs().preventDefault not working

    Hi Noushad,

    Please check the execution context, whether you did pass it to the function or not.

    The executionObj is getting null, so it is not able to access the getEventArgs method of context.

    Hope this helped.

    Thanks

    Phanidhar

  • P NOUSHAD Profile Picture
    P NOUSHAD 1,771 on at
    RE: getEventArgs().preventDefault not working

    Please any update

  • P NOUSHAD Profile Picture
    P NOUSHAD 1,771 on at
    RE: getEventArgs().preventDefault not working

    It is not working

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: getEventArgs().preventDefault not working

    Share your Skype id

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: getEventArgs().preventDefault not working

    Is it working??

    I changed from:

    alert("1");

    {

    to

    {

    alert("1");

  • P NOUSHAD Profile Picture
    P NOUSHAD 1,771 on at
    RE: getEventArgs().preventDefault not working

    let me know where is change in your code

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: getEventArgs().preventDefault not working

    Use this code:

    function Form_onsave(executionObj)

    {

    if((Xrm.Page.getAttribute("new_accountid").getValue() != null ) &&

    (Xrm.Page.getAttribute("new_name").getValue() == null ))

    {

    alert("1");

    var results = GetRelatedcontacts();

    if (results != 0)

      {

        alert("2");

     alert("Technology details of this account is already in CRM. Please close this window and select existing record from view");

              Xrm.Page.getAttribute("new_name").setValue(null);

              Xrm.Page.getControl("new_accountid").setFocus(true);

              executionObj.getEventArgs().preventDefault();

         }

     }

    }

  • P NOUSHAD Profile Picture
    P NOUSHAD 1,771 on at
    RE: getEventArgs().preventDefault not working

    Hi,

    I have corrected parameter name.  but I am getting the below error please check and save the record.  Do I have declare the variable in script.

    s111.png

    function Form_onsave(executionObj)
    {

    if((Xrm.Page.getAttribute("new_accountid").getValue() != null ) &&
    (Xrm.Page.getAttribute("new_name").getValue() == null ))
    alert("1");
    {

    var results = GetRelatedcontacts();

     if (results != 0)
       {
         alert("2");
      alert("Technology details of this account is already in CRM. Please close this window and select existing record from view");
               Xrm.Page.getAttribute("new_name").setValue(null);
               Xrm.Page.getControl("new_accountid").setFocus(true);
               executionObj.getEventArgs().preventDefault();

          } 
      }

    }

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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,269 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,198 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans