Wednesday, September 11, 2019

Cautiously changing logic of existing workflow in Dynamic CRM


How to deal with Old workflow instances that are in waiting state with old Logic?

We can accept that we are blessed with the Wait Condition in Dynamic CRM. It could be used in many simple to complex tasks such as to send a friendly reminders/email after certain duration.

There could be many workflow sessions running in a waiting state considering a typical Live (Production) environment.

Re-Defining Workflow

Now imagine for some reason your business logic got changed and you need to redefine the logic in a workflow.

Newly added logic for a respective workflow will not be applicable for workflows instances which are already in a waiting or running state and your business may complaint that notifications to customer are still going on even though you have already roll-out new changes to the workflow.

Solution

To overcome such a problem, I did follow below steps.
Step 1) Get workflow sessions which are still going ON (WAITING STATE) with OLD LOGIC
Step 2) Cancel Old workflow session with OLD LOGIC
Step 3) Trigger New Workflow Session for those records whose process got cancelled.

.Net Code for above outline steps 

public class SwitchToNewWorkflowProcess
{
IOrganizationService _orgService;

const string regardingEntityType = "invoice";
Guid workflowId = new Guid("1E99FA9F-3708-4107-A110-38191CC2BE59");     //  (Email) License has Expired


public SwitchToNewWorkflowProcess(IOrganizationService _orgService)
{
    this._orgService = _orgService;
}

public void executionPlan()
{
    // Step 1) Get workflow sessions which are still going ON (WAITING STATE) with OLD LOGIC
    EntityCollection oldWorkflowsCollection = getOldWorkflows();

    foreach (Entity sessions in oldWorkflowsCollection.Entities)
    {
        if (sessions.Contains("regardingobjectid"))
        {
            Guid invoiceId = ((EntityReference)sessions["regardingobjectid"]).Id;
                           
            //Step 2) Cancel workflow with OLD LOGIC
            cancelOldWorkflow(sessions.Id);

            // Step 3) Trigger New Workflow Session for those records whose process got cancelled.
            instantiateNewWorkflow(invoiceId);                 
        }
        else
            Console.WriteLine("Process Session where Regarding is not Set : " + sessions.Id.ToString());
    }
    Console.WriteLine("End of Execution Plan");
}


// You need to change condition clauses in below fetch XML to get specific workflow sessions
private EntityCollection getOldWorkflows()
{
    string fetchXmlOldSystemJob =            

    EntityCollection oldWorkflowsCollection = _orgService.RetrieveMultiple(new FetchExpression(fetchXmlOldSystemJob));
    return oldWorkflowsCollection;
}

private void cancelOldWorkflow(Guid workflowInstanceId)
{
    Entity operation = new Entity("asyncoperation")
    {
        Id = workflowInstanceId    //
    };

    operation["statecode"] = new OptionSetValue(3);
    operation["statuscode"] = new OptionSetValue(32);

    _orgService.Update(operation);
}     

private void instantiateNewWorkflow(Guid entityId)
{
    var executeWorkflowRequest = new ExecuteWorkflowRequest()
    {
        WorkflowId = workflowId,      // Guid of workflow 
        EntityId = entityId           // Guid of record 
    };
    var executeWorkflowResponse = (ExecuteWorkflowResponse)_orgService.Execute(executeWorkflowRequest);
}
}

From you main method just invoke the executionPlan() method.

IOrganizationService _orgService;
_orgService = getOrgService();

SwitchToNewWorkflowProcess obj = new SwitchToNewWorkflowProcess(_orgService);
obj.executionPlan();






Unable to initialize the native configuration support external to the web worker process


Unable to initialize the native configuration support external to the web worker process (HRESULT=0x80040154). nativerd.dll must be in %windir%\system32\inetsrv.



I received such an error (during run-time) when I was trying to connect to Dynamic CRM using CrmServiceClient.

This error is not related to Dynamic CRM, instead it is something to do with our machine configuration, I look over the internet and find the below solution.

Open Program and Features from Control Panel and navigate to windows feature turn on/off.

Here you need to enable (Tick) HTTP Activation for your respective .Net Framework either 6/7/8.



Once enabled, try to re-run your .Net Code and it should be able to connect to dynamic CRM without any problem.


Tuesday, September 3, 2019

Dynamic CRM data migration tool


Recently, one of my clients wanted to move from one tenant to another tenant. There was a version dependency of dynamic CRM. Microsoft denied copying CRM DB from one to another and quickly start using the new dynamic CRM on a new domain as it is not possible due o version dependency.

So, as an implementation partner we used Microsoft Dynamics CRM Configuration Migration Utility to do manual migration.

Here are some steps to showcase how to use and migrate records using CRM Configuration Migration Utility.




You can either choose a solution where you have all the entities you wish to migrate.


Or you can select specific entities and selected attributes as well.




Schema denote the various selected entities and respective field if any and further we are suppose to save the data file which contains the actual data records in the CRM system.




Schema denote the various selected entities and respective field if any and further we are suppose to save the data file which contains the actual data records in the CRM system.