Error: AliasedValue – Key not found

Key not Found

Error: Key not found in Link Entity QueryExpression (Ref above image).

Solution Sample Code 

QueryExpression queryTcMilestone = new QueryExpression(“soft_tcmilestone”);
queryTcMilestone.ColumnSet = new ColumnSet(“soft_tcmilestoneid”, “soft_amount”, “soft_milestonedate”);

queryTcMilestone.Criteria.AddCondition(“statecode”, ConditionOperator.Equal, 0);
queryTcMilestone.Criteria.AddCondition(“soft_incentiveid”, ConditionOperator.Equal, tcIncentiveId);
queryTcMilestone.AddOrder(“soft_milestonedate”, OrderType.Descending);

LinkEntity linkClaim = new LinkEntity()
{
LinkFromEntityName = “soft_tcmilestone”,
LinkFromAttributeName = “soft_claimid”,
LinkToEntityName = “soft_claim”,
LinkToAttributeName = “soft_claimid”,
JoinOperator = JoinOperator.Inner,
Columns = new ColumnSet(“soft_claimid”, “soft_claimamount”, “soft_claimeffectdate”),
EntityAlias = “soft_claim”
};
//Draft, Pending submission, Pending co-authorisation, Pending network provider authorisation, Pending Approval
linkClaim.LinkCriteria.AddCondition(“soft_claimstatus”, ConditionOperator.In, 450920007, 450920000, 450920001, 450920002, 450920008);

queryTcMilestone.LinkEntities.Add(linkClaim);

var results = service.RetrieveMultiple(queryTcMilestone);

foreach (var tcm in results.Entities)
{

Entity claim = new Entity(“soft_claim”)
{

//method 1: without entity extention
Id = (Guid)(((AliasedValue)(tcm.Attributes[“soft_claim.soft_claimid”])).Value)
};

//method 2: with entity extention
Money claimMoney = tcm.GetAliasedValue<Money>(“soft_claim.soft_claimamount”);
if (claimMoney != null && claimMoney.Value != reassessedAmount)
{
claim.Attributes[“soft_claimamount”] = reassessedAmount != null ? new Money(reassessedAmount.Value) : null;
updateEntity = true;
}

service.Update(claim);

}

Entity Extention Method:

public static class EntityExtension
 {
 public static T GetAliasedValue<T>(this Entity e, string aliasedEntityAttributeName)
 {
 var aliasedValue = e.GetAttributeValue<AliasedValue>(aliasedEntityAttributeName)?.Value;
 return (T)aliasedValue;
 }
 }

 

Thanks …

Reference:

https://msdn.microsoft.com/en-us/library/microsoft.xrm.sdk.aliasedvalue.aspx

 

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