CRM 2011 Activity Party Add, Remove, Delete from code

Sometimes we want to append the activity party list, update particular item in the existing activity party list, or remove specific activity party from the list.

Below is what I discovered on how to do this. Since we can't use the Create/Update/Delete method from the service context on ActivityParty entity, we need to alter the content of the list before attaching to the Activity Field.

In my case I have an ActivityRecipient custom entity that have N:1 to Email entity. Whenever the user add/update/delete this custom entity, the corresponding 'To' recipients gets updated as well.

I use this function in my pre create/update/delete message of my ActivityRecipient entity.

Note: for some reason Recurring Appointment doesn't like Organization Service Context (early bound), therefore we have to use IOrganizationService with Retrieve Request.

Hope this helps,

CRM 2011 SMS custom activity Characters Remaining field

Paul Nieuwelaar in his blog has implemented some javascript for the characters remaining field.

This saves me a lot of time from reinventing the wheel. All I then did was to set the default value to the field (160 characters for SMS).

Also make sure that you put the 160 limit on the multiline textbox, but leave the minimum value for the charactersremaining field to be -20000000 to avoid the annoying message box.

Hope this helps,

CRM 2011 disable lookup type for customer field or partylist

Sometimes we want to lock the lookup type for customer field type or partylist type. To do this, use the javascript (e.g. Account Type only):

Hope this helps,

CRM 2011 Export Entities Tabs and Sections Name

CRM does not provide a functionality to list all your tab and section names. With the help of SQL XML parsing feature, I managed to create an sql script to read the FormXML of the entities and parse out the tabs and sections name.

This is useful if you want to see all the tabs and sections that you have added. Enough said, this is the script:

Hope this helps,

CRM 2011 Ajax Loading Message Screen with JQuery

When you use ajax request in CRM2011 (for example, the OData call), the user is not notified when this process happens in the background. Although this call is relatively fast, sometimes you can't predict the performance of it and you want some kind of notification that lets the user know what's happening.

Fortunately with the power of JQuery you can easily make this happen. In my case, I have a custom 'Create Opportunity' button on my custom entity form that gets the data from that entity (and related entity) and create opportunity with those data.

When creating the opportunity using the OData way, I will show the loading message popup with the animated OOTB CRM progress gif image.

To make this happen, it's simple. Just put this function into your onLoad form:

You notice the center() method above. This is an add-on to the jQuery method that you can safely put on your jQuery file:

That's it. Now whenever there is an Ajax call on the form, you will get this message shown up.

Hope this helps,

CRM 2011 JQuery OData REST Endpoints Create Record

In CRM 2011, we can create record easily using JQuery and OData. This will get triggered asynchronously using the ajax functionality.

Create Record function from the SDK:

To create new record, you just need to instantiate your object and call the function. The tricky part is when you want to assign Lookup field or OptionSetValue. You can get more info on this from the SDK itself. Here is an example:

CRM 2011 Javascript Fetch XML synchronous or asynchronous call

Using helper function to execute FetchXML request is great.

The code below is retrieved from this blog, and all I had to do is to add extra logic when parsing the XML result because I want to grab the attributes from the related entity as well.

Modified FetchUtil.js:

To use it you just need to do it like this (synchronous)

If it's asynchronous the last bit will be:

The parsed result object can be queried like this:

Hope this helps,

CRM 2011 Import Custom Workflow Error - Assembly must be registered in isolation

This error does not tell much, but I fixed it by adding myself as a Deployment Administrator.

Hope this helps,

CRM 2011 Partial Refresh with jQuery

This post is a little update on my previous post regarding the Form Refresh after Sub-grid changes.

I find the previous solution annoying in the way that when the user click 'Save' or 'Save and New' , the parent form that got refreshed would get the focus. Also, if the sub-grid had paging on it, the form would get refreshed when you move to the next page - thus you will always get the first page of the sub-grid.

In order to avoid this I found a way to do a partial form element refresh using jQuery load function. The only tricky part is that you have to move your subgrid out of the tab that you want to do partial refresh on. (i.e. partial refresh does not work on sub-grid element).

In my case, my fields that I want to update is on General Tab:

hope this helps,

CRM 2011 Custom Workflow Activity - Send Email to Managers

In CRM 2011 (not online), you can create custom workflow activity for things that are not achievable using the OOTB workflow.

In my scenario, I want to send Overdue Case Reminder to group of users (in this case - managers) when a case has reached the Follow Up By date.
If you look at the SystemUser, it has a manager field by default. However, I need to be able to send the email to multiple managers.

My idea is that we create a separate Security Role (e.g. Managers) and put all managers in that role. Then we create a custom workflow activity to get all the users in that role and construct an email to notify them when the case is overdue.

Note: for the CaseLink input, I'm using the CRM 2011 Workflow Utilities to generate the Case Link and put it into the Email Description field.

Hope this helps,

CRM 2011 Get OptionSetValue Label Text

