Dynamics CRM布尔属性If条件设置及数据校验代码问题求助
tss_isoverdue = true Records Let’s walk through what was broken in your original code and how your updated version resolves the issue—plus a few extra tips to polish it further.
Why the Original Code Failed
The core problem in your initial implementation was this section:
Entity entity = new Entity(_entityname); if (entity.Contains("tss_isoverdue") && entity.Attributes["tss_isoverdue"].Equals(true)) { throw new Exception("IsOverdue with Yes Value is Exist....cannot create same data...!"); }
You instantiated a blank, empty Entity object instead of checking the actual record the user is trying to save. Since this new entity has no attributes populated, the Contains("tss_isoverdue") check always returned false, so your error never fired when it should have.
How the Updated Code Fixes It
Your revised code addresses this by using the plugin's execution context to access the current record being created (CurrentEntity), which holds the real values the user submitted. Here are the key improvements:
- Replaced the hardcoded entity name with
pluginExceptionContext.PrimaryEntityName, making the plugin more flexible if you ever need to reuse it for other entities. - Checks
CurrentEntity(the record being saved) instead of a blank entity, so it correctly evaluates whether the user is trying to settss_isoverduetotrue. - Wraps exceptions in
InvalidPluginExecutionException, which is the standard way to throw user-facing errors in Dynamics CRM plugins—this ensures the error message is properly displayed to the end user instead of failing silently.
Final Working Code
public void Form_OnCreate_PreOperation(IOrganizationService organizationService, IPluginExecutionContext pluginExceptionContext, Entity CurrentEntity) { try { QueryExpression Query = new QueryExpression(pluginExceptionContext.PrimaryEntityName); Query.ColumnSet = new ColumnSet(true); Query.Criteria.AddCondition("tss_isoverdue", ConditionOperator.Equal, true); EntityCollection Items = organizationService.RetrieveMultiple(Query); if (Items.Entities.Count > 0) { if (CurrentEntity.Contains("tss_isoverdue")) { if ((bool)CurrentEntity.Attributes["tss_isoverdue"] == true) throw new Exception("IsOverdue with Yes Value is Exist....cannot create same data...!"); } } } catch (Exception ex) { throw new InvalidPluginExecutionException(_classname + ".Form_OnCreate_PreOperation: " + ex.Message.ToString()); } }
Optional Polish Tips
- Improve performance: Instead of fetching all columns with
ColumnSet(true), you don’t need any columns at all—just check the count of matching records. UseQuery.ColumnSet = new ColumnSet(false);(ornew ColumnSet()in newer CRM versions) to reduce data transfer. - Refine error message: Make it more user-friendly, e.g., "A record with 'Is Overdue' set to Yes already exists. You cannot create another duplicate record."
- Validate execution context: Add a check to ensure this plugin only runs during create operations (though the method name suggests it’s already registered for pre-create, it’s a good safeguard).
内容的提问来源于stack exchange,提问作者aminvincent

