Friday, March 20, 2015

Scripting tit-bits for Business Process stage field: Dynamics CRM 2013 Javascript

We were facing lot of issues in trying to show hide , set disabled / enable fields or nullify fields which were not present as fields on form in Dynamics CRM 2013 but just were accessible on business process stage on the form. Here are the ways to do the same I found:
Enable/Disable Business Process stage field:
Xrm.Page.getControl(‘header_process_fieldname’).setDisabled(false);  //Enabled
Xrm.Page.getControl(‘header_process_fieldname’).setDisabled(true);  //Disabled
Show/Hide Business Process stage field:
Xrm.Page.getControl(‘header_process_fieldname’).setVisible(true);  //Show
Xrm.Page.getControl(‘header_process_fieldname’).setDisabled(false);  //Hide
Get Value/ Set Value Business Process stage field:
Xrm.Page.getControl(‘header_process_fieldname’).getAttribute().setValue(value);  //Set Value
Xrm.Page.getControl(‘header_process_fieldname’).getAttribute().getValue();  //Get Value
Set and Remove Notification on Business Process stage field:
Xrm.Page.getControl(‘header_process_fieldname’).setNotification(“Notificationmessage”);  //Set Notification message
Xrm.Page.getControl(‘header_process_fieldname’).clearNotification(); // Clear Notification message
Set Required levels on Business Process stage field:
(Xrm.Page.getControl(‘header_process_fieldname).getAttribute()).setRequiredLevel(‘required’); //Required level
(Xrm.Page.getControl(‘header_process_fieldname).getAttribute()).setRequiredLevel(‘none’); //none level
(Xrm.Page.getControl(‘header_process_fieldname).getAttribute()).setRequiredLevel(‘recommended’); //Recommended level

Note: I have noticed this code will work when current stage field in business process on form is visible on UI.

Change color for text fields :Dynamics CRM 2013

Sometimes we get requirement to change the forecolor for some fields in Dynamics CRM 2013 in order to highlight them in different color. So we implemented following script:
You can add the scripts on-load or based on some condition.
For changing color during field is non-selected:
//Setting red color
$(‘#fieldname’).find(‘div[class="ms-crm-Inline-Value"]‘).css(‘color’,color);

In order to change color during field is typed:
//Setting color blue during typing of field
$(‘#fieldname_i’).css(‘color’, color);

Note: This is unsupported script and may break in future if Microsoft changes something.

Get/Set currency field values in JavaScript for Dynamics CRM

I noticed a scenario where currency field value on CRM forms were not accessible using Xrm.Page.getAttribute(“Currency field name”).getValue() and 
Xrm.Page.getAttribute(“Currency field name”).setValue(Value)
The resolution is in such cases you can access currency fields in below manner:
// To get currency field
Xrm.Page.data.entity.attributes.get(“Currency field name”).getValue();
// To set currency field
Xrm.Page.data.entity.attributes.get(“Currency field name”).setValue(Value);

JavaScript Dependency Checker for Dynamics CRM 2011/2013

JavaScript Dependency Checker for Dynamics CRM 2011/Dynamics CRM 2013 is utility for all CRM administrators, consultants who have to deal with JavaScript issues on CRM 2011/CRM 2013 and want a quick way to document scripts. It is also very useful during product upgrades. 

Thank you to all

Thank you to all who have been reading my blog articles and have commented and sent personal messages. 

I started again posting in the blog after a long break!!! 

It means a lot to me to know some of my content might be helping.