Skip to main content

Notifications

Microsoft Dynamics 365 | Integration, Dataverse...
Answered

TypeError: Cannot read property 'preventDefault' of undefined

(0) ShareShare
ReportReport
Posted on by 17

Hi,
I am getting this error 'TypeError: Cannot read property 'preventDefault' of undefined'.

Requirement is user should not be able to save the form when a field condition is not satisfied.

Code:
var AccountCustomJS = {
telephoneValue: function(executionContext){
debugger;
var formContext = executionContext.getFormContext();
var telephoneVal = formContext.getAttribute("telephone1").getValue();
if(telephoneVal.length != 10){
var alertStrings = { confirmButtonLabel: "Ok", text: "Make sure your phone number is 10 digits", title: "Error" };
var alertOptions = { height: 120, width: 260 };
Xrm.Navigation.openAlertDialog(alertStrings, alertOptions);
executioncontext.getEventArgs().preventDefault();
}
else
var alertStrings = { confirmButtonLabel: "Ok", text: "Phone Number Saved", title: "Success" };
var alertOptions = { height: 120, width: 260 };
Xrm.Navigation.openAlertDialog(alertStrings, alertOptions);
}
}

Thanks in advance.

  • a33ik Profile Picture
    a33ik 84,325 Most Valuable Professional on at
    RE: TypeError: Cannot read property 'preventDefault' of undefined

    Hello,

    If my reply (or replies) answers your question take a minute and verify the answer. You can do it by clicking "Yes" at the top of the reply (or replies) under the label "Does this answer your question?"

  • Ethan_009 Profile Picture
    Ethan_009 17 on at
    RE: TypeError: Cannot read property 'preventDefault' of undefined

    It worked,

    Thanks.

  • Verified answer
    a33ik Profile Picture
    a33ik 84,325 Most Valuable Professional on at
    RE: TypeError: Cannot read property 'preventDefault' of undefined

    Ethan,

    JavaScript is case-sensitive and your issue is in the line

    executioncontext.getEventArgs().preventDefault();

    replace it with

    executionContext.getEventArgs().preventDefault();

    and try it again.

  • Ethan_009 Profile Picture
    Ethan_009 17 on at
    RE: TypeError: Cannot read property 'preventDefault' of undefined

    Yes, I have passed this parameter but it still shows the error.

  • Joana Pinto Profile Picture
    Joana Pinto 740 on at
    RE: TypeError: Cannot read property 'preventDefault' of undefined

    Hi,

    Are you passing the execution context as a parameter?

    pastedimage1605005494714v1.png

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

Announcing Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Announcing Forum Attachment Improvements!

We're excited to announce that attachments for replies in forums and improved…

Vahid Ghafarpour – Community Spotlight

We are excited to recognize Vahid Ghafarpour as our February 2025 Community…

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,965 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 230,817 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Product updates

Dynamics 365 release plans