Asked by:
HELP:Execute fetch query in a plugin from a textarea

Question
-
I am using this tutorial to create a FetchXML plugin: https://msdn.microsoft.com/en-us/library/gg309565.aspx
The plugin has the fetchXML string hardcoded:
string estimatedvalue_avg = @" <fetch distinct='false' mapping='logical' aggregate='true'> <entity name='opportunity'> <attribute name='estimatedvalue' alias='estimatedvalue_avg' aggregate='avg' /> </entity> </fetch>";
What I have done is create a textarea in CRM, called "new_fetchXML" in the Account entity. The User puts the EXACT fetchXML query in my textarea:
<fetch distinct='false' mapping='logical' aggregate='true'> <entity name='opportunity'> <attribute name='estimatedvalue' alias='estimatedvalue_avg' aggregate='avg' /> </entity> </fetch>"
But, it's failing. It gives me a platform error.
In my plugin, I have the following:
string query = (string)entity["new_fetchXML"];
EntityCollection estimatedvalue_avg_result = _serviceProxy.RetrieveMultiple(new FetchExpression(query));
Just to be clear - I know how to create a plugin, the query works when I follow it; it's just that I cannot put the query in a CRM textarea and have it execute.
When I run the debugger, I see that the value inside the query variable is badly formatted.
Does anyone have any snippets, suggestions?
Thanks.
- Edited by Sebd.DD Wednesday, February 17, 2016 8:45 AM
Wednesday, February 17, 2016 8:43 AM
All replies
-
this line
string query = (string)entity["new_fetchXML"];
you should put the logicalname, so all lowercase, and it's better to use GetAttributeValue
string query = entity.GetAttributeValue<string>("new_fetchxml"); if (query !=null) { //... }
My blog: www.crmanswers.net - CRM Theme Generator
- Proposed as answer by Andrii ButenkoMVP, Moderator Wednesday, February 17, 2016 12:19 PM
Wednesday, February 17, 2016 9:01 AM -
Guido, thanks that worked but why is that? I thought string query = (string)entity["new_fetchXML"]; was better practice?Wednesday, February 17, 2016 9:33 AM
-
You can use code as well and it will work
string query = (string)entity["new_fetchxml"];
Guido's point is that you should use lowercase when you work with late binding fields in CRM.
Dynamics CRM MVP
My blog- Proposed as answer by Andrii ButenkoMVP, Moderator Wednesday, February 17, 2016 12:20 PM
Wednesday, February 17, 2016 12:20 PMModerator -
Andrii, I made a mistake with capitalising but I actually had it lowercase and it didn't work. Guido's method worked, but I wonder why.Wednesday, February 17, 2016 6:48 PM
-
Hi,
Have a look at what the two different calls give as a string result and see if there are any differences.
Regards
Rickard Norström Developer CRM-Konsulterna
http://www.crmkonsulterna.se
Swedish Dynamics CRM Forum: http://www.crmforum.se
My Blog: http://rickardnorstrom.blogspot.seThursday, February 18, 2016 7:53 AM