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! 

  • Verified answer
    Abdul Wahab Profile Picture
    Abdul Wahab 12,081 Moderator on at
    RE: Need Javascript Review

    Hi Chrisbra22,

    Here you are

    function FieldValidation(executionContext) {
    let isValidate = false;
    try {
    let formContext = executionContext.getFormContext();
    let bf_dot1 = false
    let field = formContext.getAttribute("bf_dot1").getValue();
    if (field.length < 8) {
    formContext.ui.setFormNotification("Please enter 8 or more characters in field DOT #1", "ERROR", "bf_dot1");
    isValidate = true;
    bf_dot1 = true;
    }
    if (bf_dot1) {
    formContext.ui.clearFormNotification("bf_dot1");
    }
    if (isValidate) {
    executionContext.getEventArgs().preventDefault();
    }
    } catch (e) {
    Xrm.Navigation.openAlertDialog("Errors: " + 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
    Chrisbra22 on at
    RE: Need Javascript Review

    OK great, thanks for that.  I have it working for DOT1 field.  But when I add in the rest of the fields it is not working.  No errors though...

    Do you think you could provide a sample in a snippet with the first 2 DOT fields?  I can take it from there!  Here is my code for DOT 1 that is working:

    function FieldValidation(executionContext) {
    let isValidate = false;
    try {
    let formContext = executionContext.getFormContext();
    let field = formContext.getAttribute("bf_dot1").getValue();
    if (field.length < 8) {
    formContext.ui.setFormNotification("Please enter 8 or more characters in field DOT #1", "ERROR", "bf_dot1");
    isValidate = true;
    }
    else{
    formContext.ui.clearFormNotification("bf_dot1");
    }
    if (isValidate) {
    executionContext.getEventArgs().preventDefault();
    }
    } catch (e) {
    Xrm.Navigation.openAlertDialog("Errors: " + e.message);
    }
    }

    But when I add in the second set for Field 2 (DOT # 2) it throws a Script error - web resource method does not exist:

    function FieldValidation(executionContext) {
    let isValidate = false;
    try {
    let formContext = executionContext.getFormContext();
    let field = formContext.getAttribute("bf_dot1").getValue();
    if (field.length < 8) {
    formContext.ui.setFormNotification("Please enter 8 or more characters in field DOT #1", "ERROR", "bf_dot1");
    isValidate = true;
    }
    else{
    formContext.ui.clearFormNotification("bf_dot1");
    }
    if (isValidate) {
    executionContext.getEventArgs().preventDefault();
    }

    let field = formContext.getAttribute("bf_dot2").getValue();
    if (field.length < 8) {
    formContext.ui.setFormNotification("Please enter 8 or more characters in field DOT #2", "ERROR", "bf_dot2");
    isValidate = true;
    }
    else{
    formContext.ui.clearFormNotification("bf_dot2");
    }
    if (isValidate) {
    executionContext.getEventArgs().preventDefault();
    }

    } catch (e) {
    Xrm.Navigation.openAlertDialog("Errors: " + e.message);
    }
    }

     

  • Verified answer
    Abdul Wahab Profile Picture
    Abdul Wahab 12,081 Moderator on at
    RE: Need Javascript Review

    Hi Chrisbra22,

    Use it to remove: ui.clearFormNotification (Client API reference) in model-driven apps - Power Apps | Microsoft Learn

    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
    Chrisbra22 on at
    RE: Need Javascript Review

    I do notice that the error message does not disappear after the correct number of characters are entered...

    pastedimage1664992342156v1.png

  • Chrisbra22 Profile Picture
    Chrisbra22 on at
    RE: Need Javascript Review

    OK, that worked for DOT #1.  Just need to implement for the rest of the fields.  Thanks!

  • Verified answer
    Abdul Wahab Profile Picture
    Abdul Wahab 12,081 Moderator on at
    RE: Need Javascript Review

    Hi Chrisbra22,

    Here is the new one

    function FieldValidation(executionContext) {
    let isValidate = false;
    try {
    let formContext = executionContext.getFormContext();
    let field = formContext.getAttribute("bf_dot1").getValue();
    if (field.length < 8) {
    formContext.ui.setFormNotification("Please enter 8 or more characters in field DOT #1", "ERROR", "bf_dot1");
    isValidate = true;
    }
    if (isValidate) {
    executionContext.getEventArgs().preventDefault();
    }
    } catch (e) {
    Xrm.Navigation.openAlertDialog("Errors: " + e.message);
    }
    }

    Check it out.

    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
    Chrisbra22 on at
    RE: Need Javascript Review

    Getting this now...

    pastedimage1664991434835v1.png

  • Suggested answer
    Abdul Wahab Profile Picture
    Abdul Wahab 12,081 Moderator on at
    RE: Need Javascript Review

    Hi Chrisbra22,

    Try this one

    function FieldValidation(executionContext) {
    let isValidate = false;
    try {
    let formContext = executionContext.getFormContext();
    let field = formContext.getAttribute("bf_dot1").getValue();
    if (field.length < 8) {
    formContext.ui.setFormNotification("Please enter 8 or more characters in field DOT #1", ERROR, "bf_dot1");
    isValidate = true;
    }
    if (isValidate) {
    executionContext.getEventArgs().preventDefault();
    }
    } catch (e) {
    Xrm.Navigation.openAlertDialog("Errors: " + 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
    Chrisbra22 on at
    RE: Need Javascript Review

    Thank you.  I implemented the first field as below, and got a Web resource does not exist Script error.

    CODE:

    function FieldValidation(executionContext) {

    let isValidate = false;

    try {

    var formContext = executionContext.getFormContext();

    var field = formContext.getAttribute("bf_dot1").getValue();

    if (field.length < 8) {

    formContext.ui.setFormNotification("Please enter 8 or more characters in field DOT #1", ERROR , "bf_dot1");

    isValidate = true;

    }

    ERROR:

    ReferenceError: Web resource method does not exist: FieldValidation

       at y._handleMethodNotExistError (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

       at N.executeAction (bridgestonevandev.crm.dynamics.com/.../app.js

       at t.dispatch (bridgestonevandev.crm.dynamics.com/.../app.js

       at Object.dispatch (bridgestonevandev.crm.dynamics.com/.../app.js

       at dispatch (bridgestonevandev.crm.dynamics.com/.../app.js

       at Object.execute (bridgestonevandev.crm.dynamics.com/.../5.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: bf_DOTFieldLengthValidation

    Solution Name: Active

    Publisher Name: DefaultPublisherbridgestonevan

  • Suggested answer
    Abdul Wahab Profile Picture
    Abdul Wahab 12,081 Moderator on at
    RE: Need Javascript Review

    Hi Chrisbra22,

    Here you are

    function FieldValidation(executionContext) {
    let isValidate = false;
    try {
    var formContext = executionContext.getFormContext();
    var field = formContext.getAttribute("bf_dot1").getValue();
    if (field.length < 8) {
    formContext.ui.setFormNotification("Please enter 8 or more characters in field DOT #1", ERROR , "bf_dot1");
    isValidate = true;
    }

    var field = formContext.getAttribute("bf_dot2").getValue();
    if (field.length < 8) {
    formContext.ui.setFormNotification("Please enter 8 or more characters in field DOT #2", ERROR, "DOT #2");
    isValidate = true;
    }

    var field = formContext.getAttribute("bf_dot3").getValue();
    if (field.length < 8) {
    formContext.ui.setFormNotification("Please enter 8 or more characters in field DOT #3", ERROR, "DOT #3");//Do the same for remainings
    isValidate = true;
    }

    var field = formContext.getAttribute("bf_dot4").getValue();
    if (field.length < 8) {
    alert("Please enter 8 or more characters in field DOT #4");
    isValidate = true;
    }

    var field = formContext.getAttribute("bf_dot5").getValue();
    if (field.length < 8) {
    alert("Please enter 8 or more characters in field DOT #5");
    isValidate = true;
    }

    var field = formContext.getAttribute("bf_dot6").getValue();
    if (field.length < 8) {
    alert("Please enter 8 or more characters in field DOT #6");
    }

    if (isValidate) {
    executionContext.getEventArgs().preventDefault();
    }

    } catch (e) {
    }
    }

    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/.../

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

Announcing Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Vahid Ghafarpour – Community Spotlight

We are excited to recognize Vahid Ghafarpour as our February 2025 Community…

Tip: Become a User Group leader!

Join the ranks of valued community UG leaders

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 292,489 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 231,305 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans