web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

No record found.

News and Announcements icon
Community site session details

Community site session details

Session Id :
Customer experience | Sales, Customer Insights,...
Suggested Answer

preventDefault not working correctly

(0) ShareShare
ReportReport
Posted on by 5

Hi,

i am trying to get my form to give an alert and prevent save if it meets a certain criteria (the if statement) and save if anything else but if i stick preventDefault inside the IF it still allows me to save. I have tried putting it outside the IF but then no matter what i can't save the form and my formContext.data.save() doesn't work.

Please can someone help?

Thank you

function denyCreateVariationSor(executionContext)
{
debugger;

var id = Xrm.Page.getAttribute("gilm_variation").getValue()[0].id;

var url = 'gilmartins-test.crm11.dynamics.com/.../gilm_variations eq ' + id + ')';
var variationStatus;

var formContext = executionContext.getFormContext();

fetch(url).then(response => response.json()).then(data =>
{
var arr = data.value;
variationStatus = arr[0]["gilm_variationstatus"];
if (variationStatus != 870110000)
{
executionContext.getEventArgs().preventDefault();
alert("You can't add a SOR onto a completed/cancelled variation");
}
else
{
formContext.data.save()
}
});

}

I have the same question (0)
  • linly Profile Picture
    10 on at

    I can try it! fall guys

     It's so much fun that you need to fight hard

  • Suggested answer
    Bipin D365 Profile Picture
    28,985 Moderator on at

    HI,

    Add below code on onchange of gilm_variation field-

    var saveAllowed=true;

    function denyCreateVariationSor(executionContext)

    {

    debugger;

    var id = Xrm.Page.getAttribute("gilm_variation").getValue()[0].id;

    var url = 'gilmartins-test.crm11.dynamics.com/.../gilm_variations eq ' + id + ')';

    var variationStatus;

    var formContext = executionContext.getFormContext();

    fetch(url).then(response => response.json()).then(data =>

    {

    var arr = data.value;

    variationStatus = arr[0]["gilm_variationstatus"];

    if (variationStatus != 870110000)

    {

    saveAllowed=false;

    }

    else

    {

    formContext.data.save()

    }

    });

    }

    And then add below code on onsave of your form -

    function Form_OnSave(executionContext)

    {

    if(saveAllowed==false)

    {

    executionContext.getEventArgs().preventDefault();

    alert("You can't add a SOR onto a completed/cancelled variation");

    }

    }

    I hope this works. I am making use of Global variable to achieve the requirement.

    Please mark my answer verified if this is helpful!

    Regards,

    Bipin Kumar

    Follow my Blog: xrmdynamicscrm.wordpress.com/

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

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Stars!

Meet the Microsoft Dynamics 365 Contact Center Champions

We are thrilled to have these Champions in our Community!

Congratulations to the April Top 10 Community Leaders

These are the community rock stars!

Leaderboard > Customer experience | Sales, Customer Insights, CRM

#1
ManoVerse Profile Picture

ManoVerse 101 Super User 2026 Season 1

#2
11manish Profile Picture

11manish 83

#3
Muhammad Shahzad Shafique Profile Picture

Muhammad Shahzad Sh... 69 Most Valuable Professional

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans