Monday, November 24, 2014

InitializeFromRequest in CRM2013

InitializeFromRequest may be the least used request in the Dynamics CRM. It is available since CRM 4.0. I was talking to some of the guys in my team and most of them have never used this request. It initiates an instance of an entity from an existing entity.

It can be used when there is a 1:N relationship between the entities. It is equivalent of creating a child record from the parent form.

For example there is a 1:N relationship between account and contact. if you create a contact from the contact subgrid from the account form, it will prefill the parentcustomer field and all the fields defined in the relationship mappings.

The following screen shot displays the mappings of 1:N relationship between account and contact.
image
The same functionality can be achieved using InitializeFromRequest. This request returns a InitializeFromResponse. The request does not create a new record but the response can be used to create a new record.
Here is the code.
    // Create the request object
     InitializeFromRequest initialize = new InitializeFromRequest();

     // Set the properties of the request object
     initialize.TargetEntityName = "contact";
     

     // Create the EntityMoniker
     initialize.EntityMoniker = new EntityReference("account", new Guid("8A5D8108-DE3B-E311-9401-00155D1B7B00"));
    
     // fields to initialised from parent entity
     initialize.TargetFieldType = TargetFieldType.All;

     // Execute the request
     InitializeFromResponse initialized =
         (InitializeFromResponse)_serviceProxy.Execute(initialize);

     if (initialized.Entity != null)
     {
         //get entity from the response
         Entity entity = initialized.Entity;

         // set the name for the contact
         entity.Attributes.Add("firstname", "John");
         entity.Attributes.Add("lastname", "Smith");

         //create a new contact
         _serviceProxy.Create(entity);

     }
        
Happy coding..

Saturday, August 9, 2014

Setup and Deployment guide for ‘Performance Toolkit’ for Dynamics CRM 2011

Performance Toolkit for Microsoft Dynamics CRM 2011 is available on ‘Microsoft Pinpoint’. But the hyperlink for ‘setup and deployment’ guide is not working. It always displays the following message.
service unavailable 

I have found an old copy of the guide. You can download the guide from here.

Thanks.

Friday, December 6, 2013

“Run Report” button on custom entities forms

In CRM2013 the “Run Report” button is missing on custom entities. The button is not available even if you have selected display in the “Forms of the related record types”. The problem is the “Display Rule” named “Mscrm.HideOnCommandBar” associated with the “Run Report” button. It is a default behaviour for custom entities.

There are 2 different ways to display the “Run Report” button on the custom entities.

By adding the "CommandDefinition” to the ribbon definition of the entity.

  1. Create a new solution in CRM, add contact entity to the solution and export the solution.
  2. Open the customization file from the exported solution and look for <RibbonDiffXML> . It will look like the following XML.
  3. <RibbonDiffXml>
      <CustomActions />
      <Templates>
        <RibbonTemplates Id="Mscrm.Templates"></RibbonTemplates>
      </Templates>
      <CommandDefinitions />
      <RuleDefinitions>
        <TabDisplayRules />
        <DisplayRules />
        <EnableRules />
      </RuleDefinitions>
      <LocLabels />
    </RibbonDiffXml>
    
  4. Replace the highlighted line yellow with the following XML
  5. <CommandDefinition Id="Mscrm.ReportMenu.Form">
        <EnableRules>
          <EnableRule Id="Mscrm.FormStateNotNew" />
        </EnableRules>
        <DisplayRules>
          <DisplayRule Id="Mscrm.ReadReport" />
        </DisplayRules>
        <Actions />
      </CommandDefinition>
    </CommandDefinitions>
    
  6. Save the file and overwrite the Customization file in your exported solution zip file with this file.
  7. Import the solution zip file and publish it. It’s done.

By using  Ribbon Work Bench 2013


Report button can be easily restored using “Ribbon Work Bench 2013”. Check http://community.dynamics.com/crm/b/develop1/archive/2013/09/06/how-to-restore-a-hidden-button-on-the-crm-2013-command-bar.aspx for the step by step instructions.
Note: Make sure in the step 4 of the instructions,  “Form” is selected in the upper right corner as shown in the following screen shot.

image

Thursday, November 7, 2013

Behaviour of the “Add New” button on sub grids in CRM2013

CRM2013 displays the subgrids slightly differently than CRM2011.  The “Add New” button on subgrids behaves in two different ways.

 

Scenario 1

The following screen shot displays the contact grid on the account form.

image 

The “+” icon represents the “Add New Contact” button. When you click on the button, it pops up the search box as shown in the following screen shot.

image

On click of the image  button, the system displays the following pop up. You need to click on “+ New” button at the bottom right corner to open a new contact form.

image

Scenario 2

The following screen display the opportunities grid on the account entity.

image 

On click of the “+” button. the system displays the new opportunity form.

 

Reason for the behaviour

The difference in the behaviour is  because of “Field Requirement“ property of the lookup field.

In I:N relationship, if the “Field Requirement” property of the look field is “Business Required” then “Add New” button will display a new record form. The following screen shot displays “Field Requirement” of “Business Required” for the “Potential Customer” field in opportunity entity.

image

 

And if the “Field Requirement” property of the look field is not “Business Required” then “Add New” button will display search box as discussed in scenario 1. The following screen shot displays “Field Requirement” of “Optional” for the “Company Name” field in contact entity.

image

Thursday, October 24, 2013

addCustomFilter method for lookup control in CRM2013

In CRM2011, Microsoft introduced a new method “addCustomView” to filter the lookup dialogs.  The process was a bit complicated. You have to create a unique guid, fetchxml, layoutxml and few other things.
In the new release, Dynamics team has introduce a new method addCustomFilter. It makes it very easy to apply filters to the lookup dialog. But there is one gotcha.This method can only be called from a “PreSearch” event handler of the lookup control. This event get triggered as the user is about to view results for the lookup.
We can add and remove the event handler to the PreSearch event using addPreSearch(handler) and removePreSearch(handler) methods.
In this blog, I am going to filter the parent customer lookup on the contact entity based on the city of the contact. Here is the code.

