Monday, December 25, 2017

Cancel Workflow using JavaScript and Web API in Microsoft Dynamic CRM

It was just another casual task, when business scenario got me into cancelling a specific waiting workflow in Microsoft Dynamic CRM for some specific entity.

First, I googled it looking around if someone had written something about it. I was getting lot of articles to execute workflows, but I was looking to cancel the waiting workflows. I decided to go-ahead and use SQL to find the Guid of specific workflow instance, so that I can execute my code to try canceling it.

I got surprised when I found multiple records of my workflow, but then on CRM UI it was just one.

Then I took a closer look at the workflow table and try to differentiate them and finally I was able to write a query to filter out the one that I was interested in.

(If you are not seeing multiple, just activate and deactivate a workflow few times and it will generate multiple records of the workflow)

So how to find the Guid of exact workflow that would get triggered based on name only?

Select top 100 categoryname,name,workflowid,statecodename,statuscodename, parentworkflowid,parentworkflowidname 
From FilteredWorkflow
Where name like '%Hold on%'
And category = 0 -- Workflow
And statecode = 1
And parentworkflowid is not null

Rest API request would be like:

&$filter=StateCode/Value eq 1
and ParentWorkflowId/Id nq null
and Name eq \'' + workflowName + "\"

How to find the Async Job Id that get triggered and it’s associated to our workflow?

From the above query, we can find the workflow Id which is responsible for activating the instance of the workflow. That’s why it may have been named as Workflow Activation Id in AsyncOperation Table/Entity.

Now we use workflow id to filter out the instances, which we actually might be interested in.

&$filter=primaryentitytype eq 'invoice'
and operationtype eq 10
and  statecode eq 1
and ( statuscode eq 0 or  statuscode eq 20 or  statuscode eq 10)
and  _regardingobjectid_value eq 638F13B4-4AE9-E711-80DA-6C626DCF4746

and ( _workflowactivationid_value eq 98CA71E7-EEE6-E711-80D9-6C626DCF4746
      or _workflowactivationid_value eq 1AC0E180-39E9-E711-80DA-6C626DCF4746)

Finally, to cancel the workflow, here is a code.

function Cancel_Waiting_Workflows(asyncId)
       var entity = {};
       entity.statecode = 3;
       entity.statuscode = 32;

       var req = new XMLHttpRequest();"PATCH", Xrm.Page.context.getClientUrl() + "/api/data/v8.1/asyncoperations(" + asyncId + ")", false);
       req.setRequestHeader("OData-MaxVersion", "4.0");
       req.setRequestHeader("OData-Version", "4.0");
       req.setRequestHeader("Accept", "application/json");
       req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
       req.setRequestHeader("Prefer", "odata.include-annotations=\"*\"");
       req.onreadystatechange = function() {
              if (this.readyState === 4) {
                     req.onreadystatechange = null;
                     if (this.status === 204) {
                           //Success - No Return Data - Do Something
                     } else {

It was great to explore in Dynamic CRM.

Keep Learning!!!

Sunday, October 15, 2017

Failed to import Business Process 'Lead to Opportunity Sales Process' in Microsoft Dynamic 365 CRM

I am here to explain one problem that I faced, when doing import of a solution in Microsoft Dynamic 356 CRM environment. I got an exception when I tried to import my managed solution from one Dynamic 356 CRM environment to other newly instantiated one.

Before I promote my solution to Production instance, I wanted to make sure that my solution is error free and there is no component dependency, which usually one have faced during import of a solution.

So I decided to make a new test environment and do testing there and if everything is working well, I can take further steps to move it to production environment.

I did some of the changes in existing standard business process flow like removing some of the fields and adding few as my client business requirements. When I exported the solution both in managed and un-managed formed, I did not receive any complaints from Dynamic CRM platform.

When I tried to import the managed solution to my new test environment, the import wizard complains about below listed error.

Failed to import Business Process 'Lead to Opportunity Sales Process' because solution does not include corresponding Business Process entity 'leadtoopportunitysalesprocess'.

Failed to import Business Process 'Opportunity Sales Process' because solution does not include corresponding Business Process entity 'opportunitysalesprocess'.


We need to add corresponding entity to our solution, as we did some changes in Standard out the box business process.
  • These corresponding entities do not appear under standard entity list.
  • They also don’t get added when you may try to add all required components.
  •  In fact, I found them under 1:N relationship of respective entity.

Here is a screen shot of an Opportunity Sales Process as a Related entity which appear in 1:N relationship of Opportunity entity.

Similarly, we can add Lead to opportunity Sales Process entity under 1:N relationship of Lead entity. Once we do this, we can see both entities which were missing in above exception are listed in entities list of solution.

After adding these entities, my import customization went successful.

Friday, February 10, 2017

How to create Business rules in Microsoft Dynamic CRM 2016.

Dynamics CRM 2016 is here and with it comes some exciting new features. Microsoft have update Business rule editor with drag and drop features. Let’s try to think of a scenario and use this new enhanced feature.

Business Scenario :

On quote entity, we have need to display Primary contact based on account selected in Potential Customer. If Potential Customer is not selected then we need to lock the Primary Contact.

Solution :

1) Create a One to many relationship between Contact and Quote.

2) Change the field property to display only related records.

3) Create a business rule to Lock the Primary contact until valid Potential Customer not populated by user.

