Note that there are some explanatory texts on larger screens.

plurals
  1. POCRM 2011 - Intercepting the Quick search, Fetch xml and query expression don't return the same things?
    primarykey
    data
    text
    <p>I'm creating search shortcuts from the quick search box for certain entities. This is to avoid multiple returns especially when a name could contain a city name. (City searches are relevant, so it has to stay)</p> <p>I'm accomplishing this via a plugin. So a user enters </p> <pre><code>/name Todd Richardson </code></pre> <p>In the search box on the contact entity view.</p> <p><strong>Update</strong></p> <p>This intercepts (<del>pre-operation stage:20</del> prevalidation stage:10) the Retrievemultiple request for a contact. </p> <p><strong>End Update</strong></p> <p><strong>Update</strong> As requested here is the beginning of the implementation as generated and then modified from the MSCRM 2011 sdk tools Please remember that this code is in a prototypical state, and may not be suitable for production code:</p> <pre><code>protected void ExecutePreAccountRetrieveMultiple(LocalPluginContext localContext) { if (localContext == null) { throw new ArgumentNullException("localContext"); } if (localContext.PluginExecutionContext.InputParameters.Contains("Query")) { if (localContext.PluginExecutionContext.InputParameters["Query"] is QueryExpression) { //query expression from input is assigned to a local variable for modification. QueryExpression qe = (QueryExpression)localContext.PluginExecutionContext.InputParameters["Query"]; if (qe.Criteria != null) { if (qe.Criteria.Filters.Count &gt; 1) { string entitySubject = qe.EntityName; string searchSubject = qe.Criteria.Filters[1].Conditions[0].Values[0].ToString(); string namePattern = @"^([/\\-])+N(AME)?:?[\s]*(.+$)"; //.... Eliminated for brevity, only including branch thats relevant to this question. if (Regex.IsMatch(searchSubject.TrimEnd("%".ToCharArray()), namePattern, RegexOptions.IgnoreCase)) { var Match = Regex.Match(searchSubject.TrimEnd("%".ToCharArray()), namePattern, RegexOptions.IgnoreCase); if (Match.Groups.Count &gt; 1) { int lastIndex = Match.Groups.Count - 1; string name = Match.Groups.Cast&lt;Group&gt;().Last().Value; Func&lt;string, List&lt;ConditionExpression&gt;&gt; genXpress = (n) =&gt; { List&lt;ConditionExpression&gt; ce = new List&lt;ConditionExpression&gt;(); foreach (var val in name.Split(" ".ToCharArray(), StringSplitOptions.RemoveEmptyEntries).Select(x =&gt; string.Format("%{0}%", x))) { ce.Add(new ConditionExpression { Operator = ConditionOperator.Like, AttributeName = n, Values = { val } }); } return ce; }; if (entitySubject == "contact") { string[] names = name.Split(" ".ToCharArray(), StringSplitOptions.RemoveEmptyEntries); if (names.Length &gt; 1) { string fn = names[names.Length - 2]; string ln = names[names.Length - 1]; string fetchRequest = @"&lt;?xml version=""1.0""?&gt; &lt;fetch distinct=""false"" mapping=""logical"" output-format=""xml-platform"" version=""1.0""&gt; &lt;entity name=""contact""&gt; &lt;attribute name=""fullname""/&gt; &lt;attribute name=""telephone1""/&gt; &lt;attribute name=""contactid""/&gt; &lt;order descending=""false"" attribute=""fullname""/&gt; &lt;filter type=""and""&gt; &lt;filter type=""or""&gt; &lt;filter type=""and""&gt; &lt;condition attribute=""lastname"" value=""%%lastname%%"" operator=""like""/&gt; &lt;condition attribute=""firstname"" value=""%%firstname%%"" operator=""like""/&gt; &lt;/filter&gt; &lt;filter type=""and""&gt; &lt;condition attribute=""lastname"" value=""%%firstname%%"" operator=""like""/&gt; &lt;condition attribute=""firstname"" value=""%%lastname%%"" operator=""like""/&gt; &lt;/filter&gt; &lt;/filter&gt; &lt;/filter&gt; &lt;/entity&gt; &lt;/fetch&gt;" // .Replace("%lastname%", ln).Replace("%firstname%", fn); var conversionRequest = new FetchXmlToQueryExpressionRequest { FetchXml = fetchRequest }; var response = (FetchXmlToQueryExpressionResponse)localContext.OrganizationService.Execute(conversionRequest); localContext.PluginExecutionContext.OutputParameters["Query"] = response.Query; return; } //variable modified and now passed out for execution. localContext.PluginExecutionContext.OutputParameters["Query"] = qe; return; } } } //Remainder of code eliminated for different logic branches. </code></pre> <p><strong>End update</strong></p> <p>A query expression is generated and put into the output parameter named query.</p> <p>Originally I was building the QueryExpression.I was finding that this did not work. No matter how I built my query expression, I was getting </p> <pre><code>condition1 || condition 2 || condition3 || condition4 </code></pre> <p>So I took another angle. I went to the Advanced find and created a query that returned exactly what I wanted in the Results. I downloaded the fetch-xml and now this is what I have (as seen in the code previous):</p> <pre><code>string fetchRequest = @"&lt;?xml version=""1.0""?&gt; &lt;fetch distinct=""false"" mapping=""logical"" output-format=""xml-platform"" version=""1.0""&gt; &lt;entity name=""contact""&gt; &lt;attribute name=""fullname""/&gt; &lt;attribute name=""telephone1""/&gt; &lt;attribute name=""contactid""/&gt; &lt;order descending=""false"" attribute=""fullname""/&gt; &lt;filter type=""and""&gt; &lt;filter type=""or""&gt; &lt;filter type=""and""&gt; &lt;condition attribute=""lastname"" value=""%%lastname%%"" operator=""like""/&gt; &lt;condition attribute=""firstname"" value=""%%firstname%%"" operator=""like""/&gt; &lt;/filter&gt; &lt;filter type=""and""&gt; &lt;condition attribute=""lastname"" value=""%%firstname%%"" operator=""like""/&gt; &lt;condition attribute=""firstname"" value=""%%lastname%%"" operator=""like""/&gt; &lt;/filter&gt; &lt;/filter&gt; &lt;/filter&gt; &lt;/entity&gt; &lt;/fetch&gt;" // </code></pre> <p>Whether I was generating the Queryexpression in code, or fetching it from the organization service, it seems to get me the same result. Instead of </p> <pre><code>(condition1 &amp;&amp; condition2) || (condition3 &amp;&amp; condition4) </code></pre> <p>fulfilling the criteria, it basically ends up </p> <pre><code>condition1 || condition2 || condition3 || condition4 </code></pre> <p>I've tried other variations on the fetch xml, including:</p> <pre><code>string fetchRequest = @"&lt;?xml version=""1.0""?&gt; &lt;fetch distinct=""false"" mapping=""logical"" output-format=""xml-platform"" version=""1.0""&gt; &lt;entity name=""contact""&gt; &lt;attribute name=""fullname""/&gt; &lt;attribute name=""telephone1""/&gt; &lt;attribute name=""contactid""/&gt; &lt;order descending=""false"" attribute=""fullname""/&gt; &lt;filter type=""and""&gt; &lt;condition attribute=""lastname"" value=""%%lastname%%"" operator=""like""/&gt; &lt;condition attribute=""firstname"" value=""%%firstname%%"" operator=""like""/&gt; &lt;/filter&gt; &lt;/entity&gt; &lt;/fetch&gt;" </code></pre> <p>Again this ends up being </p> <pre><code>condition1 || condition2 </code></pre> <p>not</p> <pre><code>condition1 &amp;&amp; condition2 </code></pre> <p>Anyone have any clue as to what is going on. Is there a different fetchxml I should be using? Is this a bug? The answer has been elluding me for a better part of the day. </p> <p>Hopefully it's just something easy that I'm overlooking. </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload