DEVKIT1005 - phuocle/Dynamics-Crm-DevKit GitHub Wiki
This analyzer flags potential NullReferenceException when accessing Id, Name, or LogicalName properties of an EntityReference that may be null. Lookup fields in Dynamics 365 can return null if no value is set.
Always check if an EntityReference is null before accessing its properties. Lookup fields are nullable by nature and attempting to access properties on null references will cause runtime exceptions.
Accessing properties on a null EntityReference causes:
-
Runtime Exceptions:
NullReferenceExceptioncrashes your plugin - Failed Transactions: The entire operation may be rolled back
- Poor User Experience: Users see cryptic error messages
- Difficult Debugging: Stack traces don't always clearly indicate the null field
| Scenario | Risk Level |
|---|---|
| New records without lookup value | High |
| Optional lookup fields | High |
| System fields that may be empty | Medium |
| Cleared lookup values | High |
The analyzer flags direct property access on GetAttributeValue<EntityReference>():
-
.Idproperty access -
.Nameproperty access -
.LogicalNameproperty access
// Direct access without null check - will throw if ownerid is null
var ownerId = entity.GetAttributeValue<EntityReference>("ownerid").Id;
// Name access - especially risky as Name is often unset
var ownerName = entity.GetAttributeValue<EntityReference>("ownerid").Name;
// Multiple unsafe accesses
var parentId = entity.GetAttributeValue<EntityReference>("parentaccountid").Id;
var parentName = entity.GetAttributeValue<EntityReference>("parentaccountid").Name;// Option 1: Null-conditional operator (C# 6+)
var ownerId = entity.GetAttributeValue<EntityReference>("ownerid")?.Id;
var ownerName = entity.GetAttributeValue<EntityReference>("ownerid")?.Name;
// Option 2: Explicit null check
var ownerRef = entity.GetAttributeValue<EntityReference>("ownerid");
if (ownerRef != null)
{
var ownerId = ownerRef.Id;
var ownerName = ownerRef.Name;
}
// Option 3: Null-coalescing with default
var ownerId = entity.GetAttributeValue<EntityReference>("ownerid")?.Id ?? Guid.Empty;
var ownerName = entity.GetAttributeValue<EntityReference>("ownerid")?.Name ?? "Unknown";-
Add Null-Conditional Operator: Change
.Idto?.Idwhich returnsGuid? - Use Explicit Null Check: Store reference in variable and check before accessing
- Provide Default Value: Use null-coalescing operator for fallback values
- var id = entity.GetAttributeValue<EntityReference>("lookupfield").Id;
+ var id = entity.GetAttributeValue<EntityReference>("lookupfield")?.Id;If you have a legitimate need to suppress this warning:
#pragma warning disable DEVKIT1005
var ownerId = entity.GetAttributeValue<EntityReference>("ownerid").Id;
#pragma warning restore DEVKIT1005Or in .editorconfig:
[*.cs]
dotnet_diagnostic.DEVKIT1005.severity = warning| Property | Value |
|---|---|
| Rule ID | DEVKIT1005 |
| Category | DynamicsCrm.DevKit |
| Severity | Error |
| Enabled by default | Yes |