Query Expression Add Criteria for look up field (Dynamics 365

Microsoft Dynamics 365 Plugin C# code.

If you wanna retrieve multiple records, you may use RetrieveMultiple function.

You can set filters and select the columns(fields) in the retrieve response.

When I set a filter for a lookup field (GUID), I tried two ways.





Both options work normally, the second way is better, greatly reduced the Amount of retrieved records, better for system performance. But the first way is still a good try for reading and comparing GUID value from retrieve response.

Option 1:  Add filter in retrieve result procession. (Bad performance.)

// Retrieve all tasks with regarding opportunity is current opportunity

var queryExpression = new QueryExpression(kidEntity);

var qeFilter = new FilterExpression(LogicalOperator.And);

qeFilter.AddCondition(new ConditionExpression("statecode", ConditionOperator.Equal, "Open"));

qeFilter.AddCondition(new ConditionExpression("subject", ConditionOperator.Like, "%F/U%"));

qeFilter.AddCondition(new ConditionExpression("description", ConditionOperator.Like, "%Automated Task%"));

queryExpression.Criteria = qeFilter;

queryExpression.ColumnSet = new ColumnSet("regardingobjectid");

//Get results:

var result = service.RetrieveMultiple(queryExpression);

foreach (var relatedTask in result.Entities)

{

    if(((EntityReference)relatedTask.Attributes["regardingobjectid"]).Id.ToString() == (regardingobjectid).ToString())

    {

        // Create the Request Object

        var state = new SetStateRequest();

        state.State = new OptionSetValue(stateCode);

        // Point the Request to the case whose state is being changed

        state.EntityMoniker = new EntityReference(kidEntity, relatedTask.Id);

        // Execute the Request

        var stateSet = (SetStateResponse)service.Execute(state);

    }                        

}


Option 2:  Add the filter into the query expression criteria. (Good performance.)

// Retrieve all tasks with regarding opportunity is current opportunity

var queryExpression = new QueryExpression(kidEntity);

var qeFilter = new FilterExpression(LogicalOperator.And);

qeFilter.AddCondition(new ConditionExpression("statecode", ConditionOperator.Equal, "Open"));

qeFilter.AddCondition(new ConditionExpression("subject", ConditionOperator.Like, "%F/U%"));

qeFilter.AddCondition(new ConditionExpression("description", ConditionOperator.Like, "%Automated Task%"));

qeFilter.AddCondition(new ConditionExpression("regardingobjectid", ConditionOperator.Like, regardingobjectid));

queryExpression.Criteria = qeFilter;

queryExpression.ColumnSet = new ColumnSet("regardingobjectid");


//Get results:

var result = service.RetrieveMultiple(queryExpression);

foreach (var relatedTask in result.Entities)

{    

        // Create the Request Object

        var state = new SetStateRequest();

        state.State = new OptionSetValue(stateCode);

        // Point the Request to the case whose state is being changed

        state.EntityMoniker = new EntityReference(kidEntity, relatedTask.Id);

        // Execute the Request

        var stateSet = (SetStateResponse)service.Execute(state);                 

}



No comments:

Post a Comment