2. The New Client Scripting Method
Introduced in Microsoft Dynamics
CRM 2016 with the release that we
can use make to user experience and
interactive while data entered into the
crm forms.
3. METHOD – GETVALUE
• This method was previously available only for attribute
collection and using it we could get field value after the field
was updated and while the cursor was unfocused from that
field. Now, we can capture value of field as early as when users
start entering value in field. We can use this method as shown
below:
Xrm.Page.getControl(field name).getValue();
4. Where we want the user to enter value as per our specific pattern or regular
expression, we can use code mentioned below:
function OnfieldKeyPress() {
var fieldValue = Xrm.Page.getControl(“field name
here”).getValue().toString().replace(/[^0-9]/g, “”);
Xrm.Page.getAttribute(“field name here”).setValue(fieldValue);
}
This method will work only if we call this on a Keypress event. So let’s
first understand how to call a function on Keypress event for a text field.
Note:
JScript Code : OnLoad Event
5. EVENT -KEYPRESS
The following three Keypress methods are added for 2016 version:
1) addOnKeyPress()
This is used to attach a function with a Keypress event:
Xrm.Page.getControl (fieldname).addOnKeyPress(function name);
If we add this statement in function which is called at the loading of the form, then
OnFieldKeyPress function will be attached with Keypress event of field.
6. 2) removeOnKeyPress()
This is used to remove/detach a function that is attached with a Keypress event:
Xrm.Page.getControl(fieldname).removeOnKeyPress(function
name);
3) fireOnKeyPress()
This is used to call Keypress event handler for text or a number field.
Xrm.Page.getControl(field name).fireOnKeyPress();
7. So addOnKeyPress and removeOnKeyPress basically attach or delete already
attached function with Keypress event while fireOnKeyPress enables Keypress
event for text or number fields.
Example
New key Press Method on telephone number field
First we will setup a function on the new addOnKeyPress event for the main
telephone field.
Xrm.Page.getControl('telephone1').addOnKeyPress(function() { });
8. Then we will retrieve the value from the main telephone field to get the keys that
the user inputted.
var userInput = Xrm.Page.getControl('telephone1').getValue();
Then we will build a regex to replace any of the inputted keys that are not “(“, “)”,
“-“, or a number.
userInput = userInput.replace(/[^d-()]+/g, '');
Lastly, we will replace the main telephone value with the new clean version of the
user’s input
9. Xrm.Page.getAttribute('telephone1').setValue(userInput);
It’s just that easy! Now if a user presses an invalid character in the main
telephone field, it will automatically delete the character so the field will not be
saved with invalid data.
JavaScript Code
SetKeyPressMethodOnTelephoneNumber = function () {
Xrm.Page.getControl(‘telephone1’).addOnKeyPress(function () {
var userInput = Xrm.Page.getControl(‘telephone1’).getValue();
userInput = userInput.replace(/[^d-()]+/g, ”);
Xrm.Page.getAttribute(‘telephone1’).setValue(userInput);
});
}
Call these Methods on Form load Event
Note: