locked
How to reference the record id in an entity collection retrieved using FetchXML RRS feed

  • Question

  • I'm sure this is really simple but I'm not really a developer. I'm querying CRM using FetchXML and RetrieveMultiple. The querys runs against a custom entity and returns a total grouped by account (which is a lookup in the custom entity). The FetchXML looks like this:

                string fetchquery = @"<fetch distinct='false' mapping='logical' aggregate='true'>
                <entity name='new_customentity'>
                <attribute name='new_usercount' alias='UsercountSum' aggregate='Sum'/>
                <attribute name='new_account' groupby='true' alias='Account'/>
               </entity></fetch>";

    I retrieve the results using:

            EntityCollection fetchquery_result = _service.RetrieveMultiple(new FetchExpression(fetchquery));

    Now, I want to loop through the results and use the total user count for each account to update a field on the account. I can get at the user count using:

     foreach (var c in fetchquery_result.Entities)
                {
                    int usercount = (int)((AliasedValue)c.Attributes["UsercountSum"]).Value;
    }

    But I can't work out how to reference the guid of the account record to use in an Update call. I've tried:

    Guid thisaccountid = (Guid)((AliasedValue)c.Attributes["Account"]).Value;

    but that returns an error, Specified cast is not valid. When casting from a number, the value must be a number less than infinity.

    Any insight here would be highly appreciated.

    Friday, May 3, 2013 8:04 AM
    Moderator

Answers

  • Yes that helps. It's coming back as an EntityReference, rather than a Guid. Try:

    object oVal = ((AliasedValue)c.Attributes["Account"]).Value;
    Guid thisaccountid = ((EntityReference) oVal).Id;


    Microsoft CRM MVP - http://mscrmuk.blogspot.com/ http://www.excitation.co.uk

    Friday, May 3, 2013 9:35 AM
    Moderator

All replies

  • Hi,

    Have u tried

    Guid thisaccountid = (Guid)((AliasedValue)c.Attributes["Account"]).Id; ??

    Friday, May 3, 2013 8:26 AM
  • Thanks for the quick response. Yes I did, Intellisense shows an error for that:

    Error 1 'Microsoft.Xrm.Sdk.AliasedValue' does not contain a definition for 'Id' and no extension method 'Id' accepting a first argument of type 'Microsoft.Xrm.Sdk.AliasedValue' could be found

    Friday, May 3, 2013 8:29 AM
    Moderator
  • please check as below;

    ((Guid)((AliasedValue)c.Attributes["Account"])).Id;

    Friday, May 3, 2013 8:36 AM
  • Also thanks for the quick response, but alas that didn't help.

     Guid thisaccountid = ((Guid)((AliasedValue)c.Attributes["Account"])).Id;

    Error 1 Cannot convert type 'Microsoft.Xrm.Sdk.AliasedValue' to 'System.Guid' 

    I wonder if the complication is because Account is an alias in the groupby line in the FetchXML?

    Friday, May 3, 2013 8:41 AM
    Moderator
  • Feridun

    If you use the following line and debug the code, you can determine the underlying type of the AliasedValue.Value:

    object oVal = ((AliasedValue)c.Attributes["Account"]).Value;
    Once you've got the type, we can then woirk out how to get that as a Guid.

    Microsoft CRM MVP - http://mscrmuk.blogspot.com/ http://www.excitation.co.uk

    Friday, May 3, 2013 8:55 AM
    Moderator
  • Hi,

    Try like this,

    for contactID which is of type EntityReference you have to type cast it following way

    EntityReference contact = (EntityReference)(actCapTyp.GetAttributeValue<AliasedValue>("relatedcontact.contactID").Value);


    VidhyaM

    Friday, May 3, 2013 8:59 AM
  • Thanks for the suggestion. Here is a screenshot of a watch on oVal. Does this help?
    Friday, May 3, 2013 9:25 AM
    Moderator
  • Yes that helps. It's coming back as an EntityReference, rather than a Guid. Try:

    object oVal = ((AliasedValue)c.Attributes["Account"]).Value;
    Guid thisaccountid = ((EntityReference) oVal).Id;


    Microsoft CRM MVP - http://mscrmuk.blogspot.com/ http://www.excitation.co.uk

    Friday, May 3, 2013 9:35 AM
    Moderator
  • Thank you, that did the trick.  I combined the two statements:

                   Guid thisaccountid = (Guid)((EntityReference)(((AliasedValue)c.Attributes["Account"]).Value)).Id;
    You never know, I could turn into a proper programmer!  Then again, perhaps not.
    Friday, May 3, 2013 9:42 AM
    Moderator