简体   繁体   English

使用C#从CRM 365中的Lookup动态获取价值

[英]Get Value Dynamically from Lookup in CRM 365 using C#

I have an entity "new_trialxrmservicetoolkit" inside it I have lookup field "new_LookupTransactionHeader" and it linked to "new_transactionheader" entity. 我里面有一个实体“ new_trialxrmservicetoolkit”,我有一个查找字段“ new_LookupTransactionHeader”,它链接到“ new_transactionheader”实体。

How to solve this? 如何解决呢?

I follow this tutorial but doesn't solve what I am looking for. 我按照本教程进行操作,但是并不能解决我所寻找的问题。

My Code: 我的代码:

new_trialxrmservicetoolkit t = new new_trialxrmservicetoolkit();

ColumnSet myAttributes = new ColumnSet(new String[] { "new_LookupTransactionHeader" });
Entity myEntityHavingLookup = service.Retrieve("new_transactionheader", t.id, myAttributes);
var myLookupId = ((Microsoft.Xrm.Sdk.EntityReference)(myEntityHavingLookup.Attributes["new_LookupTransactionHeader"])).Id;

throw new InvalidPluginExecutionException(myLookupId.ToString()); // catch this result

this the result when I test the plugin: 这是我测试插件时的结果: 在此处输入图片说明

Is this code running inside a plugin registered for ' new_trialxrmservicetoolkit '? 此代码是否在注册为“ new_trialxrmservicetoolkit ”的插件中运行? If yes, then use below code. 如果是,则使用下面的代码。

var entity = (Entity)context.InputParameters["Target"];
if (entity.LogicalName != "new_trialxrmservicetoolkit")
    return;
var entityId=  entity.Id;
ColumnSet myAttributes = new ColumnSet(new String[] { "new_LookupTransactionHeader" });
Entity myEntityHavingLookup = service.Retrieve("new_trialxrmservicetoolkit", entityId, myAttributes);
var myLookupId = ((Microsoft.Xrm.Sdk.EntityReference)(myEntityHavingLookup.Attributes["new_LookupTransactionHeader"])).Id;
throw new InvalidPluginExecutionException(myLookupId.ToString());

If the plugin is for a 'Delete' message, Change the first line of my code: 如果插件用于“删除”消息,请更改代码的第一行:

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

t is of type new_trialxrmservicetoolkit , you cannot use t.id for new_LookupTransactionHeader type. t的类型为new_trialxrmservicetoolkit ,不能将t.id用于new_LookupTransactionHeader类型。

Get the lookup id from targetentity & use it to retrieve other columns. targetentity获取查找ID,并使用它检索其他列。

Get Value of Lookup fields 获取查询字段的值

EntityReference entref = (EntityReference)item.Attributes[attributeName];

var LookupId = entref.Id;

var logicalName = entref.LogicalName;

Set Value of Lookup fields 设置查阅字段的值

newAccount[attributeName] = new EntityReference(logicalName, LookupId);

Set Null Value of Lookup fields 设置查找字段的空值

newAccount[attributeName] = Null;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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