gpt4 book ai didi

c# - 从 CRM 插件中的 OptionSetValue 获取字符串值

转载 作者:太空狗 更新时间:2023-10-29 22:03:32 26 4
gpt4 key购买 nike

我想知道如何在我制作的 CRM 插件中获取 OptionSet 的字符串值。我以为我所要做的就是将 int 值传递给 OptionSetValue,但这似乎不起作用。这是我的代码:

aBillingFrequencyCode = new OptionSetValue(myContract.BillingFrequencyCode.Value).ToString();

但是输出只是

Microsoft.Xrm.Sdk.OptionSetValue

有什么想法吗?

最佳答案

您可以在不检索实体的所有元数据的情况下检索 OptionSet 标签。我提供了两种方法。一个将使用运行 IOrganizationService 的帐户的语言代码 (LCID)。另一个允许您指定 LCID。

请注意,如果您打算在代码中广泛使用这些,您可能需要考虑缓存这些值以提高性能 - 这将取决于您的特定应用程序要求。

如果您计划同时为单个实体的多个选项集检索这些值,您应该使用上面的 Guido 代码并在一次调用中检索所有实体元数据,以减少您需要对 CRM 进行的调用次数.因此,我们的每个代码片段在某些情况下都更有效率。

//This method will return the label for the LCID of the account the IOrganizationService is using
public static string GetOptionSetValueLabel(string entityName, string fieldName, int optionSetValue, IOrganizationService service)
{
var attReq = new RetrieveAttributeRequest();
attReq.EntityLogicalName = entityName;
attReq.LogicalName = fieldName;
attReq.RetrieveAsIfPublished = true;

var attResponse = (RetrieveAttributeResponse)service.Execute(attReq);
var attMetadata = (EnumAttributeMetadata)attResponse.AttributeMetadata;

return attMetadata.OptionSet.Options.Where(x => x.Value == optionSetValue).FirstOrDefault().Label.UserLocalizedLabel.Label;
}

//This method will return the label for the specified LCID
public static string GetOptionSetValueLabel(string entityName, string fieldName, int optionSetValue, int lcid, IOrganizationService service)
{
var attReq = new RetrieveAttributeRequest();
attReq.EntityLogicalName = entityName;
attReq.LogicalName = fieldName;
attReq.RetrieveAsIfPublished = true;

var attResponse = (RetrieveAttributeResponse)service.Execute(attReq);
var attMetadata = (EnumAttributeMetadata)attResponse.AttributeMetadata;

return attMetadata.OptionSet.Options.Where(x => x.Value == optionSetValue).FirstOrDefault().Label.LocalizedLabels.Where(l => l.LanguageCode == lcid).FirstOrDefault().Label;
}

关于c# - 从 CRM 插件中的 OptionSetValue 获取字符串值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24873581/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com