Answered by:
CRM 2011 RetrieveMultiple Help

Question
-
I looked around the forum, and couldn't come up with a solution, so:
I'm very new to CRM, and I'm having trouble finding specific information on how to get certain things to work. In this case, I have a parent view which contains a column containing a rollup of values from children. In the Retrieve version, everything works fine, and the value is an accurate sum of the child values. I'm just not sure how to get it to also show in the RetrieveMulitple version. Here's what I have so far.
Retrieve version:
//Rollup on Retrieve if (context.OutputParameters.Contains("BusinessEntity") && context.MessageName.Equals("Retrieve", StringComparison.InvariantCultureIgnoreCase)) { Entity parent = context.OutputParameters["BusinessEntity"] as Entity; using (VetUnitedXrm.XrmContext xrm = new VetUnitedXrm.XrmContext(serviceFactory.CreateOrganizationService(null))) { var total = (from c in xrm.new_childSet where c.new_ParentId != null && c.new_ParentId.Id == parent.Id && c.new_Amount != null select c.new_Amount).ToList().Sum(); if (parent.Contains("new_childrollup")) { parent["new_childrollup"] = total; } else { parent.Attributes.Add(new KeyValuePair<string, object>("new_childrollup", total)); } } }
How do I get this to work in the RetrieveMulitple version?
Thanks in advance for any help you can offer.
Friday, June 21, 2013 7:02 AM
Answers
-
Hi,
You could do this in RetrieveMultiple using the following code in a Post Plugin step:
IPluginExecutionContext context = localContext.PluginExecutionContext; EntityCollection collection = (EntityCollection) localContext.PluginExecutionContext.OutputParameters["BusinessEntityCollection"]; foreach (Entity e in collection.Entities) { // Do rollup calculation s["new_childrollup"] = // result of rollup calculation }
That said - I would recommend against this since your plugin would be very inefficient and make your system run slowly for users.
A better option would be to use goal roll ups (if you can) - or re-calculate the rollup when any of the child entities are added/removed/updated. You could do this via plugin or you could use N52 Formula Manager - http://support.north52.com/knowledgebase/articles/182595-training-video-n52-formula-manager-set-custom-
hth
Scott Durow
Blog www.develop1.netFollow Me
Rockstar365
If this post answers your question, please click "Mark As Answer" on the post and "Mark as Helpful"- Edited by Scott Durow (MVP)MVP, Editor Friday, June 21, 2013 8:32 AM updated
- Proposed as answer by Scott Durow (MVP)MVP, Editor Friday, June 21, 2013 4:42 PM
- Marked as answer by brento73 Friday, June 21, 2013 6:31 PM
- Unmarked as answer by brento73 Friday, June 21, 2013 6:37 PM
- Marked as answer by brento73 Friday, June 21, 2013 8:45 PM
Friday, June 21, 2013 8:31 AMAnswerer
All replies
-
Hi,
You could do this in RetrieveMultiple using the following code in a Post Plugin step:
IPluginExecutionContext context = localContext.PluginExecutionContext; EntityCollection collection = (EntityCollection) localContext.PluginExecutionContext.OutputParameters["BusinessEntityCollection"]; foreach (Entity e in collection.Entities) { // Do rollup calculation s["new_childrollup"] = // result of rollup calculation }
That said - I would recommend against this since your plugin would be very inefficient and make your system run slowly for users.
A better option would be to use goal roll ups (if you can) - or re-calculate the rollup when any of the child entities are added/removed/updated. You could do this via plugin or you could use N52 Formula Manager - http://support.north52.com/knowledgebase/articles/182595-training-video-n52-formula-manager-set-custom-
hth
Scott Durow
Blog www.develop1.netFollow Me
Rockstar365
If this post answers your question, please click "Mark As Answer" on the post and "Mark as Helpful"- Edited by Scott Durow (MVP)MVP, Editor Friday, June 21, 2013 8:32 AM updated
- Proposed as answer by Scott Durow (MVP)MVP, Editor Friday, June 21, 2013 4:42 PM
- Marked as answer by brento73 Friday, June 21, 2013 6:31 PM
- Unmarked as answer by brento73 Friday, June 21, 2013 6:37 PM
- Marked as answer by brento73 Friday, June 21, 2013 8:45 PM
Friday, June 21, 2013 8:31 AMAnswerer -
Thanks! I'm just getting my feet wet, so for now it's mostly an exercise to learn how things work, rather than a real production environment, so I implemented the slow way(worked perfectly, thanks again). I'll look into the other options, now, as well.
Brent
Friday, June 21, 2013 2:50 PM -
Hi Brent,
Good to here you got it working.
Don't forget to click 'Mark as Answer' to close the thread.
Cheers.
Scott Durow
Blog www.develop1.netFollow Me
Rockstar365
If this post answers your question, please click "Mark As Answer" on the post and "Mark as Helpful"Friday, June 21, 2013 4:42 PMAnswerer -
I have run into another strange behavior, though. Although it seemed fine when I just had a single parent in the list, when I added a second test parent, it didn't get updated. Here's the code I ended up with:
if (context.OutputParameters.Contains("BusinessEntityCollection") && context.MessageName.Equals("RetrieveMultiple", StringComparison.InvariantCultureIgnoreCase)) { EntityCollection query = context.OutputParameters["BusinessEntityCollection"] as EntityCollection; foreach (Entity parent in query.Entities) if (parent.Contains("new_childrollup")) { using (VetUnitedXrm.XrmContext xrm = new VetUnitedXrm.XrmContext(serviceFactory.CreateOrganizationService(null))) { var total = (from c in xrm.new_childSet where c.new_ParentId != null && c.new_ParentId.Id == parent.Id && c.new_Amount != null select c.new_Amount).ToList().Sum(); if (parent.Contains("new_childrollup")) { parent["new_childrollup"] = total; } else { parent.Attributes.Add(new KeyValuePair<string, object>("new_childrollup", total)); } } } }
Any ideas why only the first parent on the list is getting updated?
Friday, June 21, 2013 6:34 PM -
I suspect if you remove the parent.Contains check it will work. You don't need to check this.
hth
Scott Durow
Blog www.develop1.netFollow Me
Rockstar365
If this post answers your question, please click "Mark As Answer" on the post and "Mark as Helpful"Friday, June 21, 2013 8:36 PMAnswerer -
Ah! Of course! Thanks again!
Brent
Brent O'Dell
Friday, June 21, 2013 8:45 PM