Code

function addEventHandler() {
    // add the event handler for PreSearch Event
    Xrm.Page.getControl("parentcustomerid").addPreSearch(addFilter);

}

function addFilter() {
    //check if the city is not empty
    
    if (Xrm.Page.getAttribute("address1_city").getValue() != null) {
        var city = Xrm.Page.getAttribute("address1_city").getValue();
        //create a filter xml
        var filter ="<filter type='and'>" +
                     "<condition attribute='address1_city' operator='eq' value='" + city + "'/>" +
                     "</filter>";
        //add filter
        Xrm.Page.getControl("parentcustomerid").addCustomFilter(filter);

    }
}

I am calling the addEventHandler function on the form load event.
The addEventHandler() function is used to attach the event handler to the PreSearch event of the “parentcustomerid” lookup of contact entity.
The addFilter() function will be called when the PreSearch event is triggered.

Screen Shot


image
In the screen shot above, the lookup is displaying accounts and  contacts where city is equal to ‘Renton’

Things to remember


addCustomFilter method takes two parameters ‘filter’ and the ‘entityLogicalName’. The entityLogicalName is optional and if this parameter is provided, the filter will only apply to that entity type. Otherwise it will apply to all types of entities returned.

For e.g. customer lookup control display account and contact records. If we don’t provide entityLogicalName  parameter, the filter will apply to both account and contact records and if we provide “account” as a  parameter then filter will only be applied to account records not to contact records.
  

Friday, October 18, 2013

Client side notifications in CRM2013- Part 2

This is second part of my last blog Client side notifications in CRM2013- Part 1. This blog will explain how to display notifications on form level.

Important points regarding form level notifications

  • You can display multiple notifications on form.
  • The height of the notification area is limited so If you have too many notification then user may have to scroll down to see the rest of notifications.
  • The new notification is added at the top of notification area.
The following 2 methods are available to display and clear the notification on form.
  1. setFormNotification  - Xrm.Page.ui.setFormNotification(message, level, uniqueId);
  2. There are 3 parameters for this method
    • message- The text of the notification
    • level- it decides the icon infront of the message. It can be an “ERROR” or a “WARNING” or a “INFO”
    • unique- It can be any unique text string. it has to be unique as we use this uniqueId to clear the notification
  3. clearFormNotification  - Xrm.Page.ui.clearFormNotification(uniqueId);
  4. There is only one parameter for this method
    • uniqueId – it is same that is used to set the form notification

For this blog I have created two functions. 
  • formNotification1 – This function displays an error notification if the value of  “noofemplyees” attribute is less than 5.
  • formNotification2– This function displays an warning notification if the value of  “address1_postalcode” attribute is not 2150.

Code

function formNotification1()
{   
     //check if the value is greater than 5
    if (Xrm.Page.getAttribute("numberofemployees").getValue() <= 5) {

         //set the notification with an error icon
         Xrm.Page.ui.setFormNotification("Employees number should be more than 5", "ERROR", "noofemployees");
    }
    else
   {
       //clear the notification
       Xrm.Page.ui.clearFormNotification("noofemployees");
  }
  }

function formNotification2()
{   
     //check if the postal code is not 2150
    if (Xrm.Page.getAttribute("address1_postalcode").getValue() != "2150") {

         //set the notification with a warning icon
         Xrm.Page.ui.setFormNotification("Account is not located in Parramatta", "WARNING", "postcode");
    }
    else
   {
       //clear the notification
       Xrm.Page.ui.clearFormNotification("postcode");
  }
  }

Result

image

setNotication VS setFormNotification

setNotification setFormNotification
set notification on the control set notification on the form
can create just one notification per control can create multiple notification on the form
only supports error notifications supports error, warning and info notifcations
can’t save the form without clearing the notification can save the form with the notifications

If you are interested in creating a similar functionality in CRM2011, check one of my old blog How to display a warning message on the CRM form. It looks like Microsoft has copied our idea.

Thursday, October 17, 2013

Client side notifications in CRM2013- Part 1

In CRM2011, there was no out of box functionality to show notifications on the entity forms. We have used alert function of JavaScript to pop up the message boxs. There was no way to show the error messages on the form except using web resources. Microsoft has introduced some new JavaScript methods to achieve this functionality in CRM2013.
In CRM2013,notifications can be displayed on form level and/or on field level. This blog will explain How to use notification next to specific control on the entity form.
We can use the following 2 methods to display and clear the notification next to field/control on the form
  1. setNotification- Xrm.Page.getControl(controlname).setNotification(message) :
            This method will display a message near the control to indicate that data is not valid. When this method is used on Microsoft Dynamics CRM for tablets a red "X" icon appears next to the control. Tapping on the icon will display the message.
     2.  clearNotification- Xrm.Page.getControl(controlname).clearNotification() :
            Remove a message already displayed for a control. We have to remove the notification to save the form.
Here is the JavaScript code I wrote to display the notification if the “No of Employees” entered on account form is less than 5.
function fieldNotification() 
{
    //get the control
    cont = Xrm.Page.getControl("numberofemployees");

    //check if the value is greater than 5
    if (cont.getAttribute().getValue()<=5)
    {
        //set the notification
         cont.setNotification("The number of employees should be more than 5");
    }
    else
   {
       //clear the notification
       cont.clearNotification();
   }
    
}

image
The above screen shot displays the notification next to “No of employees” control.

Note

This method is only available for updated entities. Please check the CRM SDK for the list of updated entities.