In my previous blog, I had
explained about Solution and webresource link Part2.
In this blog, I will try to
focus on Javascript and business rule.
Webresource majorly used in
Form customization and Ribbon customization. We can also use Javascript in HTML
also. Developer can also use Jquery in HTML.
It is not recommended to use
Jquery in Form and Ribbon customization.
Form customization: -
Dynamics CRM provides multiple
events for form customization.
Below is list of events
supported by CRM.
Form
·
OnLoad
The OnLoad event
occurs after the form has loaded. It cannot prevent the window from loading.
Use the OnLoad event to apply logic about how the form should
be displayed, to set properties on fields, and interact with other page
elements.
·
OnSave (event occurs when)
·
Code executes the Xrm.Page.data.entity.save method,
even when there is no changed data to be saved.
·
The user navigates away from the form and there is unsaved data
in the form.
·
With auto-save is enabled, 30 seconds after data has changed and
there is unsaved data in the form.
·
Code executes the Xrm.Page.data.refresh method
passing a true value as the first parameter and there is unsaved data in the
form.
Tabs
·
TabStateChange
The TabStateChange event
occurs when the DisplayState of the tab
changes
due to user interaction or
when the setDisplayState method is
applied in code. Use this event when you wish to change the src property of an
IFRAME within the tab.
Fields
·
OnChange
The OnChange event usually
occurs when the data in a form field has changed and focus is lost.
Business Rule: -
Dynamics CRM 2013 onwards
Business rule was introduced and Microsoft enriching its
capability with each rollup
and new version. Business rules can be created from graphical interface and
dynamics 365 in backend created client script to run configured actions.
Business rule cannot replace need to Javascript but it can help to avoid writing
Javascript for simpler action like setting attribute value in form based on
some conditions. Make sure if you are using both Javascript and Business rule
it should not clash. If you face such situation avoid using business and implement
your all logic in Javascript.
Javascript have all the
capability that we can achieve from Business rule except business rule scope
can be Entity. This means that you do not need the CRM form for the rules to be
executed. It can also be executed when you do updates to a record
programmatically like the concept of Workflows and Plugins. In this aspect,
it behaves like the Plugins and Workflows.
How to create business rule: -
Open your solution
and go to your entity and select Business rule then click on new button.
· You can add image to your button. Supported images size are (16x16 px) and (32x32) px. You also need to add your image as web resource in Dynamics CRM.
It will open Business rule interface.
All screenshots are based on Dynamics 365 it may vary for other versions of
Dynamics CRM.
You can use Business rule for
below action: -
·
Lock/Unlock field
·
Recommendation
·
Show error message
·
Set default value
·
Set field value
·
Set field as business required true or false
·
Set field visibility as true or false.
You can also use “Business
Process” in business rule conditions. I will explain about “Business rule” in my
coming blogs.
Ribbon customization: -
In Dynamics crm top toolbar which
contains all buttons are known as “Ribbon”. In web application, this is all
know as “Command Bar”. When you will
start working with Dynamics CRM you will see ribbon at different place like
main Grid, Sub grid, Form Dashboard, Advance find, Chart, Web resource etc.
All these buttons follow same
concepts as all have below component: -
·
Enable rules: Specifies when a specific ribbon control is enabled
·
Action: Specifies what code executes when a ribbon control is
used
Ribbon customization is based
on XML structure
<RibbonDiffXml>
<CustomActions />
<Templates>
<RibbonTemplates Id="Mscrm.Templates"></RibbonTemplates>
</Templates>
<CommandDefinitions />
<RuleDefinitions>
<TabDisplayRules />
<DisplayRules />
<EnableRules />
</RuleDefinitions>
<LocLabels />
</RibbonDiffXml>
Case Study: Create a custom button to call alert function
of Javascript webresource.
Create a solution and create
new entity named “demo’. Create a Javascript webresource name “demo” in
solution and add function.
function OnbuttonClick()
{
alert("Hi");
}
}
Click on “Publish All
Customization”.
Use “Ribbon Workbench” plugin
of Xrmtoolbox to add buttons in Dynamics CRM Form, grid etc.
Use latest version of “Ribbon
Workbench”.
Add new button named “Demo” in
Account form by drag and drop. Shown as image below.
After adding button add a
command. In your command select your JavaScript web resource. And mention your
function name as shown in below image.
Copy command name and go to
your button and select this command and paste command in “CommandCore” textbox.
After this click on publish button.
Now when you check your account form
after refresh press (ctrl+r) you will see “Demo” button as shown below. When user
click on this button alert message will display.
Now I am sharing other
capability of ribbon customization for your button.
Toolbox: - User can add multiple
type of buttons in Dynamics CRM.
Standard Button.
Standard button is a button in which user can
click on perform action based on command definition.
Flyout Button: -
Flyout button is a container of buttons. You
can add multiple standard button in Flyout button. To add buttons in Flyout you
need to add menu section first in flyout button. If you add any command in
Flyout button it will not work. You need to add command in its drop down
standard buttons.
Split Button: -
A Split Button is a cross between a Button
and a Fly Out Anchor in that it can be clicked, or a sub-item selected from
it's drop down menu.
Group: -
You are allowed to create your own group and add button
to your group.
·
You can add button for Dynamics CRM Web client and tablet also.
You can also add button in Home grid, Sub grid and
Form.
·
With the help of “Ribbon Workbench” you can change out-of-box
button functionality. Right click on out-of-box button and select customize
option. You can also hide button. If you select Hide option it will hide the
button for all users without any condition.
· You can add image to your button. Supported images size are (16x16 px) and (32x32) px. You also need to add your image as web resource in Dynamics CRM.
Commands: -
Commands are the action which run when button
is clicked. Command support two kind of action. You are also allowed to add
multiple action call in a command.
·
URL action
·
JavaScript Action: -
Action also support parameter
for your action call. Below are lists of parameters are supported by action.
·
String
·
Integer
·
Decimal
·
Boolean
·
CRM Parameter.
These parameters are passed as parameter of your JavaScript function.
CRM Parameter is the most
important parameter which you can use to use Dynamics CRM built-in capability.
Below are lists of CRM parameters. These parameters also used in Display and
Enable rule which I will explain further.
Display Rule: -
As name
said Display rule is used to add criteria to show hide button. Below are the
rules that are supported by Display rule. After creation of Display rule, you
need to add this rule to your command.
Most of the time we use “FormStateRule” and “EntityPrivilegeRule”
to show/hide button based on form state and user privilege.
Display rule does not support
custom code or JavaScript. To use JavaScript function, we need to use Enable
rule.
Enable Rule: -
Enable rule
use same as display rule for handle visibility of button. Below are the lists
to supported enable rule. In this list “CustomRule” is most important rule in
which you can use your custom code.