简体   繁体   中英

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

The following is my QE to get the result set with columns I need with foreach loop:

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);
            }
        }
    }
}

As you can see I can get the value from the linked entity in the loop above:

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

Having a hard time setting it to a value from linked entity.

So basically I want to set for example:

(shI.Attributes["ii.new_qohm"] as AliasedValue).Value) = shI["new_claimed"] within the loop?

Any suggestions would be fantastic. I have already tried setAttributeValue .

就像大家都说过的那样,使用QueryExpression service.RetrieveMultiple将为您提供EntityCollection ,如果您想更新每个实体,则组成一个属性最少的新Entity对象,并使用service.Update更新。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM