Filtering Entities with type Entity Reference?

前端 未结 3 816
旧时难觅i
旧时难觅i 2021-01-26 11:30

How do I query using the string value for an entity reference object?

QueryExpression query = new QueryExpression(\"entityName\");
query.Criteria = new FilterExp         


        
相关标签:
3条回答
  • 2021-01-26 11:41

    While it appears that we're out of luck on getting the "like" operator to compare a string to a GUID on the fly, there are at least a couple potential workarounds:

    1. Retrieve all the GUID's, convert them to a list of strings, and find your subset by querying that list (i.e. with LINQ).
    2. Write a very simple onCreate plugin or workflow that sets a new text field on the entity to the GUID. Then you have the GUID as a string, to work with as you please.

    Here's code I use in a workflow that does exactly what I'm describing (i.e. populates the GUID into a text field). It sets an output parameter to the GUID, then the next step of the workflow populates that value into the field.

    public partial class GetGuid : BaseWorkflow
    {
        [Output("Entity Id")]
        public OutArgument<string> EntityId { get; set; }
    
        protected override void ExecuteInternal(LocalWorkflowContext context)
        {
            EntityId.Set(context.CodeActivityContext, context.WorkflowContext.PrimaryEntityId.ToString());
        }
    }
    

    Please also note that while the "like" operator doesn't work for GUID's, "greater than" and "less than" do, so something like this works:

    <fetch>
        <entity name="account" >
            <attribute name="accountid" />
            <attribute name="name" />
            <filter type="and" >
                <condition attribute="accountid" operator="gt" value="14000000-0000-0000-0000-000000000000" />
            </filter>
        </entity>
    </fetch>
    
    0 讨论(0)
  • 2021-01-26 11:41

    parentaccountid is a Lookup Property .

    LinkEnitity can only be applied for another entities.

    The easy way for filtering is adding name at the end of attribute.

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

    Thanks to Dave, refer here for Q&A

    0 讨论(0)
  • 2021-01-26 11:45

    You need to filter on the name attribute of the associated account entity.

    Join the account entity to the opportunity entity and apply a ConditionExpression to it like this:

    var query = new QueryExpression("opportunity");
    LinkEntity link = query.AddLink("account", "parentaccountid", "accountid");
    link.AddCondition("name", ConditionOperator.BeginsWith, "14");
    
    0 讨论(0)
提交回复
热议问题