The editor also show us the tips that we are dragging control to valid area. After attaching the Action Control, we are then supposed to populate its properties.

After locking the primary contact, we then clear the value if any in Primary Contact.

We then unlock primary contact if valid potential customer provided by having condition branch.

We then save and activate the Business Rule. CRM does a validation check and if there are no error it display a “Validation successful” messages.

I hope you find this Microsoft Dynamic CRM feature interesting and ease of use.

Vipin Jaiswal

Wednesday, December 14, 2016

Lookup is slow in Microsoft Dynamic CRM.

Problem Statement :

Lookup is unresponsive and painfully slow in Dynamic CRM.

Objective :

To identify the cause which is making Location lookup to be unresponsive.

Lookup in Dynamic CRM

A lookup represent parent child relationship between two entities.
For an example :
An Account (Organization) can have many contacts associated.
CRM by default have 1-Many relationship between account and a contact.

What SQL Index has to do with Lookup

Purpose of Indexes is to fine tune search operation in a table/entity.
In Dynamic CRM each table/entity have a Primary key which becomes a unique clustered index.
One table can have only one Unique-Clustered Index and can have Many Non-Unique-clustered indexes.

If overall Dynamic CRM is suffering from a performance issues, one may think of Re-building Non-Unique Clustered Indexes.
  • It is recommended to schedule a job which takes care of Re-building indexes from time to time.
  • Re-building of indexes may involve extremely high usage of memory, CPU and other resources so it should be done mostly during non-working hours, to avoid impacting business operation.

One should be able to analyse and see if overall CRM performance is at stake or there are few Lookup’s which may causing a performance problem and need to be taken care off.

Now, let see how internally Lookup types get represented  in SQL.
Diagram is from On-Premise solution but it would be same for On-Line CRM.

A Primary Key is a 32 Bit Guid represented in format like this (E5E154C1-62D0-E311-940C-005056946CA6).

A Lookup which is a primary key from related entities is a same represented in Guid format along with its Primary name (string) attribute, as we can see in diagram.
  • ParentCustomerId                 - Lookup Guid Column
  • ParentCustomerIdName         - Lookup Name (string)

How can we create Indexes in Dynamic CRM

For On-premises deployments of Dynamics CRM we can add SQL indexes as needed directly in SQL.

For CRM Online we are required to open a support ticket and make the request to Microsoft Support.

(We have to do a homework and be sure that the index will solve the issue and not leave database performance unaffected or, worse, slow down your CRM)

Enabling Quick Find Record Limits to “YES”

Navigate to Settings -> Administration -> System Setting page.

Understanding Quick Find in Dynamic CRM

Quick Find allows you to search for a record from a grid view.

Every entity have Quick Find Active view and can be configure from customization page.

When you open the configuration page of Quick Find Active view, we get an option to “Add Find Columns”.

We can tick on attributes which we think are beneficial for a quick search.

Don’t forget to save and Publish your changes.

Thursday, October 6, 2016

Hide a Dashboard in Microsoft Dynamics CRM 2016

Microsoft Dynamic CRM 2016 comes with list default system dashboards. In many cases organization/ clients may not need such a long list of system dashboards appearing on UI.

Default System Dashboard’s should not be deleted from Dynamic CRM. 
If we delete them, there are chances that we may encounter further issues when we try to import any managed solution that reference a deleted dashboard and import will fail as it’s is expecting them.

Now there are two scenario which we need to understand    
1)    Hide a specific dashboard for all users.
2)    Hide a specific dashboards for only few or specific users. 

Hiding a Specific Dashboards for all users.

The best practise says to use security roles to hide them from Crm users.
The steps to hide a system dashboard from any Crm users is

     1)    Open dashboard from default solution from Settings -> Customizations -> Customize the System -> Dashboards   


2)    Click on Enable Security Roles and change radio button from Display to everyone to Display only to selected security roles.

      3)    Unchecked all security role.

This way we can hide specific dashboard from the UI.

Hiding specific dashboards for only few or specific users.

Hiding dashboards from specific user from a security role is not suggested.

We can understand why it’s not suggested by thinking of a situation where users may have same security role and we need to hide dashboards among few of them.

In this scenario we need to create a personal dashboard and then share it with specific users/teams.

Steps to make dashboard visible to specific list of users.

     1)    Create a personal dashboard by clicking NEW on dashboard home page.


2) Select a template accordingly which best suits your requirements.

 3)    Finalize dashboard by configuring various components on dashboards accordingly. 
          Save and close the dashboard.
    4)    Open your dashboard and you would be available with SHARE DASHBOARD option.

  5)    Open Share user dashboard and Choose the users or team you want to share the user dashboard with, 
       and give them specific permissions.

That’s it, only user with given permission would then be allowed to make a use of dashboard.

Happy CRM Learning.