"тнιѕ вℓσg ¢συℓ∂ ѕανє уσυя мσηєу ιƒ тιмє = мσηєу" - ∂.мαηנαℓу

Thursday, 31 March 2011

How to retrieve the optionset text / value using javascript in CRM 2011 OR How to get the optionset text / value in CRM 2011


Code snippet:

var myOptionSet = Xrm.Page.data.entity.attributes.get("optionsetattributename");

var optionSetValue = myOptionSet.getValue();

var optionSetText = myOptionSet.getText();

How to retrieve lookup text value / id using Javascript in CRM 2011 OR How to get lookup text value / Id in CRM 2011

Related Post: How to retrieve lookup attribute from Plugin in CRM 2011 

 http://crmdm.blogspot.com/2011/06/how-to-retrieve-lookup-attribute-from.html  


Code Snippet:

var lookupObject = Xrm.Page.getAttribute("yourlookupattributename");

    if (lookupObject != null)
  {

        var lookUpObjectValue = lookupObject.getValue();

        if ((lookUpObjectValue != null))
        {

         var lookuptextvalue = lookUpObjectValue[0].name;

         var lookupid = lookUpObjectValue[0].id;

        }

  }

Wednesday, 30 March 2011

How to retrieve Checkbox value in CRM 2011 using Javascript / How to get Checkbox value in CRM 2011 using Javascript

The following code snippet is to retrieve the checkbox value in CRM 2011 using Javascript.

    var myAttribute = "attributename";

    var myControl = Xrm.Page.ui.controls.get(myAttribute);

    var  myCheckboxValue = myControl.getAttribute().getValue();
// returns True or false

    if (myCheckboxValue == true)

    {

//Code- eg: hide or show a control

    }

How to Hide / Show a Tab in CRM 2011 using Javascript OR Toggle visibility of Tab in CRM 2011

Code snippet to show or hide a tab on the entity form in CRM 2011 using javascript.

To show a tab:

Xrm.Page.ui.tabs.get("yourtabname").setVisible(true);


To hide a tab:
Xrm.Page.ui.tabs.get("yourtabname").setVisible(false);

How to Hide / Show a Section in CRM 2011 using Javascript OR Toggle visibility of section in CRM 2011

Code snippet to hide or show a section on the entity form in CRM 2011

To show a section :

Xrm.Page.ui.tabs.get("yourtabname").sections.get("your section name").setVisible(true);


To hide a section:

Xrm.Page.ui.tabs.get("yourtabname").sections.get("your section name").setVisible(false);

Checkbox onChange event is not working properly in CRM 2011 OR Checkbox click Issue in CRM 2011

In CRM 2011, it was found that the checkbox is not working as per our expectation. As we all know, the twooptionset(bit field) attribute type is converted to a checkbox on the form. Now consider a scenario. When you tick or untick the checkbox we would like to execute some code (Say hide a control). But the code written in Onchange is not fired until we leave the checkbox. So if we click outside of checkbox or if we press tab button, then only code changes would reflect. Whereas our expectation is always an immediate change when we tick or untick the checkbox. The following workaround could be applied to the checkbox. This code could be written in any event which is prior to Onchange event of the checkbox. So obviously we should prefer OnLoad event of the form in most of the cases.

crmForm.all.yourattributename.onclick = function ()  {
crmForm.all.yourattributename.FireOnChange();
};

Eg: If you have a checkbox called new_mycheckbox then

In Page Load (OR any event which happens before Onchange of checkobx) write this code

   crmForm.all.new_mycheckbox.onclick = function () {

            crmForm.all.new_mycheckbox.FireOnChange();

        };
and then write the code (hide control or whatever ...) in OnChange event of checkbox

Wednesday, 23 March 2011

How to add a new navigation link / Custom navigation link on the entity form in CRM 2011

  In CRM 2011 its possible to define a new left navigation link on the entity form. The new left navigation could be linked in 2 ways.
1. As an external URL
2. As a webresource ( could be HTML Page, Silverlight page etc)

So let's go through each of these.
As you all know, the left navigation could be enabled by choosing Navigation button on the Entity Form Ribbon as shown below.


Now let's choose the Insert tab and click on the button 'Navigation link'.


        This will result a pop up window as shown below.


There is a provision to choose the Icon as well. However this Icon must be retained in the web resources. So lets try the external URL option first.


Please don't forget to Save and Publish once you complete the above steps. Lets go to see the outcome.


Now we would try the second option. So prior to that we should upload a page to the web resources. Lets upload a sample HTML Page. This page will be linked with the left navigation.


Please note the difference.


Here we go...


The key advantage is that we could define our custom pages, in the sense whatever pages supported in the web resources. And when we click on the left navigation, these pages could be loaded on the entity form as shown.

Related Article : Editing form left navigation in CRM 2011

