Posted on 31. March 2011

“Value cannot be null.Parameter name: attributeName” when running Linq query with join

If you run the following query using the CRM 2011 Linq provider (I'm using Linq pad here):

from u in SystemUserSet
where u.Id == new Guid("f19f4c09-965b-e011-ab82-000c2957f385")
select u
All is well. However, if you add a join:
from u in SystemUserSet
join t in TerritorySet
on u.TerritoryId.Id equals t.TerritoryId.Value
where u.Id == new Guid("f19f4c09-965b-e011-ab82-000c2957f385")
select u

You will get the following exception:

System.ArgumentNullException: Value cannot be null.
Parameter name: attributeName

Looking at the Query Expression created, you will see:

<ConditionExpression>
       <AttributeName i:nil="true" />
        <Operator>Equal</Operator>
        <Values xmlns:d5p1="http://schemas.microsoft.com/2003/10/Serialization/Arrays">
          <d5p1:anyType xmlns:d6p1="http://schemas.microsoft.com/2003/10/Serialization/" i:type="d6p1:guid">f19f4c09-965b-e011-ab82-000c2957f385</d5p1:anyType>
        </Values>
      </ConditionExpression>

The problem seems to do with the way that the Linq provider resolves attribute names on the Id field.

You need to change the query to be (specifying the Id parameter explicitly):

from u in SystemUserSet
join t in TerritorySet
on u.TerritoryId.Id equals t.TerritoryId.Value
where u.SystemUserId == new Guid("f19f4c09-965b-e011-ab82-000c2957f385")
select u
Posted on 16. March 2011

How to change the default Lookup type on ‘Customer’ fields

In CRM 4, this used to be a very popular request:
"Please change the default lookup type on the customer field to contact"
Also…
"Please limit the lookup type on the customer field to only allow selection of contacts"
In CRM 2011, there still doesn't seem to be an easy way of doing this, so we have to revert to 'unsupported' means:
document.getElementById("customerid").setAttribute("lookuptypes", "2");
document.getElementById("customerid").setAttribute("defaulttype", "2");
If I find a more supported way, I'll update this post.