0
votes

Saw this post about converted the results from FetchXML to strongly typed CRM entities.

//CrmEntity is a base class I insert into my generated code 
//to differentiate early-bound classes; you can use Entity

public static T ToRelatedEntity<T>(this Entity rawEntity, string relatedAlias)
    where T:CrmEntity, new()        
{
    var result = new Entity(new T().LogicalName);

    foreach(var attribute in rawEntity.Attributes
                                .Where(kvp=>kvp.Key
                                          .StartsWith(relatedAlias + ".")))
    {
        var newName = attribute.Key.Replace(relatedAlias + ".", String.Empty);
        result[newName] = ((AliasedValue)attribute.Value).Value;
    }
    foreach(var formattedValue in rawEntity.FormattedValues
                                    .Where(kvp=>kvp.Key
                                              .StartsWith(relatedAlias + ".")))
    {
        var newName = formattedValue.Key.Replace(relatedAlias + ".", String.Empty);
        result.FormattedValues[newName] = formattedValue.Value;
    }

    return result.ToEntity<T>();
}

//usage
var query = new FetchExpression(fetchXml);
var response = service.RetrieveMultiple(query);
var contact = response.Entities[0].ToEntity<Contact>();
var newCustom = response.Entities[0].ToRelatedEntity<new_custom>("nc");

Unfortunately, I need to retrieve the related entities ID and no IDs are returned after a FetchXML call; just empty Guids.

I can get the parent entity (cc_case) ID by calling result.ID.

But what about the children entities? How do I get their IDs?

EDIT: Here's an example of FetchXML:

<fetch no-lock="true">
<entity name="cc_case" >
<all-attributes/>
<filter type="and" >
  <condition attribute="cc_caseid" operator="equals" > 
     XXXX
  </condition>
</filter>
<link-entity name="cc_contact_account" from="contactid" to="cc_submitterid" link-type="outer" alias="CC_Contact_Account" >
  <all-attributes/>
  <link-entity name="account" from="accountid" to="accountid" alias="Account" >
    <all-attributes/>
    <link-entity name="contact" from="contactid" to="cc_billingcontactid" alias="Contact" >
      <all-attributes/>
    </link-entity>
  </link-entity>
</link-entity>
<link-entity name="cc_patient" from="cc_patientid" to="cc_patientid" link-type="outer" alias="Patient" >
  <all-attributes/>
</link-entity>

So, the question is how do you get the ID from the cc_contact_account and cc_patient entities. I can retrieve the ID from entity cc_case, but not from link-entities.

1
Did you look for "new_customid"? Can you post your fetchxml? - dynamicallyCRM
What is "this post"? Also post your query and describe your entity model, hard to answer this question based on the information you have provided. - James Wood

1 Answers

0
votes

The id's from the link-entities should be available as aliased attributes in the AttributeCollection, so in your case result["Patient.cc_patientid"] would get you the patient id. That is what your ToRelatedEntity function is doing.

If cc_contact_account is a many-to-many relationship, I don't think that would have an id at all, but you could use the same alias method as above to get the id's on the other side of the relationship.