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

D365 (On-Prem): Update 'LinkedTo" Entity field with "LinkedFrom" Entity field using QueryExpression

以下是我的 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.

正如大家所说,你意识到,service.RetrieveMultiple 使用 QueryExpression 会给你 EntityCollection,如果你想更新每个实体,然后组成一个具有最少属性的新实体对象 &使用 service.Update.

更新