在CRM中更新字段的插件

时间:2014-07-30 09:35:35

标签: c# plugins dynamics-crm-2013

我有以下C#插件正在我的问题实体上从网格中检索数据

通过FetchXML查询

检索数据

最终结果是我有一个名为Total的int,它保存我想要的数据

我现在想把数据放入CRM中的一个名为" new_businessstring"但是,当点击保存按钮并执行插件时,数据不会显示在字段中

这是我的所有代码

 public class CostBusiness : IPlugin
{

    string costToBusiness;
    int total;

    public void Execute(IServiceProvider serviceProvider)
    {

        IPluginExecutionContext context = (IPluginExecutionContext)
            serviceProvider.GetService(typeof(IPluginExecutionContext));


        IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
        IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);




        // The InputParameters collection contains all the data passed 
        // in the message request.
        if (context.InputParameters.Contains("Target") &&
            context.InputParameters["Target"] is Entity)
        {
            // Obtain the target entity from the Input Parameters.

            Entity entity = (Entity)context.InputParameters["Target"];




            //Retrieve Products Details
            Guid orderID = (Guid)((Entity)context.InputParameters["Target"]).Id;
            //FetchXML Query will retrieve product details from different entityes, Link-entity is where the different entites used are bought in
            //Condition Attribute is used to only select the products that is in the Grid on the Order Form

            string fetchxml =
            @"<fetch distinct='true' mapping='logical' output-format='xml-platform' version='1.0'>
                    <entity name='new_issue'>
                        <attribute name='new_issueid'/>
                        <attribute name='new_name'/>
                        <attribute name='createdon'/>
                            <order descending='false' attribute='new_issueid'/>
                            <filter type='and'> 
                                <condition attribute='new_issueid' operator='eq' value='" + orderID + @"'/> 
                            </filter> 
                            <link-entity name='new_costtobusiness' alias='ab' to='new_issueid' from='new_issue_costid'>
                                <attribute name='new_coststring'/>                                             
                            </link-entity>
                    </entity>
              </fetch>";

            EntityCollection result = service.RetrieveMultiple(new FetchExpression(fetchxml));
            {
                if (result != null && result.Entities.Count > 0)
                {
                    List<int> _product = new List<int>();

                    foreach (Entity _entity in result.Entities)//loop through every record
                        {

                           //costToBusiness = _entity.Attributes["new_coststring"].ToString();
                            costToBusiness = ((AliasedValue)_entity.Attributes["ab.new_coststring"]).Value.ToString();
                            total = (total) + Convert.ToInt32(costToBusiness);           
                        }

                    if(entity.Attributes.Contains("new_businessstring"))
                    {
                        entity.Attributes["new_businessstring"] = total;

                    }
                    else
                    {
                        entity.Attributes.Add("new_businessstring", total);

                    }

                }

            }

        }
    }

我假设问题与以下

有关
if(entity.Attributes.Contains("new_businessstring"))
                    {
                        entity.Attributes["new_businessstring"] = total;

                    }
                    else
                    {
                        entity.Attributes.Add("new_businessstring", total);

                    }

或者我错过了什么?

0 个答案:

没有答案