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

Need Javascript Review

(0) ShareShare
ReportReport
Posted on by

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! 

I have the same question (0)
  • Verified answer
    Abdul Wahab Profile Picture
    12,119 Moderator on at

    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.

    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: www.linkedin.com/.../

  • Chrisbra22 Profile Picture
    on at

    Thanks!  It does work, but it throws this error before the alert dialog appears:

    pastedimage1664476089108v1.png

  • Verified answer
    Abdul Wahab Profile Picture
    12,119 Moderator on at

    Hi Chrisbra22,

    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"); ?

    If I answer your question then please mark it as 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: www.linkedin.com/.../

  • Chrisbra22 Profile Picture
    on at

    That seemed to improve the performance...
    It shows the dialog first now, and then the odd error!

    pastedimage1664476675416v1.png

  • Verified answer
    Abdul Wahab Profile Picture
    12,119 Moderator on at

    Hi Chrisbra22,

    Remove it Xrm.Navigation.openErrorDialog("Error: " + e.message); and check

  • Chrisbra22 Profile Picture
    on at

    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?

  • Suggested answer
    Abdul Wahab Profile Picture
    12,119 Moderator on at

    Hi Chrisbra22,

    Remove try and catch also and check.

    If I answer your question then please mark it as 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: www.linkedin.com/.../

  • Chrisbra22 Profile Picture
    on at

    Just to clarify - are you saying to remove lines 2 and 9 (from your example code) completely?

  • Verified answer
    Abdul Wahab Profile Picture
    12,119 Moderator on at

    Hi Chrisbra22,

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

    }

    If I answer your question then please mark it as 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: www.linkedin.com/.../

  • Chrisbra22 Profile Picture
    on at

    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.

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

#2
Gerardo Rentería García Profile Picture

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

#3
#ManoVerse Profile Picture

#ManoVerse 40

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans