MS Dynamics CRM 365 Control Warning Notification
In MS Dynamics CRM 365 we can add a warning or error notification on a certain control, and from the notification we can provide for the user the ability to do some action based on his/her confirmation.
Let's do this in a scenario, check below,
Scenario:
Requirement: In the Account Entity, in the Account Name Field, if the user entered a name in a lower case, or a name contains a first small case letter and any of the rest letters in upper case, it should notify the user if he/she wants to rewrite the name in this form(first letter upper case and the rest in lower case), if the user clicks apply the name will be rewritten, if the user clicks dismiss the name will remain the same.
Solution:
- The First thing we should create a JavaScript Library and put the code in it, below is my JavaScript Code,
Array.prototype.reWrite = function(){
for (var i = 0, len = this.length; i < len; i += 1)
{
this[i] = this[i][0].toUpperCase() + this[i].slice(1).toLowerCase();
}
return this;
}
function addRecommendationNotification() {
var myControl = Xrm.Page.getControl('name');
var accountName = Xrm.Page.data.entity.attributes.get('name');
var accountNameValue = accountName.getValue();
//if the account name is null then return
if (accountName.getValue() == null) {
return;
}
var isDirty = false;
var accN = accountNameValue.split(' ');
for(var i =0;i < accN.length;i++){
if(accN[i][0].toUpperCase() != accN[i][0])
isDirty = true;
}
if (isDirty == true) {
var actionCollection = {
message: 'Rewrite the name? First letter capital and the rest small case?',
actions: null
};
actionCollection.actions = [function () {
accN.reWrite();
var NewName = accN[0];
if(accN.length > 1){
NewName+=" ";
for(var i = 1;i<accN.length;i++){
NewName += accN[i];
NewName+=" ";
}
}
accountName.setValue(NewName);
myControl.clearNotification('9999');
}];
myControl.addNotification({
messages: ['Account Name is not written well'],
notificationLevel: 'RECOMMENDATION',
uniqueId: '9999',
actions: [actionCollection]
});
}
}
2. Then add the library to the Account form, from the form properties add the library, then on change event of the Account Name field and the 'addRecommendationNotification' function.
Output:
When the user enter a name, a notification will appear to the user.
if the user clicks on the notification sign, a help box will be displayed, asks the user if he/she wants to apply an action.
Then if the user clicks Apply, the account name will be rewritten.
Comments
Post a Comment