Dynamics crm +插件代码可在整个实体集合中存储求和公式 [英] Dynamics crm + Plugin code to store sum formula across a entity collection
问题描述
我有以下要求,要在实体(例如实体A")上的插件代码中实现-
I have the below requirement to be implemented in a plugin code on an Entity say 'Entity A'-
下面是实体A"中的数据
Below is the data in 'Entity A'
用字段值记录1
- 价格= 100
- 数量= 4
用字段值记录2
- 价格= 200
- 数量= 2
我需要做两件事
- 添加字段的值并在新记录中对其进行更新
- 将加法公式存储在其他配置实体中
下面显示的示例-
记录3
-
价格
价格值= 300公式值= 100 + 200
Price
Price Value = 300 Formula Value = 100 + 200
数量数量值= 6公式值= 4 + 2
Quantity Quantity Value = 6 Formula Value = 4 + 2
实体A具有名为执行添加"的按钮.然后单击该按钮将触发插件代码.
Entity A has a button named "Perform Addition" and once clicked this will trigger the plugin code.
下面是我尝试过的代码-
Below is the code that i have tried -
AttributeList是我需要对其求和的字段列表.所有字段均为小数
AttributeList is the list of fields i need to perform sum on. All fields are decimal
Entity EntityA = new EntityA();
EntityA.Id = new Guid({"Guid String"});
var sourceEntityDataList = service.RetrieveMultiple(new FetchExpression(fetchXml)).Entities;
foreach (var value in AttributeList)
{
EntityA[value]= sourceEntityDataList.Sum(e => e.Contains(value) ? e.GetAttributeValue<Decimal>(value) : 0);
}
service.Update(EntityA);
我想知道是否有通过linq的方法可以存储公式而不循环?如果没有,我怎么能做到这一点?
I would like to know if there is a way through linq I can store the formula without looping? and if not how can I achieve this?
任何帮助将不胜感激.
推荐答案
以下是一些想法:
有趣的是,您正在从多个记录中计算值,并将结果填充到同级记录而不是父记录中.这不同于典型的汇总"操作.计算.
It's interesting that you're calculating values from multiple records and populating the result onto a sibling record rather than a parent record. This is different than a typical "rollup" calculation.
Dynamics使用SQL顺序GUID生成器生成其ID.如果要在Dynamics之外生成GUID,则可能需要研究
Dynamics uses the SQL sequential GUID generator to generate its ids. If you're generating GUIDs outside of Dynamics, you might want to look into leveraging the same logic.
这是一个如何使用LINQ重构代码的示例:
Here's an example of how you might refactor your code with LINQ:
var target = new Entity("entitya", new Guid("guid"));
var entities = service.RetrieveMultiple(new FetchExpression(fetchXml)).Entities.ToList();
attributes.ForEach(a => target[a] = entities.Sum(e => e.GetAttributeValue<Decimal>(a));
service.Update(target);
GetAttributeValue< Decimal>()
方法默认值设置为0,因此我们可以跳过 Contains
调用.
The GetAttributeValue<Decimal>()
method defaults to 0, so we can skip the Contains
call.
就将公式存储在配置实体上而言,如果您要寻找存储和使用任何公式的功能,则需要一个完整的表达式解析器,其语法与此
As far as storing the formula on a config entities goes, if you're looking for the capability to store and use any formula, you'll need a full expression parser, along the lines of this calculator example. Whether you'll be able to do the Reflection required in a sandboxed plugin is another question.
但是,如果您有一些设置公式,则可以将它们全部编码到插件中,并根据实体的属性和/或配置数据确定在运行时使用哪个公式.
If, however, you have a few set formulas, you can code them all into the plugin and determine which to use at runtime based on the entities' properties and/or config data.
这篇关于Dynamics crm +插件代码可在整个实体集合中存储求和公式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!