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,...
Answered

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

(1) ShareShare
ReportReport
Posted on by 2,667

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

I have the same question (0)
  • Suggested answer
    a33ik Profile Picture
    84,331 Most Valuable Professional on at

    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.

  • Jharana Baliyar Singh Profile Picture
    2,667 on at

    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 ?

  • Jharana Baliyar Singh Profile Picture
    2,667 on at

    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;

    }

    }

  • a33ik Profile Picture
    84,331 Most Valuable Professional on at

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

  • CU12050317-0 Profile Picture
    2 on at
    After reading this, I felt like I had a real headache… I wish I could handle these complicated errors as quickly as I 'handled' the Earth in Solar Smash!

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 74 Super User 2025 Season 2

#2
Daniyal Khaleel Profile Picture

Daniyal Khaleel 32 Most Valuable Professional

#3
Gerardo Rentería García Profile Picture

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

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans