Wednesday, May 30, 2018

Fetch records from Entity having N: N relationship

In Microsoft Dynamics CRM, when there is a many-to-many (N:N) relationship between two entities, an intersect entity is automatically created. This is true for both system relationships built into the product as well as custom many-to-many relationships. The name of the entity is specified in the IntersectEntityName property in the relationship metadata. The name of the relationship is specified in the SchemaNameproperty in the relationship metadata.

Here are few out of the box intersect entities listed in below table for better understanding-

Entity1
Entity2
Intersect Entity Name
Schema Name
account
lead
accountleads
accountleads_association
contact
lead
contactleads
contactleads_association
product
entitlement
entitlementproducts
product_entitlement_association
systemuser
role
systemuserroles
systemuserroles_association
list
account
listmember
listaccount_association

These are just few examples, you can refer the complete list of out of the box intersect entities with below link-


Because it creates an intersect entity for the relationship automatically to fetch the records from the Entity which having N: N relationship is quite different, we would discuss the method in this article.

Let’s take an example of the last records from the above table for out of the box N:N relationship between list and account entity.

The requirement is to fetch all the ‘Marketing List’ which the account with name ‘Bhausaheb Patil’ is associated, below is the code for the same-


        private EntityCollection GetRealtedRecords()
        {
            string entity1 = "account";
            string entity2 = "list";
            string relationshipEntityName = "listmember"; // Provide intersect entity name here
            QueryExpression query = new QueryExpression(entity1);
            query.ColumnSet = new ColumnSet(true);
            LinkEntity linkEntity1 =
  new LinkEntity(entity1, relationshipEntityName, "accountid", "{Entity 1 Primary field}", JoinOperator.Inner);
            LinkEntity linkEntity2 =
  new LinkEntity(relationshipEntityName, entity2, "list", "{Entity 2 Primary field}", JoinOperator.Inner);
            linkEntity1.LinkEntities.Add(linkEntity2);
            query.LinkEntities.Add(linkEntity1);
            linkEntity2.LinkCriteria = new FilterExpression();
            linkEntity2.LinkCriteria.AddCondition(new ConditionExpression("name", ConditionOperator.Equal, "Bhausaheb Patil"));

            EntityCollection result = service.RetrieveMultiple(query);

            return result;

        }        

However, you cannot retrieve the intersect entity records directly by using the QueryExpression class. To retrieve the records in an intersect entity; you must use the FetchExpression class.

Secure Vs Unsecure configuration of plugin in MS CRM

While developing plugin for Microsoft Dynamics CRM, there are some scenarios where you required an input parameters or a configuration for the plugin execution which can be easily updated without having to re-compile or re-registering the plugin.

When you register a plugin step, there are fields where you can specify configuration parameters for the plugin execution.

In Microsoft Dynamics CRM there are two types of different configurations fields are available for plugin-

        1.       Unsecure configuration.
        2.       Secure configuration.

The main difference between these two settings is that, secure configuration is only viewable by CRM Administrators while the unsecure configuration is viewable by any CRM user. 

Another major difference is- the unsecure configuration will automatically move between environments with your CRM solutions.

How to use the plugin step configuration:

While registering the new step in plugin you can provide the configuration data with the Secured or Unsecured fields as show in the below screenshot-




When you provide the configuration details in XML format in the constructor of your plugin class you will get the configuration value which you can use later in the execution method.

Tuesday, May 29, 2018

Show default Lookup window using Typescript in MS CRM


While working with the MS CRM customization, especially when developing custom HTML pages there is always required to show the default or out of the box lookup window and get the selected values in variable as well as pass the previous selected values etc.
We can achieve this using typescript or JavaScript where we have to create a URL with the required parameters for lookupinfo.aspx page.
The basic URL and the parameters details are listed below-

Parameter:
Parameter
Description
Default value
DefaultType
ObjectType Code of the entity

DefaultViewId
Specify the view id with this parameter which you wanted to show with the lookup window

DisableQuickFind
If you want quick find set enable=1 or if not then set disable=0
0
DisableViewPicker
If you want quick view picker enable or not disable

IsInlineMultiLookup


LookupStyle
Lookup Style(single/mutli)

ShowNewButton
if you want new button enable or not disable

ShowPropButton
if you want prop button enable or not disable

dType

1
Mrsh

False
Objecttypes
ObjectType Code of the entity

search
Provide a default search string if you want


Lets go through the code sample of the TypeScript to open the default lookup-


/**
   * The method to Open CRM default lookup for the respective field
   */
  public openLookupDialog() {
    const myObject = new Object();
    let lookupURL: any;

      lookupURL = Globals.BaseURL + '/_controls/lookup/lookupinfo.aspx?LookupStyle=1&browse=0&showpropbutton=1&AllowFilterOff=1&objecttypes=10073';

const dialogwindow = new Mscrm.CrmDialog(Mscrm.CrmUri.create(lookupURL), myObject, 500, 650);
    dialogwindow.setCallbackReference(function (result) {
      if (result != null) {
          this.value = result.items.length > 0 && result.items[0].id != null ?
            result.items[0].id.replace(/[\])}[{(]/g, '') : null;
          this.text = result.items.length > 0 ? result.items[0].name : '';
          this.valueChanged.emit(this.value);
          this.textChanged.emit(this.text);
        this.cd.detectChanges();

      }
    }.bind(this));
    dialogwindow.show();
  }


The above code will generate the URL (I have removed actual server and organization name from the url.) and open the lookup dialog for the specified entity.

http://<server>/<organization>/_controls/lookup/lookupinfo.aspx?AllowFilterOff=1&LookupStyle=single&browse=0&dType=1&objecttypes=10073&showpropbutton=1

There is a method called dialogwindow.setCallbackReference above which will return the selected lookup values and you can store those values in your variables as shown in sample code


MS CRM Entity Types & Ownership



The entities are used to model and manage business data in Microsoft Dynamics CRM. For example, entities such as account, campaign, and incident (case) can be used to track and support sales, marketing, and service activities. An entity has a set of attributes and each attribute represents a data item of a particular type.

The entities are divided into three categories:

  1. System: System entities are used by Microsoft Dynamics to handle all internal processes, such as workflows and asynchronous jobs. You cannot delete or customize system entities.
  2. Business: As a developer working with business data, you will use business; an account, contact, and letter are examples of business entities. Business entities can be customizable.
  3. Custom:  After installation you can add custom entities to Microsoft Dynamics to address specific business needs of the organization.Custom entities also can be customizable.


What is Entity Ownership?

While creating the Entity in MS CRM you have to set the Entity Ownership and there are four types of ownership types available in MS CRM, let’s discuss one by one here:

1.       Organization Owned: Records of Organization Owner entities can be viewed by whole organization, this type of ownership entity records cannot be share or assigned. Organization entities have two access levels- None and Organization.
Article, Article Template, Currency and Web Resource are the type of Organization Entity.

2.       Business Owned: Entities that belong to business unit. Business unit, calendar, team, security role and user are the example of business entity.

3.       User or Team Owned: Assigned to a user or to a team. These entities contain the data that relates to customers, such as accounts or contacts. Security can be defined according to a business unit for the user or team. Because these records are owned by a user or team, they’re connected to a business unit and specific security roles for the business unit. Therefore, these entities participate in role-based security.

4.       None: These entities are not owned by another entity, most of aren’t visible in the solution explorer. These mostly consist of intersect entities created to support Many-to-Many relationships or where access to the record is controlled by a parent record. For example, Opportunity Product records must be accessed through a user or team owned Opportunity record.

Monday, May 7, 2018

Use of String Map Table in MSCRM

String map table basically used in MSCRM for storing the details of Option Set Fields exists in an organization. It contains all the data (Attribute Name, Option Set name, option value , option name, Object Type Code) of option set.

Let’s take one requirement to clear the use of StringMap table, requirement is like:
 “Show the incident by status Name” like: "CAS-T4R4F-U7Y6T5" shown status as "Problem Solved" So in this scenario if we will have no string map table then either you need records which contain all the distinct type of record by status or you need hard code value use in report query.

By String Map table you can join the table and can collect all the status present in incident and can collect records number by grouping of them.
So this type of many scenarios can be fulfill by this table

Result of Case with Status Without using Stringmap Table :

Query : 

SELECT TicketNumber[TicketNumber],statuscode[Status] FROM incident

Result of Case with Status using Stringmap Table :

Query :

SELECT INC.TicketNumber[Case ID],SMT.Value[Status Name], SMT.AttributeValue[Status Value]
FROM incident AS INC
INNER JOIN StringMapBase AS SMT ON INC.StatusCode = SMT.AttributeValue
WHERE SMT.AttributeName='statuscode' AND SMT.ObjectTypeCode=112

QueryExpression vs. FetchXML in MS CRM with C#

Microsoft Dynamics CRM (Customer Relationship Management) is a powerful platform that helps organizations streamline their business processe...