D365(本地):使用QueryExpression用“ LinkedFrom”实体字段更新“ LinkedTo”实体字段

时间:2018-08-05 19:03:56

标签: c# join dynamics-crm updates query-expressions

以下是我的QE,以使用foreach循环获取需要的列的结果集:

using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Workflow;
using System;
using System.Activities;
using Microsoft.Xrm.Sdk.Query;

namespace UpdtPrntInvQuants
{
    public class UpdtPrntInvQuants : CodeActivity
    {
        protected override void Execute(CodeActivityContext executionContext)
        {
            ITracingService tracer = executionContext.GetExtension<ITracingService>();
            IWorkflowContext context = executionContext.GetExtension<IWorkflowContext>();
            IOrganizationServiceFactory serviceFactory = executionContext.GetExtension<IOrganizationServiceFactory>();
            IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);

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

                //TODO: Do stuff
                Guid shiGuid = entity.Id;

                QueryExpression qe0 = new QueryExpression();
                qe0.EntityName = entity.LogicalName;
                qe0.ColumnSet = new ColumnSet();
                qe0.ColumnSet.Columns.Add("new_shid");
                qe0.ColumnSet.Columns.Add("new_name");
                qe0.ColumnSet.Columns.Add("new_claimed");
                qe0.ColumnSet.Columns.Add("new_ii");
                qe0.ColumnSet.Columns.Add("new_prod");
                qe0.Criteria = new FilterExpression();
                qe0.Criteria.AddCondition("new_shid", ConditionOperator.Equal, shiGuid);

                LinkEntity ii = new LinkEntity("new_sh", "new_ii", "new_prod", "new_prod", JoinOperator.Inner);
                ii.Columns = new ColumnSet("new_qohm", "new_qoht", "new_qohg");
                ii.EntityAlias = "ii";

                qe0.LinkEntities.Add(ii);

                EntityCollection shis = service.RetrieveMultiple(qe0);

                foreach (var shi in shis.Entities)
                {
                    tracer.Trace("SHI: ");
                    tracer.Trace("new_shId: " + shI["new_shid"]);
                    tracer.Trace("new_name: " + shI["new_name"]);
                    tracer.Trace("new_claimed: " + shI["new_claimed"]);
                    tracer.Trace("II: ");
                    tracer.Trace("ii qohm: " + (shI.Attributes["ii.new_qohm"] as AliasedValue).Value);
                    tracer.Trace("ii qoht: " + (shI.Attributes["ii.new_qoht"] as AliasedValue).Value); ;
                    tracer.Trace("ii qohg: " + (shI.Attributes["ii.new_qohg"] as AliasedValue).Value);



                }

            }
            catch (Exception e)
            {
                throw new InvalidPluginExecutionException(e.Message);
            }
        }
    }
}

如您所见,我可以在上面的循环中从链接的实体获取值:

tracer.Trace("ii qohm: " + (shI.Attributes["ii.new_qohm"] as AliasedValue).Value);

很难将其设置为来自链接实体的值。

所以基本上我想举个例子:

(shI.Attributes["ii.new_qohm"] as AliasedValue).Value) = shI["new_claimed"]是否在循环中?

任何建议都是很棒的。我已经尝试过setAttributeValue

1 个答案:

答案 0 :(得分:1)

正如所有人都说过的那样,您意识到,service.RetrieveMultiple使用QueryExpression会给您EntityCollection,如果您想更新每个实体,则组成一个属性最少的新Entity对象,并使用service.Update