Monday, June 1, 2015

Sharing and Unsharing Records in CRM 2011/2013

Hi All, Here I am going to share Account record with a user and then unsharing the account from the user in CRM 2011. Here is the logic to share and unshare records

sharing
 // Create the request object and set the target and principal access
GrantAccessRequest grantRequest = new GrantAccessRequest()
            {
                Target = new EntityReference(Account.EntityLogicalName, accountId),
                PrincipalAccess = new PrincipalAccess()
                {
                    Principal = new EntityReference(SystemUser.EntityLogicalName, userId),
                    AccessMask = actionRights
                }
            };

 // Execute the request.
GrantAccessResponse granted = (GrantAccessResponse)service.Execute(grantRequest);

Unsharing 
 // Create the request object and set the target and revokee.
            RevokeAccessRequest revokeRequest = new RevokeAccessRequest()
            {
                Target = new EntityReference(Account.EntityLogicalName, accountId),
                Revokee = new EntityReference(SystemUser.EntityLogicalName, accountuserId)
            };

// Execute the request.

 RevokeAccessResponse revoked = (RevokeAccessResponse)service.Execute(revokeRequest);

Actions that can be automated in MS CRM

Normally, progressing along the business process depends on user input. As a developer, you can perform the same actions programmatically in form scripts.

Change the process when there are more than one process available for the entity.
Use Xrm.Page.data.process.getEnabledProcesses to retrieve information about enabled processes that the user can choose for the entity. Then useXrm.Page.data.process.setActiveProcess to make one of the enabled processes the active one.

Move to the next stage when all required steps are completed to make it the current active stage.
Use Xrm.Page.data.process.moveNext.

Move to the previous stage and make it the current active stage.
Use Xrm.Page.data.process.movePrevious.

Select a stage to view the status of the steps in the stage.
Use Xrm.Page.data.process.getActivePath to retrieve information about the stages that have been completed, the current active stage, and valid stages available from the current active stage. Examine the steps included in that stage and compare the corresponding form attribute values to determine whether they are completed.

Complete a step
Steps are completed when the corresponding data in the form is entered. You can determine the attribute using the step getAttribute method. This will return the logical name of the attribute. Then use Xrm.Page.getAttribute to retrieve attribute from the Xrm.Page.data.entity.attributes collection and then use the attribute setValue method to set the value.

Detect whether a step is required
Use the step isRequired method to determine if a step is required by the business process flow.

Expand or collapse the business process flow control
Use Xrm.Page.ui.process.setDisplayState.
There are also some things you can do as a developer that a user cannot perform.

Hide the process control
Use Xrm.Page.ui.process.setVisible, you can control whether to display the business process flow control.

Skip to a valid completed stage.
Use Xrm.Page.data.process.setActiveStage to set one of the valid completed stages for the current entity.

Query the process definition including stages not currently visible

Use Xrm.Page.data.process.getActiveProcess to query the definition of the business process flow, including stages that might not be visible because of branching logic in the process.

Events for business process flows
You can interact any event provided by the form with business process flows, but two new events allow you to execute code based on events just for the business process flow control. You can execute code when the active stage of a business process flow changes (OnStageChange event) or when a stage is selected (OnStageSelected event).

Neither of these new events provide a user interface to register your event handlers. You must use methods provided to add or remove handlers for these events in the form OnLoad event.. More information: Business Process Flow control events

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.