Skip to main content

Notifications

Announcements

No record found.

Dynamics 365 general forum

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

Posted on by 261

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.

  • J. O. Profile Picture
    J. O. 261 on at
    RE: D365 (On-Prem): Update 'LinkedTo“ Entity field with ”LinkedFrom" Entity field using QueryExpression

    ANSWER:  just created 2 entities and passed ER GUID in to what I needed.  Just wanted to see if I could quickly update LinkedToEntity field to equal LinkedFromEntity field.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

November Spotlight Star - Khushbu Rajvi

Congratulations to a top community star!

Forum Structure Changes Coming on 11/8!

In our never-ending quest to help the Dynamics 365 Community members get answers faster …

Dynamics 365 Community Platform update – Oct 28

Welcome to the next edition of the Community Platform Update. This is a status …

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,269 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,198 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans