Archive
D 365 – Set Recommendation on a field using JScript/Business Rule
In this article I am going to detail the steps to set a notification (i.e., Recommendation/Error) to a particular control on the form.
Notifications can be of 2 types; Error or Recommendation.
- If an ‘Error’ notification set, a red “X” icon appears next to the control. Setting an error notification on a control blocks saving the form.
- If a ‘Recommendation’ notification set, an “i” icon appears next to the control. A recommendation notification does not block saving the form.
To explain this better, I am taking below scenario
- If ‘Account Name’ is ‘Microsoft’ recommend to set ‘Ticker Symbol’ to ‘MSFT’
- From the recommendation, if I click on ‘Apply’, set ‘Ticker Symbol’ to ‘MSFT’
Setting Recommendation using ‘Business Rule’:
Create a new ‘Business Rule’ with below components
- Add a “Condition” flow with condition (If ‘Account Name’ = ‘Microsoft’ AND ‘Ticker Symbol’ <> MSFT)
- If condition met, add ‘Recommendation’ action
- Under ‘Recommendation’ action, add sub Action, set ‘Ticker Symbol’ field to ‘MSFT’
Setting Recommendation from JScript:
Register this function on form ‘onload’ and ‘onchange’ of ‘Account Name’ field.
function setRecommendationOnAccountName() {
var ctrlAccountName = Xrm.Page.getControl(‘name’);
var accountName = Xrm.Page.data.entity.attributes.get(‘name’);
var tickerSymbol = Xrm.Page.data.entity.attributes.get(‘tickersymbol’);// Check condition (If ‘Account Name’ = ‘Microsoft’ AND ‘Ticker Symbol’ <> MSFT)
if (accountName.getValue(‘Microsoft’) && tickerSymbol.getValue() != ‘MSFT’) {
var actionCollection = {
message: ‘Set the Ticker Symbol to MSFT?’,
actions: null
};// Add sub Action, set ‘Ticker Symbol’ field to ‘MSFT’ and clear Recommendation
actionCollection.actions = [function () {
tickerSymbol.setValue(‘MSFT’);
ctrlAccountName.clearNotification(‘notify_account_name’);
}];// Set the Notification to ‘Account Name’ control
ctrlAccountName.addNotification({
messages: [‘Set Ticker Symbol’],
notificationLevel: ‘RECOMMENDATION’,
uniqueId: ‘notify_account_name’,
actions: [actionCollection]
});
}
}
Notes:
- notificationLevel : Valid values are either ERROR or RECOMMENDATION. If nothing specified in object definition, it is set to ERROR by default.
🙂
Show custom messages as banner notifications – CRM 2011
In CRM 2011, one way to show custom messages is using Jscript ‘alert’ function.
In one of our requirement we have to display a custom message on CRM form banner like OOB notification.
We can achieve this using Jscript, but it’s an unsupported way and there are no guarantees that this code won’t change in the future.
I am providing 2 scripts which is compatible Pre and Post UR 12.
Post UR 12
// Level – 1 (Critical), Level – 2 (Info), Level – 3 (Warning),
function addNotification(message, level) {
try {
var notificationsList = Sys.Application.findComponent(‘crmNotifications’);
if (notificationsList) {
notificationsList.AddNotification(‘noteId1’, level, ‘namespace’, message);
}
} catch (e) {
}
}
Pre UR 12
// Level – 1 (Critical), Level – 2 (Info), Level – 3 (Warning),
function addNotification(message, level) {
try {
var notificationsArea = document.getElementById(‘crmNotifications’);
if (notificationsArea) {
if (level == 1) { //critical
notificationsArea.AddNotification(‘mep1’, 1, ‘source’, message);
}
if (level == 2) { //Info
notificationsArea.AddNotification(‘mep3’, 3, ‘source’, message);
}
if (level == 3) { //Warning
notificationsArea.AddNotification(‘mep2’, 2, ‘source’, message);
}
if (message == “”) { // Clear the notifications
notificationsArea.SetNotifications(null, null);
}
}
} catch (e) {
}
}
How do I use the method
- To show custom message as Information, pass message and Level =2
addNotification(“This is sample message.”, 2);
Note – CRM 2013, has a new feature to display Notifications which is a supported way.
🙂
Notifications using script – CRM 2013
CRM 2013 has a new feature to display notifications (i.e., Errors,Warnings and Information’s) like a banner message using JavaScript.
We can create both Form level notifications as well as Field notifications.
- Form notifications
- This method display notifications on the top of the record form.
Syntax – Xrm.Page.ui.setFormNotification(<Notification message>,’ ERROR\WARNING\INFORMATION’);
- Field notifications
Syntax – Xrm.Page.getControl(Field).setNotification(<Notification message>);
Observations
- The Save action happens even if you have form level error notifications
- The order of notifications are 1) Information notifications 2) Error notifications and 3) Warning notifications
- The banner only show 3 rows of notification, the user has to scroll down to see the other
- Use
Xrm.Page.ui.clearFormNotification()
to clear the banner notifications
Refer this link for more details
🙂