Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Customer experience | Sales, Customer Insights,...
Answered

eventArgs.preventDefault(); is not working while restricting to page save in crm 365

(0) ShareShare
ReportReport
Posted on by 2,665

Hi All,

Using below method calling on save form & on change of prod sub type & dob change too.

/// Calculate age based on DOB & pop up error if age is not between min & max age value from Product Config
Deal_Retail.calculateAgeBasedOnDOB = async function(executionContext) {
"use strict";
try {
if (executionContext !== null && executionContext !== undefined) {
var eventArgs = executionContext.getEventArgs();
if (FormContext !== undefined && FormContext !== null) {
var productId = null;
var customerType = FormContext.getAttribute(attributes.CUSTOMER_TYPE); //New or existing
var customerTypeVal = null;
if (customerType !== null && customerType !== undefined) {
customerTypeVal = customerType.getValue();
}
var subproductAttribute = FormContext.getAttribute(attributes.PRODUCTSUBTYPE);
var productAttribute = FormContext.getAttribute(attributes.PRODUCT_TYPE);
if (subproductAttribute !== undefined && subproductAttribute !== null) {
var subProductValue = subproductAttribute.getValue();
if (subProductValue !== undefined && subProductValue !== null) {
productId = subProductValue[0].id.replace(/[{}]/g, "");
} else {
if (productAttribute !== undefined && productAttribute !== null) {
var productValue = productAttribute.getValue();
if (productValue !== undefined && productValue !== null) {
productId = productValue[0].id.replace(/[{}]/g, "");
}
}
}
}

if (productId !== null) {
var dobFormContext = FormContext.getAttribute(attributes.DOB);
if (dobFormContext !== undefined && dobFormContext !== null) {
var dobValue = dobFormContext.getValue();
if (dobValue !== null && dobValue !== undefined) {
var val = Deal_Retail.GetAge((dobValue.getMonth() + 1), dobValue.getDate(), dobValue.getFullYear());
var ageYear = val.years;
var agemonth = val.months;
var ageday = val.days;
var productConfigRecordsMin = await Deal_Retail.GetProductConfigRecords(ProductConfiguration.MinimumAgeForAnnuityPensionPlan, productId);
var productConfigRecordsMax = await Deal_Retail.GetProductConfigRecords(ProductConfiguration.MaximumAgeForAnnuityPensionPlan, productId);
if (productConfigRecordsMin !== undefined && productConfigRecordsMin !== null && productConfigRecordsMax !== undefined && productConfigRecordsMax !== null) {
var minValue = parseInt(productConfigRecordsMin.entities[0].new_value);
var maxValue = parseInt(productConfigRecordsMax.entities[0].new_value);
var isEligible = (minValue !== null && ageYear >= minValue) && (maxValue !== null && ageYear <= maxValue);
var isRestrict = (ageYear === maxValue) && (agemonth > 0 || ageday > 0);
if (!isEligible || isRestrict) {
var message1 = new RegExp(common_Retail.getResourceString(resx.PROSPECT_RETAIL_FORM, "AlertDOB")).toString();
if (customerTypeVal !== null && customerTypeVal !== undefined) {
if (customerTypeVal !== 100080001) {
common_Retail.openAlertDialog("Ok", message1.substr(1, message1.length - 2));
var productConfigRecordsDOB = await Deal_Retail.GetProductConfigRecords(ProductConfiguration.DOBMandatoryAgePCFVisible, productId);
if (productConfigRecordsDOB !== undefined && productConfigRecordsDOB !== null) {
var dobMandatoryValue = productConfigRecordsDOB.entities[0].new_value;
if (dobMandatoryValue !== null && dobMandatoryValue === "Yes") {
dobFormContext.setValue(null);
}
}
} else {
common_Retail.openAlertDialog("Ok", message1.substr(1, message1.length - 2));
if (eventArgs !== null && eventArgs !== undefined) {
eventArgs.preventDefault();
}
}
}
}
}
}
}
}
}
}
} catch (e) {
common_Retail.openAlertDialog("Ok", e.message);
}
};

Anything doing wrong above functionality is working & throwing alert but after alert its saving the record which it should not save the page anything any idea?

Thanks,

Jharana

  • a33ik Profile Picture
    a33ik 84,325 Most Valuable Professional on at
    RE: eventArgs.preventDefault(); is not working while restricting to page save in crm 365

    I'm sorry but it's a bit complicated to suggest you something without seeing the full code.

  • Jharana Baliyar Singh Profile Picture
    Jharana Baliyar Singh 2,665 on at
    RE: eventArgs.preventDefault(); is not working while restricting to page save in crm 365

    I haveused global variable boolean val false in else part making it true & based on true on save method calling preventDefault.

    if (isExistingdobValidate === true) {

        Deal_Retail.calculateAgeBasedOnDOB(executionContext);

    var eventArgs = executionContext.getEventArgs();

    if (eventArgs !== undefined && eventArgs.getSaveMode() !== undefined && eventArgs.getSaveMode() !== null){

    eventArgs.preventDefault();

        return;

    }

    }

  • Jharana Baliyar Singh Profile Picture
    Jharana Baliyar Singh 2,665 on at
    RE: eventArgs.preventDefault(); is not working while restricting to page save in crm 365

    Hi Andrew thanks for ur response.

    I am calling Deal_Retail.calculateAgeBasedOnDOB in another method like below:

    ///Calling calculateAge async method

    Deal_Retail.callAsyncMethod = function (executionContext) {

       "use strict";

       try {

           var formContext = executionContext.getFormContext();

           Deal_Retail.calculateAgeBasedOnDOB(executionContext);

       } catch (e) {

           common_Retail.openAlertDialog("Ok", e.message);

       }

    };

    Then on save calling Deal_Retail.callAsyncMethod this method

    Deal_Retail.OnSave = function (executionContext) {

    Deal_Retail.callAsyncMethod(executionContext);

    }

    will it get resolve the issue ?

  • Suggested answer
    a33ik Profile Picture
    a33ik 84,325 Most Valuable Professional on at
    RE: eventArgs.preventDefault(); is not working while restricting to page save in crm 365

    Hello,

    The main reason is the async nature of your code. Sync operation is synchronous. In the following article I described on how it's possible to handle such a scenario - butenko.pro/.../

    Good luck.

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,516 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 231,409 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans