LinkEntity issue with ‘Partyid’

Today, I was working with LinkEntity for ‘PartyId’, So I want all appointment where specific account or contact is in the required field and is connected to a specific entity.

Thus, I created 3 appointments on the entity and wrote following query…

QueryExpression appointmentQuery = new QueryExpression(“appointment”);
appointmentQuery.ColumnSet = new ColumnSet(“activityid”, “requiredattendees”, “optionalattendees”, “regardingobjectid”, “createdon”,
“scheduledstart”, “soft_consulationsubtypeid”, “soft_consultationtypeid”, “statecode”, “soft_modeofcontact”);

appointmentQuery.Criteria.AddCondition(“regardingobjectid”, ConditionOperator.Equal, primaryEntity.Id);
//appointmentQuery.Criteria.AddCondition(“statecode”, ConditionOperator.Equal, 0);
appointmentQuery.AddOrder(“createdon”, OrderType.Descending);
appointmentQuery.Distinct = true;

//link to consultation
LinkEntity linkToActivity = new LinkEntity()
{
LinkFromEntityName = “appointment”,
LinkFromAttributeName = “activityid”,

LinkToEntityName = “activityparty”,
LinkToAttributeName = “activityid”,

JoinOperator = JoinOperator.Inner,

Columns = new ColumnSet(“activityid”, “partyid”),

EntityAlias = “activityparty”,

};

linkToActivity.LinkCriteria.AddCondition(“partyid”, ConditionOperator.In,
((EntityReference)primaryEntity.Attributes[“aams_apprenticeid”]).Id, ((EntityReference)primaryEntity.Attributes[“aams_employerid”]).Id);

appointmentQuery.LinkEntities.Add(linkToActivity);

EntityCollection appointments = service.RetrieveMultiple(appointmentQuery);

Expected result of it is 3 appointments but it returned 5 appointments. I was stunned, not able to figure out the issue, so created fetch XML from advanced find and it gave me correct 3 records as expected. That fetch XML is

private string queryString(Guid cntId, Guid acId, Guid entId, IOrganizationService service)
{
var fetchXML = String.Format(@”<fetch version=’1.0′ output-format=’xml-platform’ mapping=’logical’ distinct=’true’>
<entity name=’appointment’>
<attribute name=’subject’ />
<attribute name=’statecode’ />
<attribute name=’scheduledstart’ />
<attribute name=’scheduledend’ />
<attribute name=’createdby’ />
<attribute name=’regardingobjectid’ />
<attribute name=’activityid’ />
<attribute name=’instancetypecode’ />
<order attribute=’subject’ descending=’false’ />
<link-entity name=’activityparty’ from=’activityid’ to=’activityid’ alias=’ah’>
<filter type=’and’>
<filter type=’or’>
<condition attribute=’partyid’ operator=’eq’ uitype=’contact’ value='{0}’ />
<condition attribute=’partyid’ operator=’eq’ uitype=’account’ value='{1}’ />
</filter>
</filter>
</link-entity>
<link-entity name=’soft_training_contract’ from=’soft_training_contractid’ to=’regardingobjectid’ alias=’ai’>
<filter type=’and’>
<condition attribute=’soft_training_contractid’ operator=’eq’ uiname=’FOOD CORPORATION PTY. LTD.’ uitype=’soft_training_contract’ value='{3}’ />
</filter>
</link-entity>
</entity>
</fetch>”, cntId, acId, entId);

return fetchXML;
}

So, to get the correct result, I added a group by on ID and it was working fine, but it’s not correct way. After a couple of hours, I removed ‘Partyid’ from linkEntity and it worked perfect, that query is …

QueryExpression appointmentQuery = new QueryExpression(“appointment”);
appointmentQuery.ColumnSet = new ColumnSet(“activityid”, “requiredattendees”, “optionalattendees”, “regardingobjectid”, “createdon”, “scheduledstart”, “soft_consulationsubtypeid”, “soft_consultationtypeid”, “statecode”, “soft_modeofcontact”);

appointmentQuery.Criteria.AddCondition(“regardingobjectid”, ConditionOperator.Equal, primaryEntity.Id);
//appointmentQuery.Criteria.AddCondition(“statecode”, ConditionOperator.Equal, 0);
appointmentQuery.AddOrder(“createdon”, OrderType.Descending);
appointmentQuery.Distinct = true;

//link to consultation
LinkEntity linkToActivity = new LinkEntity()
{
LinkFromEntityName = “appointment”,
LinkFromAttributeName = “activityid”,

LinkToEntityName = “activityparty”,
LinkToAttributeName = “activityid”,

JoinOperator = JoinOperator.Inner,

Columns = new ColumnSet(“activityid”),//, “partyid”),

EntityAlias = “activityparty”,

};

linkToActivity.LinkCriteria.AddCondition(“partyid”, ConditionOperator.In,
((EntityReference)primaryEntity.Attributes[“aams_apprenticeid”]).Id, ((EntityReference)primaryEntity.Attributes[“aams_employerid”]).Id);

//FilterExpression activityFilter = new FilterExpression(LogicalOperator.Or);
//activityFilter.Conditions.Add(new ConditionExpression(“partyid”, ConditionOperator.Equal, ((EntityReference)primaryEntity.Attributes[“aams_apprenticeid”]).Id));
//activityFilter.Conditions.Add(new ConditionExpression(“partyid”, ConditionOperator.Equal, ((EntityReference)primaryEntity.Attributes[“aams_employerid”]).Id));
//linkToActivity.LinkCriteria = activityFilter;

appointmentQuery.LinkEntities.Add(linkToActivity);

EntityCollection appointments = service.RetrieveMultiple(appointmentQuery);

var x = appointments.Entities.GroupBy(e=> e.Id);

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s