Category Archives: JavaScript
Custom Field Validation for Website Fields in Dynamics CRM
Dynamics 365 provides functionality to create a text field of type website field where the user can type in the website name. But out of the box, it has no validation to validate if the user is actually putting a web URL or just a text value, this can be achieved using simple JavaScript. In this blog, let’s see how to put a validation on a website field in CRM so that users enter the correct data. I have created a website field in CRM and here is how it looks. Using the below javascript code you can put a validation on this website field. Code: validateWebsiteURL: function (formContext, fieldName) { if (formContext.getAttribute(fieldName)) { var websiteurl = formContext.getAttribute(fieldName).getValue(); if (websiteurl != “”) { var pattern = new RegExp(‘^(https?:\\/\\/)?’ + // protocol ‘((([a-z\\d]([a-z\\d-]*[a-z\\d])*)\\.)+[a-z]{2,}|’ + // domain name ‘((\\d{1,3}\\.){3}\\d{1,3}))’ + // OR ip (v4) address ‘(\\:\\d+)?(\\/[-a-z\\d%_.~+]*)*’ + // port and path ‘(\\?[;&a-z\\d%_.~+=-]*)?’ + // query string ‘(\\#[-a-z\\d_]*)?$’, ‘i’); // fragment locator if (!pattern.test(websiteurl)) { formContext.getControl(fieldName).setNotification(‘Website: Enter a valid Website URL.’); } else { formContext.getControl(fieldName).clearNotification(); } } } } I hope this helps 😉!
Share Story :
Dynamically filter required Fields/Columns from a Record’s BPF and Form and apply Requirement to those fields
Hi Everyone, Let me explain this topic with an example of a scenario that you might encounter. I’ll walk you through how to complete this scenario in a concise manner below. Use Case Let’s say you have a Table (Entity) with or without multiple Business Process Flows (BPFs) that include some required Fields and even on the Form. Even if the required fields are empty and you want to change a field or flag within the form and save the record. We must disable all required Fields on the Form and then re-enable the requirements for those fields. We can do this with some simple JavaScript code. Step 1: Find a trigger point for your JS function to be called.It can be done with a Ribbon Button or by manually changing a Field. Find the code you’ll need below. I’ll be using Ribbon Button to trigger my JS Function. Quick Tip: You cannot get any Attribute Value of a Field residing in BPF directly. You need to get the entire Control of the Field and then call its attribute values. Step 2: Register your JS function onto your Ribbon Workbench or OnChange of any Field on Form. Since I called my function using Ribbon Button, I used “OpportunityForm.executeMain” with Parameters; “CRM Parameter -> Primary Control“ If you’re calling the JS using Field OnChange, then register the function as “OpportunityForm.executeMain” and do pass ‘executionContext‘. In this case, your part of the script will change as below (use this if you use JS on your Form only) OUTPUT This is how all fields will have no requirement on the Form. I took this output before re-enabling the requirement level for the fields. That’s all, I hope this helped you