Skip to main content

Notifications

Announcements

No record found.

Customer experience | Sales, Customer Insights,...
Suggested answer

preventDefault not working correctly

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()
}
});

}

  • Suggested answer
    Bipin D365 Profile Picture
    Bipin D365 28,964 Super User 2024 Season 1 on at
    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
    linly 10 on at
    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

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