2013-03-22 81 views
2

我正在编写一个插件,用于检查字段,选项集的特定值是否等于特定值,如果是,则执行特定操作。CRM 2011插件 - 检查OptionSet是否为空

现在,在插件C#代码中,如何检查选项集字段是否为空 - 即设置为默认值?

我做了什么(显然,这是错误的),因为它从来没有超过Null检查语句。而且,如果我没有检查,然后我得到这个错误信息

错误

Unexpected exception from plug-in (Execute): CRM.AppStateHandler.Plugins.PostApplicationCreate: System.NullReferenceException: Object reference not set to an instance of an object. 

代码

application applicationEntity = entity.ToEntity<new_application>(); 
if (new_applicationEntity.new_Applicationstatus != null) 
{ 
    var applicationStatus = applicationEntity.new_Applicationstatus.Value; 
    if (applicationStatus == CRMConstants.EntityApplication.Attributes.ApplicationStatusOptions.Applying) 
    { 
     //my logic 
    } 
} 

文件constants.cs有以下

class CRMConstants 
{ 
    public struct EntityApplication 
    { 
     public struct Attributes 
     { 
      public struct ApplicationStatusOptions 
      { 
       // More before this 
       public const int Applying = 100000006; 
       // More to come 
      } 
     } 
    } 

回答

2

正在检查:

if (new_applicationEntity.new_Applicationstatus != null) 

,但你需要检查:

if (applicationEntity.new_Applicationstatus != null) 
3

我认为SergeyS有你的修复,但我会添加一些其他的(希望)有用的意见。

不要为您的选项集值自定义创建结构。使用CrmSrvcUtil自动为您创建枚举。

我得到恼火不必检查OptionSetValues为空或不是,所以我用这些扩展方法,使我的生活更轻松:

/// <summary> 
/// Returns the value of the OptionSetValue, or int.MinValue if it is null 
/// </summary> 
/// <param name="osv"></param> 
/// <param name="value"></param> 
/// <returns></returns> 
public static int GetValueOrDefault(this OptionSetValue osv) 
{ 
    return GetValueOrDefault(osv, int.MinValue); 
} 

/// <summary> 
/// Returns the value of the OptionSetValue, or int.MinValue if it is null 
/// </summary> 
/// <param name="osv"></param> 
/// <param name="value"></param> 
/// <returns></returns> 
public static int GetValueOrDefault(this OptionSetValue osv, int defaultValue) 
{ 
    if (osv == null) 
    { 
     return defaultValue; 
    } 
    else 
    { 
     return osv.Value; 
    } 
} 

/// <summary> 
/// Allows for Null safe Equals Comparison for more concise code. ie. 
/// if(contact.GenderCode.NullSafeEquals(1)) 
/// vs. 
/// if(contact.GenderCode != null && contact.gendercode.Value == 1) 
/// </summary> 
/// <param name="osv"></param> 
/// <param name="value"></param> 
/// <returns></returns> 
public static bool NullSafeEquals(this OptionSetValue osv, int value) 
{ 
    if (osv == null) 
    { 
     return false; 
    } 
    else 
    { 
     return osv.Value == value; 
    } 
} 

/// <summary> 
/// Allows for Null safe Equals Comparison for more concise code. ie. 
/// if(contact.GenderCode.NullSafeEquals(new OptionSet(1))) 
/// vs. 
/// if(contact.GenderCode != null && contact.gendercode.Value == new OptionSet(1)) 
/// </summary> 
/// <param name="osv"></param> 
/// <param name="value"></param> 
/// <returns></returns> 
public static bool NullSafeEquals(this OptionSetValue osv, OptionSetValue value) 
{ 
    if (osv == null) 
    { 
     return osv == value; 
    } 
    else 
    { 
     return osv.Equals(value); 
    } 
} 

有各自两种方法与过载:

  • GetValueOrDefault - 这相当于Nullable.GetValueOrDefault()。一个区别是,而不是默认为0,我默认为int.MinValue,以确保我不会意外匹配0选项集值。 Overload允许您指定默认值,如果您愿意。

  • NullSafeEquals - 这是你会使用在你的代码不用检查空

application applicationEntity = entity.ToEntity<new_application>(); 
if (applicationEntity.new_Applicationstatus.NullSafeEquals(CRMConstants.EntityApplication.Attributes.ApplicationStatusOptions.Applying)) 
{ 
    //my logic 
} 
+0

你可以概括的程序使用CRMCrvcUtil生成枚举的人吗?这是否包括自定义字段的选项集值? – DeveloperM 2014-04-02 18:17:24

+0

@DeveloperM检查了这一点:http://www.develop1.net/public/post/Generate-Early-Bound-OptionSet-enums.aspx转换,我还为XrmToolBox创建一个插件,以加上更多..希望我能在一两周内把它弄清楚。 – Daryl 2014-04-02 18:55:04

+0

感谢您的链接。我在几个月前看到并遵循指示,但生成的文件不包含枚举,如预期的那样。我会再试一次,也许我有一个错字或错误汇总...... – DeveloperM 2014-04-03 19:46:40