In CRM 2011, when you try to get the value of an OptionSet, you will always get an Integer value instead of the label value.

I found out that you have to use RetrieveAttributeRequest and get the OptionMetadata in order to get the label text of a given value. This is intended so that it supports multiple languages.

Therefore I created a service extension method in order to get the label from the OptionSet:

To use this you just need to pass in the Entity, the OptionSet attribute you're looking for, and the Option Value you want to get the label from:

Example (early bound):

Hope this helps,

CRM 2011 Convert Activity to Case - Populate Case Details

When converting an activity (such as Phone Call) to case, we can only specify the Customer and the Subject on the popup dialog. What if we want to auto-populate the case details with the information from the activity?

At first I thought we can write a plugin that triggers on the post-create of a case. However this is not possible since CRM creates the Case before it sets the Regarding field on the Activity.

The workaround for this is to create the post-update plugin on the Activity itself. This way when the Regarding field is assigned by CRM, it will look for the Case and update its details.

Be careful though because this might not work when converting Activity to Case from Outlook. I read somewhere that when converting Email to Outlook, the Regarding field is set to the Parent Customer of the Case rather than the Case itself.

Hope this helps,

CRM 2011 Custom Filtered Lookup

Sometimes the filtered lookup functionality provided by CRM 2011 doesn't fit your requirement. However Microsoft provides a function for us to add our own custom view for the lookup field.

Example: In a 'Case' form we have customer field. I use this field only for 'Contacts', and I have created a relationship to the 'Account' so that we can see both on the form. I want the Accounts to only show Contacts that have 'Connections To' and vice versa (Only show Contacts that have 'Connections From' if I specified the Account first). If nothing is specified then I want both to show just Active Contacts/Accounts.

The script:

Then you can use it like this:

However there is still a major limitation of FetchXML that you cannot use UNION clause (or something similar). So If you want to show the primarycontact/parentcustomer along with the connections, you're out of luck as you can only do one of them.

Hope this helps,

CRM 2011 Javascript Retrieve Record using jQuery REST Endpoints.

Using jQuery and REST Endpoints is one of the easiest way to get record details in CRM 2011.

To do this you just need to include:
1. jquery-1.6.2.min.js (or just get the latest version)
2. json2.js

The SDK provides some functions for retrieving records:

Use retrieveRecord if you know the ID of the record. Otherwise, use retrieveMultiple if you want to get the record(s) based on some filters as it will return an array of result objects.

In a custom entity form, I have an Account lookup field and an Account Number field. I want to auto-populate one of the field based on the other field and vice versa. Therefore:

Hope this helps,

CRM 2011 Auto-populate Name from Fields or Attributes

Are you tired of thinking about what to put into the name field when creating records and want a way to populate that name automatically?

I have created a service extension method to make my life easier. The goal of this is to autopopulate name field using the format like [field1] - [field2] - [field3] through pre-plugin:

To use this you just need to construct the column set and pass in the entity from the plugin context:

We also need a javascript to disable this name field when the form load:

Hope this helps,

CRM 2011 Refresh Form after Subgrid Changes or Refresh

I found on some articles that to be able to do this you need to attach an OnRefresh event to the element that then calls the function to load the parent window again:

When I deployed to CRM 2011 Rollup 3, I found some issues that throw error like null object (document not ready) and in some IE versions the form keeps refreshing for eternity...

After spending some time investigating this annoying issue, I found out that we have to wait for the subgrid to finish loading its contents for this to work properly. The final javascript is as follows:

Hope this helps,

CRM Report Pre-Filtering with Multiple Datasets

As you know there are two ways of doing pre-filtering with CRM Reports (SQL and not FetchXML). The first one is using the CRMAF_ prefix alias like this:

I soon found limitation using this method. You can't use the same CRMAF_Filtered multiple times because it only filters on the first instance.

Therefore if you have multiple datasets or UNION query that are using that filter more than once, it will not work. To avoid this issue, we can use the Explicit pre-filtering - the second method that CRM provides.

Let's say that you want to have a report that display single Account details and the associated related records information. However rather than building complex single query you want to break it down into multiple datasets.

The Steps:
  1. Create a parameter called @CRM_FilteredAccount. You can set the default value to just:

  2. Build your datasets using this parameter. Use the SQL Dynamic Query so that CRM will replace the parameter with the Advanced Find instance that you specify when running the report.

    These are two of the dataset examples:

Hope this helps,

CRM 2011 Embed Report in IFrame and Pass Parameters from the Form

In CRM you can insert SubGrid and Chart. However this functionality is limited in a way that it can only grab the related entities that have direct relationship to that entity. If we want to get the other related entities from the related entity, it's not possible using the OOTB Subgrid and Chart.

In my case I want to display a chart in an Engagement Plan entity. This Engagement Plan is associated with an Account (N:1). The Account has 1:N relationships with Production History and Contract History entities that have information about commodity and tonnes produced. I want that chart to be shown on the Engagement Plan form. The parameters for this chart will be the Account Id and the Season Id located on the form.

