Tuesday, November 23, 2010

Debug script in Microsoft Dynamics CRM

In Internet Explorer 8 (IE 8 )Microsoft provided a way to debug the scripts.

With Internet Explorer 8 and JScript Libraries, debugging scripts has become much easier. To debug the script, Please follow the step below.
  1. When working with Microsoft Dynamics CRM try to reproduce the conditions where an error is occurring press F12 to open the Internet Explorer developer tools.


  2. On the Script tab, to the right of the Start Debugging button, use the drop-down to locate your JScript library.


  3. Set a breakpoint by clicking on the left margin within your function.

  4. Click Stop Debugging to stop debug.


  5. If your script is in the Onload event, you may need to select the Microsoft Dynamics CRM window and press F5 to reload the window.
For more information, see the MSDN topic Debugging Script with Developer Tools. http://msdn.microsoft.com/en-us/library/dd565625(VS.85).aspx

Hope this helps!

Friday, November 19, 2010

CRM 2011 - Dialogs Explored

Dialogs are the new feature addition to CRM 2011.

Just like we had the ability to extend CRM using plugins and workflows, we now have the option of Dialogs as well.

What is different in Dialog?

Dialog runs synchronously like the Plugins but they have the option of providing user interface to accept custom information required for processing.

I took an example of a situation where, you want to add custom logic that will do the following if the user tries to make an Account inactive.

  1. Check if it has a parent account associated with it, if so let the user know about it

  2. Confirm if they want to de-activate the parent account as well.

  3. If yes go ahead and de-activate it

  4. Check if the account has Child accounts associated with it, if so let the user know

  5. Disable the Child accounts as well if the users wants.

Dialogs let you design a wizard like user interface for user input. In this example we have tried to use all the features that the dialog offers. The aim here is to demonstrate the various features of Dialogs.

Check if it has a parent account associated with it, if so let the user know about it

This can be done through the if condition that was available within workflows as well.

Confirm if they want to de-activate the parent account as well.

Here we shall make you of the Page feature that has been added within dialogs. We shall design a user interface that will ask the user the question and get the Yes/No response back.


For response we have set the type to be Option Set (Radio button). We have defined the values as Yes and No.

This page is then called from the Dialog as the next step


If yes go ahead and de-activate it

We can check the user response to our question in the earlier step and based on that proceed further. Here we have called another Child workflow that is designed to de-activate the account passed to it. The parameter can be passed by selecting the attribute of the Account entity on which the workflow needs to be executed.


Check if the account has Child accounts associated with it, if so let the user know

We need to design a query that will let you search for child accounts.

Here is the trick to designing a parameterized query. When designing the query, user the like condition and specify a dummy name in the query as shown below


There after switch to the Define fetch XML query text tab.


Change the query value to Variable1%. Once you write that, you have the Variable1 showing up on the XML values section. There select the Account Name attribute of the account entity.

This will now search for accounts where parent account = current account.

To see if the query returned results, use the following condition


Disable the Child accounts as well if the users wants

The dialogs do not allow us to loop through the query results and perform action on each of the record found. But it supports calling of Workflows. So the last step is performed by making a call to a workflow assembly.


You can pass the responses received from the user on a page designed in the dialog to the Custom Workflow Assembly.


Dialogs are fun and if put to correct use, it is a very strong tool that has been provided to us.

Wednesday, November 3, 2010

Displaying CRM emails using SSRS 2008

Email body in Dynamics CRM is stored in HTML format and when we try to display the data as is in the SRS report, it shows up with all the HTML tags.
Microsoft Dynamics CRM reports strip the HTML content and display it in Plain text format.
Now with SSRS 2008, you can display them with part of the HTML formatting. It does not support all the HTML tags as yet, but some basic tags are supported that help provide a presentable view of the email body.

Let’s take an example, we will create sample email as shown below.


To show same content in SSRS report 2008 we need to follow the below steps:

  1. Place a textbox control on report and within it place the PlaceHolder by right clicking inside textbox and choose PlaceHolder.

  2. Set the properties of the 'PlaceHolder', change Markup type to HTML.

  3. Extract description(body) of email by query eg. Select description from filteredemail where activityid='A0773451-3DE3-DF11-9EAB-0003FFD4167C'

  4. Set the expression value of the placeholder to description, from dataset fields.( For e.g. Fields!description.Value).

  5. Review the report to see the following result.



Hope this helps...Time to move on to SSRS 2008!

Tuesday, October 26, 2010

Field Level Security in CRM 2011

CRM 2011 will now allow to decide permissions not just at the record level but also “Field Level”. This means that you can decide if a user has the permission to that attribute at the time of Create or Update or simply view the data for that attribute.

Currently, this feature is only available for the custom attributes that you add. They are not available for the system attributes. When you create a new attribute you can enable “Field Level Security” for that attribute.



Once the attribute has been defined for Field Security, it becomes available for defining the security levels for different roles.

Similar to the Security roles, you can now create Field Security Profiles



The profile lists out all the attributes setup for Field Security and you can edit the permission to Read, Update and Create.

Note any security that you apply here is not just available/applicable on the CRM forms, but also programmatically. So if you try to edit a custom attribute of an entity for which you do not have permission to edit, field security will be enforced and you will not be allowed to update the record. You will receive an error.

Field security can also be specified at the time of sharing a record.



