Notifications
Announcements
No record found.
Hi, I am relatively green when it comes to JScript. I need to require a field (text) to have a minimum length of 8 characters. Maximum length is already set at the field properties level. If field does not have 8 characters, it needs to display a message. I am getting a script error on change of this field when using the following code. I believe it has something to do with the XRM.Page line:function FieldValidation(context){ var field = Xrm.Page.getAttribute("bf_DOT1").getValue(); if(field.length < 8) { Xrm.Utility.alertDialog("Please enter 8 or more characters in field"); context.getEventArgs().preventDefault(); }}Can someone identify the correction that needs to be made here? I then need to apply this to DOT2, DOT3, etc etc. fields on the form on change.
Thanks!
Hi Chrisbra22,
Check out the below one
function FieldValidation(_executionContext) { try { var formContext = _executionContext.getFormContext(); var field = formContext.getAttribute("bf_dot1").getValue() if (field.length < 8) { Xrm.Navigation.openAlertDialog("Please enter 8 or more characters in field"); _executionContext.getEventArgs().preventDefault(); } } catch (e) { Xrm.Navigation.openErrorDialog("Error: " e.message); } }
If I answer your question then please mark it as verified.
Let me know if I can provide you with more details.
ThanksRegards,
Abdul WahabPower Platform & Customer Engagement Developer/Lead/Solution Architecture/Project ManagerDirect/WhatsApp: 923323281237E-mail: abdulwahabubit@outlook.comSkype: abdul.wahabubitLinkedin: www.linkedin.com/.../
Thanks! It does work, but it throws this error before the alert dialog appears:
that is interesting. Can you please replace Xrm.Navigation.openAlertDialog("Please enter 8 or more characters in field"); with alert("Please enter 8 or more characters in field"); ?
Abdul WahabPower Platform & Customer Engagement Developer/Lead/Solution Architecture/Project ManagerDirect/WhatsApp:+923323281237E-mail: abdulwahabubit@outlook.comSkype: abdul.wahabubitLinkedin: www.linkedin.com/.../
That seemed to improve the performance...It shows the dialog first now, and then the odd error!
Remove it Xrm.Navigation.openErrorDialog("Error: " + e.message); and check
Success! No more error. Thanks.
One other thing...if I want to prevent the record from being saved until there is at least 8 characters in the fields, what can I add to the code?
Remove try and catch also and check.
Just to clarify - are you saying to remove lines 2 and 9 (from your example code) completely?
Use exactly below one
function FieldValidation(_executionContext) { var formContext = _executionContext.getFormContext(); var field = formContext.getAttribute("bf_dot1").getValue() if (field.length < 8) { alert("Please enter 8 or more characters in field"); _executionContext.getEventArgs().preventDefault(); } }
It does not work, it throws this script error:
TypeError: Cannot read properties of undefined (reading 'preventDefault')
at FieldValidation (bridgestonevandev.crm.dynamics.com/.../bf_DOTFieldLengthValidation:7:33)
at y._executeFunctionInternal (bridgestonevandev.crm.dynamics.com/.../app.js
at y.execute (bridgestonevandev.crm.dynamics.com/.../app.js
at bridgestonevandev.crm.dynamics.com/.../app.js
at i (bridgestonevandev.crm.dynamics.com/.../app.js
at ee._executeIndividualEvent (bridgestonevandev.crm.dynamics.com/.../app.js
at ee._executeEventHandler (bridgestonevandev.crm.dynamics.com/.../app.js
at Object.execute (bridgestonevandev.crm.dynamics.com/.../app.js
at N._executeSyncAction (bridgestonevandev.crm.dynamics.com/.../app.js
at N._executeSync (bridgestonevandev.crm.dynamics.com/.../app.js
Error Details:
Event Name: onchange
Function Name: FieldValidation
Web Resource Name: msdyn_/WorkOrderProduct/WorkOrderProduct.Library.js
Solution Name: msdyn_FieldService_patch_update
Publisher Name: microsoftdynamics
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.
As AI tools become more common, we’re introducing a Responsible AI Use…
We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…
These are the community rock stars!
Stay up to date on forum activity by subscribing.
Tom_Gioielli 83 Super User 2025 Season 2
Gerardo RenterÃa Ga... 49 Most Valuable Professional
#ManoVerse 40