Tuesday, 22 March 2011

How to enable /disable a custom button on Application ribbon based on the selection of records in CRM 2011 / EnableRules sample on Application ribbon in CRM 2011 / SelectionCountRule sample in CRM 2011

How to place a custom button on Application ribbon in CRM 2011
Reference :http://crmdm.blogspot.com/2011/02/how-to-place-custom-button-on-main-page.html


Lets  consider a scenario where we have a custom button on the Application ribbon and we would like to enable it only when the user selectes minumum 2 records. This requirement could be attained by using <EnableRules> in the customizations.xml.
Lets try by creating a New solution. For detailed steps please refer the first link.
Before exporting add the Application ribbon component to the Solution as shown below.





After this step, export the solution and then extract it to open the customizations.xml.


Lets modify the customizations.xml in such a way that we would place a button and that button will be enabled only when the user selects a minimum of 2 records.


EnableRule could be defined under the ruledefinitions tag as shown.
Here we have chosen one of the enable rules namely, SelectionCountRule. As the name indicates, this enables a button based on the count of the records selected. In this case we have defined the minimum as 2 and maximum as 50. The button will be enabled only if we choose at least 2 records.
 <SelectionCountRule AppliesTo="SelectedEntity" Minimum="2" Maximum="50"></SelectionCountRule>


So when we import the customizations.xml, we would get a disabled button as shown below.

Please note 2 important points. 1. There is a button namely AlfaPeople on the Application ribbon but its disabled 2. There are no records chosen by the user.

So lets create 2 Account records and test the same. As you could see, the button gets enabled when we choose atleast 2 records. If you click on the button it will navigate to our company website.




Code snippet:
<ImportExportXml xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <Entities></Entities>
  <Roles></Roles>
  <Workflows></Workflows>
  <FieldSecurityProfiles></FieldSecurityProfiles>
  <Templates />
  <RibbonDiffXml>
    <CustomActions>
<CustomAction Id="myAction" Sequence="41"
        Location="Mscrm.HomepageGrid.{!EntityLogicalName}.MainTab.Workflow.Controls._children">
       <CommandUIDefinition>
<Button Id="myButton" TemplateAlias="o1" LabelText="AlfaPeople" Command="sample.myCommand">
</Button>
       </CommandUIDefinition>
    </CustomAction></CustomActions>
    <Templates>
      <RibbonTemplates Id="Mscrm.Templates"></RibbonTemplates>
    </Templates>
<CommandDefinitions>
      <CommandDefinition Id="sample.myCommand">
        <EnableRules>
          <EnableRule Id="enableifrecords"></EnableRule>
        </EnableRules><DisplayRules></DisplayRules>
        <Actions>
          <Url Address="http://www.alfapeople.com"></Url>
        </Actions>
      </CommandDefinition>
    </CommandDefinitions>
    <RuleDefinitions>
      <TabDisplayRules />
      <DisplayRules />
      <EnableRules>
        <EnableRule Id="enableifrecords">
          <SelectionCountRule AppliesTo="SelectedEntity" Minimum="2"                    Maximum="50">
          </SelectionCountRule>
        </EnableRule>
      </EnableRules>
    </RuleDefinitions>
    <LocLabels />
  </RibbonDiffXml>
  <EntityMaps />
  <EntityRelationships />
  <OrganizationSettings>
    <customization>
      <isappmode>False</isappmode>
    </customization>
  </OrganizationSettings>
  <optionsets />
  <Languages>
    <Language>1033</Language>
  </Languages>
</ImportExportXml>

 


Monday, 21 March 2011

Plugin Deployment in CRM 2011 - Best Practices

    As you all know, the new concept called 'Solution' which bundles the components was introduced in CRM 2011. Solutions could help us to deploy plug-ins in a very good manner. If you open a solution in CRM 2011 you could find out a section called 'Plug-in Assemblies'.


Now consider a scenario that we have a plug-in called myPlugin with steps Create and Update of Contact Entity. So let's register the plugin using the CRM 2011 Plugin Registration tool.



Once its registered, it would be available in our solution. So navigate to Plug-in Assemblies section of the solution and click on 'Add Existing button' . Then you would get the following window. All registered plugins would be listed here.



We could choose the plugin and then click OK.



So our plugin is added to the solution as shown above. So this is how we bundle the plugin in the solution. However its a very good practice to bundle the steps of plugin too. So lets consider the following 2 steps to be bundled in the solution.
Create Contact  & Update Contact steps


So navigate to 'Sdk Message Processing Steps' section of our solution which could be found just below the 'Plug-in Assemblies' section. And click on add existing option.


Choose the message steps as shown and then click OK.

As you could see the plugin message steps are added to the soultion.


Very Important step: Select the steps and click on Activate Button. Please don't forget this step


The key advantage of this practice is when you move the solution to another server for instance from development server to the production server, you don't need to deploy the plugins again. Because the plugins and steps are already wrapped with solution.
Related article: How to use ILMerge
http://crmdm.blogspot.com/2011/02/crm-cannot-find-plugin-assembly-or-one.html

Wednesday, 16 March 2011

Intellisense in Customizations.xml file - CRM 2011 / Easy way to edit Customizations.xml file in CRM 2011

     As you all know, customization plays a major role in MS Dynamics CRM. Sometimes we may confuse with tags in the Customization file. Hence intellisense is very helpful to speed up the editing process of Customization.xml file. To bring intellisense we just need to follow some simple steps.

Firstly download the latest SDK for MS Dynamics CRM 2011 and extract to a local folder. If you open the SDK folder you could find a folder called Schemas. This folder contains a few XML schemas. If we include these schemas in Visual studio, we will have intellisense for the Customizations.xml file.

So let's do it.
Go to Visual studio and choose XML tab and then select Schemas as shown below.

You could see a XML Schemas window. Click on the add button.




Extension of Schema files is .xsd. Choose customizationssolution.xsd and 3 xsd files which starts with the name ribbon as shown.


Add them to the existing XML Schemas as shown. And the click OK.


Here we go...

Note: Ctrl+ Spacebar - For Intellisense








Please remember to follow the order. For instance as shown above, Inside <CustomActions> tag we have  <CustomAction>. Hence Intellisense will show you<CustomAction> only if you get inside <CustomActions>.

Intellisense in Customizations.xml will save your valuable time.

Related article : How to place a custom button on the application ribbon of MS Dynamics CRM 2011

Tuesday, 15 March 2011

How to retrieve current Locale ID of the User /Organization in CRM 2011 using Javascript / How to retrieve current Language Code / LCID of the User in CRM 2011 using Javascript

In MS Dynamics CRM 2011, Locale ID represents the current Language Pack of the User. It would be the preferred language of the current User.
For Instance, LCID of Italian Language is 1040
LCID of Japanese Language is 1041
Current LCID of the user could be retrieved using Javascript with the following code snippet.
var userlcid= Xrm.Page.context. getUserLcid();


Also LCID of the base language for the current Organization could be retrieved using the following code snippet.

var orglcid= Xrm.Page.context. getOrgLcid();

"Delete event is not fired in CRM 2011" / "Delete event is not working in CRM 2011"

Have you ever faced the above issue? If so lets figure out why its happening? In simple words the Villian is context.InputParameters["Target"

Delete event -> Type of context.InputParameters["Target"]  is EntityReference
Create event / Update event ->Type of context.InputParameters["Target"] is Entity

In a nutshell,
  
context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
if (context.InputParameters.Contains("Target") &&
 context.InputParameters["Target"] is EntityReference)
{
if (context.MessageName == "Delete")
{
 // Code to be executed during Delete event
}
}

Whereas Create / Update events work as follows

context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));


if (context.InputParameters.Contains("Target") &&
 context.InputParameters["Target"] is Entity)
{
  
 if (context.MessageName == "Create")
 {
//Code to be executed during Create event of an entity
}

   if (context.MessageName == "Update”)
{
//Code to be executed during Update event of an entity
}

}


Please note the key difference Entity and EntityReference

How to use a common plugin for Create / Update /Delete events

Its possible to use a common plugin to handle Create / Update / Delete events of an entity.
Code snippet:

context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));

if (context.InputParameters.Contains("Target") &&

context.InputParameters["Target"] is Entity)
{
  
 if (context.MessageName == "Create")
 {
//Code to be executed during Create event of an entity
}

   if (context.MessageName == "Update”)
{
//Code to be executed during Update event of an entity
}

}

MessageName is assigned when we create step by using Plugin registration tool as shown.
Above code shows how to differentiate between the Create and Update events.
Delete event works in a different way. Please refer the following link
http://crmdm.blogspot.com/2011/03/delete-event-is-not-firing-in-crm-2011.html

Monday, 14 March 2011

How to set order for the plugins which share common trigger event in CRM 2011

          When we use plugins in MS Dynamics CRM, sometimes we may need to use more than one plugins with common trigger event. In order to handle this scenario, we have a property in Plugin Registration tool. Its called 'Execution Order'. So the plugins which share same trigger event could be assigned with a Execution order. Execution Order numbering starts from one. Even if all the plugins share the common trigger event, the Plugin with Execution order 1 would be executed first and then the rest. Hence we could decide this order according to our requirement.
For Instance,
Imagine that we have two plugins
1.Plugin : AssignOwner
2.Plugin : Calc
Both of these must be fired when we create a contact and also as Pre-Operational.
But as per our requirement we decide that Plugin 'Assign Owner' should be executed first and then 'Calc'
So it could be done while defining the step.