You can specify the permission to each user with which the record has been shared and the permission for the secured fields.

Friday, October 22, 2010

Issues with class level declaration in CRM 4 Plugins and Workflows

It is important that you avoid class level declaration and initialization of variables in Plugins and Workflows.

Let us discuss the issues that one can face with Plugins. Plugins as you know is executed synchronously. But if there happen to be multiple calls made to the same plugin assembly and class, a new object for the Plugin is not always created. Instead the same object is reused. So if you have initialized a class level variable at the start and not in the Execute method, it will be initialized only once at the start and there after for subsequent calls the plugin will reuse the last value stored in the variables much like the static variables.

An example of this would be

public class MyPlugIn : IPlugin
   {
      ICrmService _service = null;
      Bool _myBool = false;

public void Execute(IPluginExecutionContext context)
   {
       //Check if particular field contains data then set the _myBool as true
   {
       _myBool = true;
   }
      //Update record with _myBool Field.
   }
   }

_mybool is initialized to be false only once at the start. If for some execution the condition succeeds and the value is set to true. The next call to this plugin would use the _mybool value as true by default not initialize it to false.

This behavior can be replicated using the Bulk Edit feature of Dynamics CRM. If you have a plugin registered for the Update method and you use the Bulk Edit feature to update multiple records at one go, the plugin would fire for each of the records modified but the _mybool variable will only be initialized once.

It would be advisable to ensure that class level variables are initialized in the Execute method each time.

Workflow Issues:

If you happen to declare the ICrmService variable at class level in a workflow assembly and make a call to that assembly in step 1 of the workflow and have step 2 that performs another action in the same workflow, the workflow would go into Waiting and not close with a completed or failed status.

The Workflow error message will display the following error for the Workflow job.

Workflow paused due to error: Unhandled Exception: System.Workflow.Runtime.Hosting.PersistenceException: Type 'Inogic' in Assembly 'Inogic, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' is not marked as serializable. at System.Workflow.Runtime.WorkflowExecutor.Persist(Activity dynamicActivity, Boolean unlock, Boolean needsCompensation) at System.Workflow.Runtime.WorkflowExecutor.System.Workflow.ComponentModel.IWorkflowCoreRuntime.PersistInstanceState(Activity activity) at System.Workflow.ComponentModel.Activity.MarkClosed() at System.Workflow.ComponentModel.Activity.MarkCompleted() at System.Workflow.ComponentModel.ActivityExecutionContext.CloseActivity() at System.Workflow.ComponentModel.ActivityExecutorOperation.Run(IWorkflowCoreRuntime workflowCoreRuntime) at System.Workflow.Runtime.Scheduler.Run() Inner Exception: System.Runtime.Serialization.SerializationException: Type 'Inogic' in Assembly 'Inogic, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' is not marked as serializable. at System.Runtime.Serialization.FormatterServices.InternalGetSerializableMembers(RuntimeType type) at System.Runtime.Serialization.FormatterServices.GetSerializableMembers(Type type, StreamingContext context) at System.Runtime.Serialization.Formatters.Binary.WriteObjectInfo.InitMemberInfo() at System.Runtime.Serialization.Formatters.Binary.WriteObjectInfo.InitSerialize(Object obj, ISurrogateSelector surrogateSelector, StreamingContext context, SerObjectInfoInit serObjectInfoInit, IFormatterConverter converter, ObjectWriter objectWriter) at System.Runtime.Serialization.Formatters.Binary.WriteObjectInfo.Serialize(Object obj, ISurrogateSelector surrogateSelector, StreamingContext context, SerObjectInfoInit serObjectInfoInit, IFormatterConverter converter, ObjectWriter objectWriter) at System.Runtime.Serialization.Formatters.Binary.ObjectWriter.Serialize(Object graph, Header[] inHeaders, __BinaryWriter serWriter, Boolean fCheck) at System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Serialize(Stream serializationStream, Object graph, Header[] headers, Boolean fCheck) at System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Serialize(Stream serializationStream, Object graph) at System.Workflow.ComponentModel.Activity.Save(Stream stream, IFormatter formatter) at System.Workflow.ComponentModel.Activity.Save(Stream stream) at System.Workflow.Runtime.Hosting.WorkflowPersistenceService.GetDefaultSerializedForm(Activity activity) at Microsoft.Crm.Workflow.CrmWorkflowPersistenceService.SaveWorkflowInstanceState(Activity rootActivity, Boolean unlock) at System.Workflow.Runtime.WorkflowExecutor.Persist(Activity dynamicActivity, Boolean unlock, Boolean needsCompensation)

This is again because of the declaration of the CRM service object at the class level. The variable is not released after the completion of job if the variable is defined at class level and hence conflicts with the steps that follow in the same workflow job.

[CrmWorkflowActivity("Customer Search")]
Public partial class CustomerSearch: Activity
{
public static ICrmService crmservice = null; // or make it as function level reference
…… Execute(…….) { }
………..
}

Moving the ICrmService object declaration from Class level to the Execute method ensure that the object is released upon the completion of the job.

protected override ActivityExecutionStatus Execute(ActivityExecutionContext executionContext)
{
ICrmService crmservice = null;
Processing…..
}

hopefully this solution will help you to avoid custom WF error. Any feedback on this blog is commendable.