Skip to main content
Post a question

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id : AxRx6IZGqUcsTY0FojSnOZ
Customer experience | Sales, Customer Insights,...
Suggested answer

preventDefault not working correctly

Like (0) ShareShare
ReportReport
Posted on 28 Apr 2022 08:17:51 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()
}
});

}

  • Suggested answer
    Bipin D365 Profile Picture
    28,961 Moderator on 28 Apr 2022 at 11:05:53
    RE: preventDefault not working correctly

    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/

  • linly Profile Picture
    10 on 28 Apr 2022 at 09:40:28
    RE: preventDefault not working correctly

    I can try it! fall guys

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

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

Daivat Vartak – Community Spotlight

We are honored to recognize Daivat Vartak as our March 2025 Community…

Announcing Our 2025 Season 1 Super Users!

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

Kudos to the February Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 292,642 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 231,593 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156 Moderator

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans
Loading started