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 :
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,983 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

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 > Customer experience | Sales, Customer Insights, CRM

#1
Tom_Gioielli Profile Picture

Tom_Gioielli 70 Super User 2025 Season 2

#2
Gerardo Rentería García Profile Picture

Gerardo Rentería Ga... 43 Most Valuable Professional

#3
Daniyal Khaleel Profile Picture

Daniyal Khaleel 32 Most Valuable Professional

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans