0
votes

We are trying to query a collection of custom entities in CRM 2011, using createquery:

var queryActivatedProducts = 
    from c in svcContext.CreateQuery("mur_activatedproducts")
    where ((string)c["mur_activatedproduct"]).Contains("Feature")
    select new
    {
        accountname = c.Attributes["mur_activatedproduct"],
    };

foreach (var c in queryActivatedProducts)
{
    Debug.WriteLine("" + c.accountname);
}

The code above is a modification of an example taken straight from Microsoft: http://msdn.microsoft.com/en-us/library/gg334415.aspx

We receive the following exception:

An exception System.FormatException was thrown while trying to convert input value '%Feature%' to attribute 'mur_activatedproducts.mur_activatedproduct'. Expected type of attribute value: System.Guid. Exception raised: Guid should contain 32 digits with 4 dashes (xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx).

Why is it asking for GUID here? If we follow Microsoft's example, it appears that this is where we want to input the string value of the attribute we are querying. We tried creating a new GUID object with the value of the entity we are searching and placing that here, but this failed as well. Any suggestions are greatly appreciated!

1
What is the datatype in CRM of the field mur_activatedproduct.mur_activatedproduct? I don't think it is a string, I think it is most likely a Lookup - which is why the only valid search criteria is a Guid.Nicknow

1 Answers

0
votes

The exception is due to the datatype of field mur_activatedproduct. Looks like it is a lookup in CRM, so an EntityReference and not a string.

If you want to filter by this field you have to define an EntityReference, and filter by it

EntityReference er = new EntityReference("<entityLogicalName>", new Guid("<recordGuid>")); // your entityreference
var queryActivatedProducts = from c in ctx.CreateQuery("mur_activatedproducts")
                                             where ((EntityReference)c["mur_activatedproduct"]) == er
                                             select new
                                             {
                                                 accountname = c.Attributes["mur_activatedproduct"],
                                             };

If you want to use this filter where ((string)c["mur_activatedproduct"]).Contains("Feature") you have to replace the field mur_activatedproduct by one that is Single Line of Text in CRM.