The Steps:
  1. Create a custom SSRS Report to display this chart, taking @CRM_AccountId and @CRM_SeasonId as parameters.
  2. Insert an IFRAME on the form and give dummy Url. Also uncheck 'Restrict Cross Site Scripting' to allow cross scripting between servers.
  3. Create a javascript that is triggered in Form_OnLoad, Account_OnChange, and Season_OnChange:

The method for the form OnLoad is 'Form_onload'
The method for the field Onchange is 'loadChart'

Hope this helps,

CRM 2011 Report Error - rsProcessingAborted

I encountered this error in my project when trying to run CRM reports within CRM (including the default reports). In the reportservice logs it says:

Cannot create a connection to data source 'CRM'. ---> Microsoft.Crm.Reporting.DataExtensionShim.Common.ReportExecutionException: Immediate caller Domain\SqlService has insufficient privilege to run report as user S-1-5-21-1162093662-620106126-315576832-39399.

The CRM is installed on the application server and the SSRS is installed on different server. After futher investigation it turns out that it's caused by double hopping issue regarding the authentication and we just need to set the correct SPN on the machines.

setspn -a http/your-crm-server-name domain\crm-user 
setspn -a http/your-crm-server-name(FQDN) domain\crm-user

Other things to check to make sure your reports work:
1. Make sure your SRS connector has the same RollUp version as the CRM Server RollUp
2. In Reporting Service Configuration Manager, uncheck the 'Specify an Execution Account'
3. Browse to both SharedReports\5.0.xxxx\ and _MSCRM report folder and check the MSCRM_DataSource connection. The connection string should be empty,  DataSource Type should be Microsoft Dynamics CRM SQL, and the connection should be Credentials supplied by the user running the report (uncheck the Windows Credentials box)

To run the report from the report server Url, you might find that if you are using normal domain account for username/password you will get the following error:

An error has occurred during report processing (rsProcessingAborted) Cannot create a connection to data source ‘CRM.’ (rsErrorOpeningConnection) Guid should contain 32 digits with 4 dashes (xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx).

This is because the Username should be 'systemuserid' and the Password should be 'organizationid'. You can get this by running the query against the CRM database:

SELECT fullname, systemuserid, organizationid FROM FilteredSystemUser

Hope this helps,

CRM 2011 Plugin Template

Here is the template to get you started on plugin development in CRM 2011:

To get the service context of your organisation :

Important Notes:

  • You don't need to call SaveChanges because the entity can be updated before it is saved into database.
  • For pre-update, entity will only have attributes that are changed. To get the other attributes that don't change, you can query those attributes using the service context.

  • You need to call SaveChanges because the entity has been saved into database.
  • Entity will have all updated attributes when retrieved using service context.
  • When doing the post update of an entity that update the same entity attributes, loop will occur. To avoid this you can use context.Depth to check the loop count.

  • For delete message, the context.InputParameters["Target"] is an EntityReference instead of Entity.
  • You can check the context message using context.MessageName

Hope this helps,

CRM 2011 Custom Button on Custom Entity to trigger Workflow through JavaScript

I will provide detailed step on what I did to do this. The objective of my example is to have a custom button 'Create Opportunity' on my custom entity that triggers a workflow to create an Opportunity. This way the users don't have to click 'Run Workflow' and choose which workflow they want to run. You can also alter the JavaScript to do other stuff as well.

There are three places where we should put the button mainly:
1. Custom Entity Form

2. Homepage Grid

3. Sub Grid

1. In order to inspect the correct sequence number of where the button will be placed on each section, you can first run the tool provided from the sdk \sdk\samplecode\cs\client\ribbon\exportribbonxml. Just modify the exportribbonxml.cs to only generate the entity you are interested in:

Run the tool program, specify your server connection and it should give you the XML in the output folder with the name Ribbon.xml.

Open the xml, search for the sections and write down the sequence numbers of where you want to put them in. In this case I want to put it after 'Start Dialog' button:

Sequence is 25, 50, 40

2. Export your solution with the custom entity you want. Open the customisations.xml and inside the RibbonDiffXml tag, specify all the contents of the child tags that describe the rules, custom actions, commands, etc. (Look at the SDK for more information). This is the section that I wrote:

3. For the JavaScript that triggers the workflow, there is a post that describes how to do that here and I just make use of that. The CRM Parameter passed from the above is iterated and the workflow is triggered for each of the selection.

Hope this helps :)


CRM 2011 Javascript Functions

Starting to use CRM 2011, here are some useful javascript reference (this list will get longer and longer as time goes):

Hide/Show Field

Hide/Show Field but removing white space

Disable/Not Disable Field

Set Requirement Level

Set Required Level

Validate Number Field and Character Limit

Validate Future DateTime

Example function to hide left navigation item based on a condition

Avoiding the checkbox OnChange bug so that it fires off immediately (put this in OnLoad event of the form)

Set Lookup Value
Hide Picklist Item Text