Tuesday, 15 March 2011

Crm 2011 Blog

Hi everyone,

It has been a while since I posted on this CRM 4 .0 blog. I have now started a new blog for Crm 2011, where I will progressively add new entries:


Friday, 30 January 2009

Relationship names change when importing customizations to a new Crm Install


After reading about this issue on a forum recently I decided to post my experience and workaround for the problem. The problem is that the schema names of relationships change when they are imported to a new Crm install (call it live) from an existing Crm (call it dev). The most common naming problem is that the prefix "new" is added to the relationship name. This causes several issues:

  1. When making changes on dev and then trying to roll it out again you get the following error: "Failure: abc_entity1_abc_entity2: An attribute with the specified name already exists. Please specify a unique name." When you open up the relationship from the customizations section in Crm, you see that the above relationship is now called new_abc_entity1_abc_entity2.
  2. When using code to retrieve records based on their ManyToMany relationships, as stated in the SDK, you need the linking entity name to be this name that has changed to new_abc_entity1_abc_entity2. If this link name changes then your code will also through an error saying that the relationship "abc_entity1_abc_entity2" does not exist.

It has to be said that this naming problem occurs across all the custom relationships imported. I have tracked down the problem to be caused by the order that things get imported into Crm. All entities and relationships get created before the "General system settings" get imported. I can only assume that at this point the new Crm still thinks that it's prefix should be "new" instead of "abc" in the above example.


I have found two ways to combat this, one proactive and one reactive. Please read through it carefully before doing and make sure you understand what happens and the risks involved.

The first way is to import all the customizations of type "Settings" first, and then import the whole file. The "Settings" nodes will be exported when all customizations are exported. So when importing customizations, simply order the grid by type and select all the settings entitie and click "Import Selected Customizations" (it may be worth while to then go into your Crm's system settings and make sure the prefix is now correct). Once this is done, to a full import of the same customization file. I have not tested this thoroughly yet, but the few occasions I tested it, it solved my problem.

The second one is if, after the first solution, there are still some wrong relationship names OR the first step was never implemented. For this you will have to manually change the relationship names in SQL. Please adapt each of the sql statements to reflect your system, and maybe even do it for each individual relationship one at a time to make sure you don't break anything.


Inside SQL management studio, back up your sql database and run the following query to see if the correct values are displayed. If your naming convention was non-standard you may need to adapt some sql statements to suit:

select substring(SchemaName,1,4) as oldPrefix, substring(SchemaName,5,999) as noPrefix, 'abc_'+substring(SchemaName,5,999) as withNewPrefix from MetadataSchema.EntityRelationship where SchemaName like 'new_%'

If the resultset is as expected, run the following which will update the customization relationship names so that customizations can be imported, note that it may be worth testing it on one entity import first by adapting the where clause:

update MetadataSchema.EntityRelationship set SchemaName = 'abc_'+substring(SchemaName,5,999) where SchemaName like 'new_%'

Problem is now that the above breaks all the associated views, but this can be fixed by using the following sql statement, once again, please test it on one entity first because it could have adverse effects:

update MetadataSchema.Relationship set Name = 'abc_'+substring(Name,5,999) where Name like 'new_%'

Thanks and please post any questions or comments so that I can adapt this post accordingly.


Wednesday, 17 December 2008

Complex Queries in Crm 4

Hi All,

I am not normally a big fan of FetchXml. It is messy and since it is often passed in as a string, it can be prone to syntax errors. But over the years, people have been using Fiddler to extract the SOAP headers to the Crm server in order to catch the FetchXml query. There is nothing new here and it is quite handy if you accept the fact that data will also be returned in xml format, meaning it will have to be iterated to get the needed information. Which can really be a pain. I have spent time writing methods to parse the result xml into a dictionary that I can easily access. But even this can be painful at times.

So the other day I discovered a class called FetchXmlToQueryExpressionRequest. This class can be used to convert any fetchXml to a QueryBase object, that can be fed into a RetrieveMultipleRequest. The beauty of this lies in the fact that it is the best of both worlds. You can use advanced find to build complex queries without having to confuse yourself with linked entities etc AND you can then retrieve the data as a BusinessEntityCollection.

Here's some sample code:

string fetch = @"
<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>
<entity name='contact'>
<attribute name='mobilephone'/>
<attribute name='lastname'/>
<attribute name='firstname'/>
<attribute name='emailaddress1'/>
<attribute name='contactid'/>
<order attribute='lastname' descending='false'/>

FetchXmlToQueryExpressionRequest fetchReq = new FetchXmlToQueryExpressionRequest();

FetchReq.FetchXml = fetch;

FetchXmlToQueryExpressionResponse res = (FetchXmlToQueryExpressionResponse)service.Execute(fetchReq);

RetrieveMultipleRequest req = new RetrieveMultipleRequest();

req.Query = res.Query;

req.ReturnDynamicEntities = true;

RetrieveMultipleResponse recordSet = (RetrieveMultipleResponse)service.Execute(req);

Have a nice day
Paul Reyneke

Thursday, 4 December 2008

The specified organization is disabled

I ran into a little problem on my development server today. I was playing round with a windows service that I wrote to run timed events. At one point the Windows service ran into an infinite loop and "End Task" wouldn't kill it, so the only way I could break out of it was to restart ISS. All of a sudden the windows service would no longer start. After a while I tried to log into the crm incarnation of the organization I'm working on and I kept on getting a "The specified organization is disabled" error. This made me panick a bit because I thought I'd corrupted the DB. After more time trying to figure it out, I also tried each of the other organizations (there are 10+ of these) on the crm server and they all had the same problem, "The specified organization is disabled".

Googling the problem kept on coming back to an issue with disabling the default organization form within Deployment Manager. But since I had not done this, I just ignored it's advice and kept on trying to find BD corruption. But after a while I had a look at Deployment manager, and sure enough, the default organization was disabled. This puzzled me a bit, until I remembered that I did infact disable it weeks ago. So I had a look at this link:


It was clear that, by pure bad luck, I had not restarted IIS since the default organization got disabled, which was weeks ago. So, re-enabled the default organization, restarted IIS and everything was working again. But 4 hours was wasted on this problem.

Moral of the story, the most obvious cause/solution is often the most likely.

Friday, 26 September 2008

Plugin throws "The request failed with HTTP status 401: Unauthorized" error

Right, I ran into this a while ago but completely forgot about it until now. After digging around a bit I found that there wasn't much documentation on this outside of the forums. So here goes:

This error occurs the first time the CrmService (or ICrmService) is envolked using a command like "Execute" or "Retrieve". What is meant to happen is that the service should connect to Crm based on it's "Unique Name", but a bug forces it to use the "Fieldly Name" instead. So consider this. You created a new organization on your Crm server called "My Crm Server", this is the friendly name, the Deployment Manager then auto-creates a unique name based on this, but without the spaces "MyCrmServer". Note that you can overwrite the unique name to make it anything else, so long as it doesn't contain non alphanumerics.

It seems to the end user that the organization got created without any problems and everything goes fine.

You write your first plug-in for the organization and deploy it. Your surprised to receive the "The request failed with HTTP status 401: Unauthorized" error and cannot find the cause of this error. Then, after you do a lot of debugging, you realize that it is in fact a Crm bug and not a bug in your code.

To fix this, there are one of two options.

The Workaround
1. From the deployment manager, disable the organisation and rename the friendly name to be identical to the unique name and then re-enable the org.

The Patch
2. Install the patch: http://support.microsoft.com/kb/948746

Hopefully this helps those who, like me, struggled hours to figure this out.


Tuesday, 26 August 2008

Field level security in Crm

There are no supported way to enforce field level security in Crm. What is often done is that the field or tab is hidden on the form. This has a number of weaknesses.
  • Firstly, when you print the form, all the hidden fields will be printed out too. Now you can simply hide the print button too, but this can be overcome in the same way as the next bullet point.
  • Any user with a little bit of javascript knowledge could easily unhide these fields by using a tool such as the IE Dev Toolbar

The second option is to create a child entity that hangs off the entity that the sensitive fields live on and then to move these sensitive fields to this new entity. This will allow you to use the Crm out of the box security on this child entity. This will work fine, but it does mean that each record you create, you'll have to also create a child record, which can be a pain.

The final, and most complicated, way is to do a combination of both the above. Now this is a little hard to explain, so bear with me. Note that for this part I do assume that the user has a reasonable grasp on writing plugins and javascript as well as knowledge of how to do a webservice call to the crmService via javascript.

For this example, I will use the account entity, and assume a new bankaccount field is the sensitive field.

I create the bankaccount field on the account form and show and hide it based on whether the user is in the "Manager" security role (note that determining the security role of a user is covered by other blogs and out of scope for this entry, if anyone needs more information, email me).

Next I create a new entity called bankaccount, and create a 1:N relationship with account. This entity only has one field, "bankaccount". Assign security to this entity so that only users in the "Manager" security role can use it (read, write, create etc) and no one else can see it.

Now, onCreate of an entity, if the bankaccount field is filled in I create a bankaccount record that hangs off the account entity. Also in this plugin I clear the value of the bankaccount field on the account form, so it is essencially empty. Note that you may even put something like "bank account hidden" as the bankaccount in in the plugin.

So now when a user opens this new account and does not have rights to see the bankaccount details, the field is both hidden and empty, so even if they do try and unhide it, it won't show the correct value.

Finally, if a user has rights to see the bank acount, use a crm soap call (also covered on blogs elsewhere, and is similar to retrieving the user role, email me if you need more info) to retrieve the bankaccount from the child record and populate the field accordingly.

Now you could also create an onUpdate plugin to compare the account.bankaccount field with the bankaccount.bankaccount field and update the bankaccount.bankaccount accordingly.

This was rather hard to explain, so feel free to post questions and I'll answer them.



Monday, 18 August 2008

Showing and hiding fields based on another field

Hi All,

This is not a new topic, but it does creep up in the forums from time to time. I thought it may be easier to write a blog entry on the topic.

So here is the scenario, you have a picklist (or any other field) and you want to show and hide other fields based on this. Let's say your picklist field is called "mypicklist" and you have two other fields, one is a text field called "mytextfield" and one is a lookup called "mylookup". The values in the picklist are "Show Text Field", with a value of 1 and "Show Lookup Field" with a value of 2. You want to show/hide the fields accordingly.

Firstly you need to write the following code in the mypicklist onChange event:

if (crmForm.all.mypicklist.DataValue == 1) //show text field
//hide the lookup
crmForm.all.mylookup.style.display = 'none';
crmForm.all.mylookup_c.style.display = 'none'; //note the _c
crmForm.all.mylookup_d.style.display = 'none'; //note the _d, only required for lookups

//show the text field
crmForm.all.mytextfield.style.display = 'inline';

crmForm.all.mytextfield_c.style.display = 'inline'; //note the _c
else if (crmForm.all.mypicklist.DataValue == 2) //show loookup
//hide the lookup
crmForm.all.mylookup.style.display = 'inline';

crmForm.all.mylookup_c.style.display = 'inline'; //note the _c
crmForm.all.mylookup_d.style.display = 'inline'; //note the _d, only required for lookups

//show the text field

crmForm.all.mytextfield.style.display = 'none';
crmForm.all.mytextfield_c.style.display = 'none'; //note the _c
else // hide all
//hide the lookup crmForm.all.mylookup.style.display = 'none';
crmForm.all.mylookup_c.style.display = 'none'; //note the _c
crmForm.all.mylookup_d.style.display = 'none'; //note the _d, only required for lookups

//show the text field
crmForm.all.mytextfield.style.display = 'none';
crmForm.all.mytextfield_c.style.display = 'none'; //note the _c


As you can see, each field has two (three for lookups) sections that has to be hidden, the first section is identified by the field schema name and represents the textbox area. The second, represented by the fieldname following by an "_c" represents the label area. Lookups also has an additional area, replresented by the fieldname followed by an "_d" which represents the lookup button. Each of these sections needs to be shown/hidden individually.

The second part is ensuring these are hidden/shown after they are saved, so the exact same code above needs to be placed in the form load event too. It may also make your life easier to create a global function in the onload event and then call this function from both within the onload event and the picklist onchange event. Something like:

ShowHideField = function()
//all the above code goes in here

// then call the above in both the onload and the relevent onchange event

The above goes for most field types and you can also show or hide fields based on whether they contain data or not with the following:

if (crmForm.all.mypicklist.DataValue != null) //show text field


Hope this helps