Querying Lookup Property Attribute with “Name” in Opportunity Entity - Dynamics CRM Online [duplicate]

对着背影说爱祢 提交于 2019-12-12 05:57:48

问题


I'm retrieving the results after connecting to the dynamics CRM as below:

CrmServiceClient crmConn = new CrmServiceClient(ConfigurationManager.ConnectionStrings["default"].ConnectionString);
IOrganizationService crmService = crmConn.OrganizationServiceProxy;

QueryExpression query = new QueryExpression("opportunity");    
query.Criteria = new FilterExpression();
query.Criteria.AddCondition("name", ConditionOperator.Like, "14%");

EntityCollection results = crmService.RetrieveMultiple(query);

Now as the name is of type string, I was able to add condition & perform operations & get the results.

My requirement is I've to add another filter which is a lookup property "parentaccountid".

I'm trying to add the condition as below but it throws cast exception error as it is expecting only GUID.

query.Criteria.AddCondition("parentaccountid", ConditionOperator.Like, "%In%");

Note: the type of parentaccountid is Microsoft.Xrm.Sdk.EntityReference when I retrieved from early results

The reason is we can apply filter for parentaccountid only with GUID.

Is there any way to add the condition based on "Name" instead of "Id"?


回答1:


Yes, just add "name" to the end of "parentaccountid":

query.Criteria.AddCondition("parentaccountidname", ConditionOperator.Like, "%In%");

You'll notice that if you create an Advanced Find like this:

And then download the FetchXML, CRM just appends "name" to the end of lookup attribute name:

<fetch version="1.0" output-format="xml-platform" mapping="logical" distinct="false">
  <entity name="opportunity">
    <attribute name="name" />       
    <order attribute="name" descending="false" />
    <filter type="and">
      <condition attribute="parentaccountidname" operator="like" value="%In%" />
    </filter>
  </entity>
</fetch>

An alternative solution would be to use a LinkEntity, and query the name of the linked account.



来源:https://stackoverflow.com/questions/45230749/querying-lookup-property-attribute-with-name-in-opportunity-entity-dynamics

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!