Skip to main content

Notifications

Announcements

No record found.

Service | Customer Service, Contact Center, Fie...
Suggested answer

Javascript code (On save event) does NOT prevent save when custom validation fails

Posted on by 2
Hello,
 
I want to restrict save the Work Order record when it does not have related Work Report records(count=0), using javascript.
The executionContext.getEventArgs().preventDefault() is not working inside the success function, but works well outside. My code is below, appreciate any help
 
async function wrmandatory(executionContext)
{
var formContext = executionContext.getFormContext();
var sysstatus=formContext.getAttribute(/msdyn_systemstatus/);
var sysstatusvalue=Xrm.Page.data.entity.attributes.get(/msdyn_systemstatus/).getText();
var error='N';
// get WO GUID
var workorderId = formContext.data.entity.getId().replace('{', '').replace('}', '');
//get WR records
if (sysstatusvalue==='In Progress') {
var fetchXml = /?fetchXml=<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='true' > <entity name='vel_workreport'> <attribute name='vel_workreportid' /> <filter> <condition attribute='vel_workorder' operator='eq' value='/ + workorderId +/'/> </filter> </entity></fetch>/;
await Xrm.WebApi.retrieveMultipleRecords(/vel_workreport/, fetchXml).then(
function success(result) {
var wr_count = result.entities.length;
alert(/wr_count variable is / + wr_count);
//check system status and wr count
if (wr_count ===0 )
{
alert(/ There are no Work Reports. Please enter Work Reports for the job before closing/)
executionContext.getEventArgs().preventDefault();
}
},
function (error) {
alert(/error/);
}
);
}
}
}
  • Suggested answer
    Abdul Wahab Profile Picture
    Abdul Wahab 12,070 Super User 2024 Season 1 on at
    Javascript code (On save event) does NOT prevent save when custom validation fails
    Hi
     
    Did you follow the below links?
    1. preventDefault (Client API reference) in model-driven apps - Power Apps | Microsoft Learn
    2. getSaveMode (Client API reference) in model-driven apps - Power Apps | Microsoft Learn
    3. getEventArgs (Client API reference) in model-driven apps - Power Apps | Microsoft Learn
    If still you are unable to do so. You can use pre validation stage of plugin.
     
    If my reply provided you with a solution, pleased mark it as a verified.
    Let me know if I can provide you with more details.
     
    Thanks
    Regards,
    Abdul Wahab

    Power Platform & Customer Engagement Developer/Lead/Solution Architecture/Project Manager
    Direct/WhatsApp:+923323281237
    E-mail: abdulwahabubit@outlook.com
    Skype: abdul.wahabubit
    Linkedin: https://www.linkedin.com/in/abdul-wahab-a5b8b011a/
    Youtube Channel: https://www.youtube.com/channel/UCBjgLqLK_2kU-3bwo4McrTw
    Twitter: https://twitter.com/Abdulwahabubit

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