Monday, June 20, 2016

MS Dynamics CRM Plug-ins Exception Handling

Its always have Exception blocks as well as finally block to handle all Exception in your custom Plug-ins. Here is basic snap shot.

MS Dynamics CRM Impersonate a user

Impersonation is used to execute business logic (code) on behalf of another Microsoft Dynamics CRM user to provide a desired feature or service using the appropriate role and object-based security of that impersonated user.

Impersonation involves two different user accounts: one user account (A) is used when executing code to perform some task on behalf of another user (B).

To impersonate a user, set the CallerId property on an instance of OrganizationServiceProxy before calling the service’s Web methods.

1) In General Console Application way




2) with in Plugin or Custom work flow




MS CRM Plugins- When to use Entity/EntityReference

For create and Update we use Entity and for Delete it is EntityReference for checking Target.



For other messages we have to use mostly Entity but we need to think where to use EntityReference. For Associate/Disassociate we have to use EntityReference as well. 

Now the Question is What is Target ? Is it all the time used or is any other Properties are available ?

Ans :- Target is a properties for MessageRequest. it is not same for all type of messages.
 For Assign if you have write code , then you can check


For Assign if you have write code , then you can check

Saturday, June 4, 2016

Email signatures in CRM 2016.

CRM2016 Update 1 has introduced email signatures in CRM2016. It is very important if you are using CRM web clients. In the past, CRM users used to copy paste their signatures into CRM emails or used email templates for signatures.
Now you have an option to create one or more personal email signatures.

1. Go to personal options

2. Navigate to the Email Signatures tab and press New.

3. Enter your email signature and press save.

4. You have an option to set the email signature as the default by clicking on the Set as Default button.

Testing the email signatures

Create a new email activity and your default email signature will appear automatically.

There is also a button available ‘Insert Signature’, which adds the signature manually or replaces the default signature. These buttons are highlighted in yellow in the above screenshot.
Note: The embedded images in the email signature will not appear in the email unless they are hosted on the internet.
Now you have email signatures in CRM2016.

How to Qualify Lead without creating Opportunity/Account/Contact in Microsoft Dynamics CRM


How to prevent MS CRM from creating Opportunity/Account/Contact while qualifying a Lead.




A lot of sales teams are frustrated with how lead conversion works in Microsoft Dynamics CRM.  Starting with CRM 2013, whenever a user clicks the “Qualify” button, an Opportunity is always created.  We were hoping to see a fix in CRM 2016 but, sadly, the process still works the same.

How can an organisation reconfigure Microsoft Dynamics CRM to allow sales to qualify a lead without creating an opportunity?

The qualify process built into Microsoft Dynamics CRM assumes that the lead conversion process always ends either in disqualification, or in the creation of an opportunity.  But most businesses have lead conversion scenarios that don’t end in the creation of an opportunity.  Consider the following:

A new contact from an existing account visits the website and completes a contact us form (perhaps requesting a downloadable resource, without any intent to purchase anything at this time).  Sales wants this lead to be visible on the account, so they would like to convert the lead and create a contact, but without creating an opportunity.

Inside Sales qualifies incoming leads, and then passes them along to Field Sales for ongoing contact.  Oftentimes a lead is qualified, but is not yet in the market to make a purchase.  Field Sales still wants to be able to work this prospect, but they don’t work with leads  (only accounts and contacts). They need to convert a lead into an account and contact, but without creating an opportunity.

There are many more scenarios like the two above. 

How can the sales department fix CRM to work the way that they do, instead of being forced to work the way CRM does?

Sometimes what developers does is that they create a custom button on Lead entity ribbon and Write a plugin code using  QualifyLeadRequest Class where they set what to create and what not (among Account/Contact/Opportunity).

Although it serves the purpose,but we have a better solution where we need not to Qualify Lead using different code by calling a plugin from custom button, instead we tell system what to create and what not.
What we need to do is to register a plugin on QualifyLead message on pre-event on lead and set CreateOpportunity/CreateAccount/CreateContact false.


Here what we got is, we need not to hide system Qualify button and neither we need to create other custom button to qualify lead.


Below is the code snippet where i have prevented Lead from creating Opportunity and Account:

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

public void Execute(IServiceProvider serviceProvider)
        {
            IPluginExecutionContext contextPlugin = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
            IOrganizationServiceFactory factory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
            IOrganizationService service = factory.CreateOrganizationService(contextPlugin.UserId);
            if (contextPlugin.MessageName != "QualifyLead") return;

            OptionSetValue LeadStatecode = new OptionSetValue();
            OptionSetValue LeadStatuscode = new OptionSetValue();
            EntityReference LeadRef = null;
            try
            {
                LeadRef = (EntityReference)contextPlugin.InputParameters["LeadId"];
                Entity Lead = service.Retrieve("lead", LeadRef.Id, new ColumnSet(true));
                if (LeadRef.LogicalName != "lead") { return; }
                LeadStatecode = Lead.GetAttributeValue<OptionSetValue>("statecode");
                LeadStatuscode = Lead.GetAttributeValue<OptionSetValue>("statuscode");
                if (LeadStatecode.Value == 0 && LeadStatuscode.Value == 1)
                {
                    // set to true by default 
                    contextPlugin.InputParameters["CreateOpportunity"] = false;
                    // set to true by default 
                    contextPlugin.InputParameters["CreateAccount"] = false;   
                }
            }
            catch (InvalidPluginExecutionException ex)
            {
                throw new InvalidPluginExecutionException(ex.Message);
            }
            catch (Exception ex)
            {
                throw new InvalidPluginExecutionException(ex.Message);
            }
        }




If we prevent Account/Contact creation this solution works great,but It have a little issue when we prevent it from creating Opportunity.

In this case there is no opportunity Created to which Lead page can redirect to it keps displaying Lead page.

Now if user clicks Qualify again, system throw error that you can't qualify a already qualified Lead.

But, we have a solution for this as well.

What we can do is write a JavaScript code on Form OnSave event which refresh the form:

var entityname = Xrm.Page.data.entity.getEntityName();
var entityid =Xrm.Page.data.entity.getId();
Xrm.Utility.openEntityForm(entityname,entityid);function refresh(Context) {
    var SaveMode, SaveEventVal;

    // Change the Save Event Value as per required Save Event
    SaveEventVal = 16;

    if (Context != null && Context.getEventArgs() != null) {
        SaveMode = Context.getEventArgs().getSaveMode();

        // 16 will pass on Lead Qualify button click
        if (SaveMode == SaveEventVal) {
            setTimeout(function () {
    var entityname = Xrm.Page.data.entity.getEntityName();
    var entityid =Xrm.Page.data.entity.getId();
    Xrm.Utility.openEntityForm(entityname,entityid);

            }, 1);
        }
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Hope it would be helpful.

Get files of last hour in Azure Data Factory

  Case I have a Data Factory pipeline that should run each hour and collect all new files added to the data lake since the last run. What is...