WorkqueueAssignment
Overview
WorkqueueAssignmentTask assigns items (accounts or visits) to workqueue stacks based on configured assignment criteria. The task evaluates all enabled WorkqueueAssignmentCriteria, searches for matching items using the criteria's search terms, creates ordered item stacks, and optionally removes items from workqueues that no longer match the criteria.
Use this task when: You need to populate workqueues with accounts or visits that match specific search criteria, typically as a scheduled job to keep workqueues up-to-date.
Don't use this task when:
- You need to manually assign specific items to a workqueue (use the UI or workqueue controller directly)
- You want to fetch items from an existing stack (use workqueue fetching logic instead)
- You need to update a single criteria without affecting others (use the
CriteriaKeyparameter to target one criteria)
Parameters
| Parameter | Type | Required | Default | Description |
|---|---|---|---|---|
CriteriaKey |
string |
No | null |
The key of a specific WorkqueueAssignmentCriteria to process. If null or empty, all enabled criteria are processed. |
Execution Flow
Usage Examples
Example 1: Process all enabled criteria (scheduled task)
var task = new WorkqueueAssignmentTask
{
// CriteriaKey = null by default - processes all enabled criteria
};
// Run this as a scheduled job (e.g., daily at 3 AM) to keep all workqueues current
Example 2: Process a specific criteria after configuration change
var task = new WorkqueueAssignmentTask
{
CriteriaKey = "WAC-12345" // Only process this specific criteria
};
// Use when a specific criteria was modified and needs immediate refresh
Example 3: Initial workqueue population
// First, create the WorkqueueAssignmentCriteria in the database
var criteria = new WorkqueueAssignmentCriteria
{
Key = "HighValueAccounts",
Name = "High Value Accounts Queue",
Enabled = true,
Type = WorkqueueEntityType.Account,
AutoUnassign = true,
ViewType = WorkqueueType.Standard,
FetchSize = 1,
SortBy = "Service Date",
SortDirection = "DESC",
Assignees = new List<string> { "[email]", "[email]" },
Criteria = new List<SearchTerm>
{
new SearchTerm("Account Status", "Open"),
new SearchTerm("Balance", "> 5000")
}
};
criteriaRepo.Save(criteria);
// Then run the assignment task
var task = new WorkqueueAssignmentTask
{
CriteriaKey = "HighValueAccounts"
};
// This will create workqueues for both assignees and populate them
Related Tasks Comparison
| Task | Use Case | Manual vs Automatic | Scope | Item Removal |
|---|---|---|---|---|
| WorkqueueAssignment | Populate workqueues based on search criteria | Automatic (scheduled) | All or specific criteria | Controlled by AutoUnassign flag |
| Manual Assignment (UI) | Ad-hoc item assignment | Manual | Single items | Not applicable |
| Workqueue Fetch (UI) | Retrieve items from existing stack | Manual | User-triggered | Items remain in stack |
Performance Considerations
Database Impact
- Connection Usage: Single connection for the entire operation
- Tables Read:
WorkqueueAssignmentCriteria- to get enabled criteriaAccountorVisit- to search for matching itemsWorkqueue- to check existing workqueues- Custom property tables (if criteria includes custom properties)
- Tables Written:
WorkqueueCriteriaItemMap- bulk updates viaSaveItemsForCriteria(deletes old, inserts new)Workqueue- creates new workqueues for assignees if they don't existWorkqueueEntityMap- updates workqueue itemsActivityLog- logs all assignment/unassignment activities
- Bulk Operations: Uses bulk copy for activity logging
Optimization Tips
| Scenario | Recommendation | Rationale |
|---|---|---|
| Many criteria (10+) | Run as nightly batch job | Avoids running during business hours when users are actively working |
| Large item counts (10K+) | Use specific CriteriaKey when testing | Reduces scope during development/debugging |
| AutoUnassign = true | Expect additional overhead | Requires fetching all possible items twice (once with status filter, once without) |
| Custom property sorting | Ensure custom property filters are present | Task automatically adds filters for custom property sorts to improve performance |
| High-volume criteria | Monitor ActivityLog growth | Each assignment/unassignment creates activity logs |
Important Notes
- Execution Time: Scales with: (number of criteria) × (items per criteria) × (assignees per criteria)
- Incremental Updates: The task intelligently detects new items and removed items, only logging changes
- Concurrency Safety: Safe to run concurrently for different criteria keys, but avoid running the same criteria key simultaneously
- Workqueue Creation: Automatically creates workqueues for new assignees added to existing criteria
Error Handling
The task uses a continue-on-error approach with comprehensive logging:
// From Assigner.cs - logs errors but continues processing other criteria
foreach (WorkqueueAssignmentCriteria criteria in criterias)
{
if (criteria.Enabled)
{
logger.Log($"Processing criteria: {criteria.Name} ({criteria.Key})");
// ... processing logic ...
}
else
{
logger.Log($"Enabled set to false for criteria: {criteria.Name} ({criteria.Key}).");
}
}
Important Error Behaviors:
- Disabled Criteria: Skipped with log message, no error thrown
- Missing Criteria: If CriteriaKey specified but not found, logs warning and exits gracefully
- Empty Results: No items matching criteria is not an error - stack is cleared
- Failed Assignee Workqueues: If workqueue creation fails for one assignee, others still process
- Activity Logging: All activity logs are batched and written at the end of each phase (unassignment, then assignment)
Common Error Scenarios:
- Criteria not found for specified key → Logs "Criteria not found for key: {key}" and exits
- No enabled criteria → Logs "No criterias to assign for. Exiting..." and exits gracefully
- Search failure for criteria → Exception propagated, task stops
- Workqueue save failure → Exception propagated, task stops
Common Pitfalls
| Issue | Problem | Solution |
|---|---|---|
| Items not appearing in workqueue | Items are in the stack (WorkqueueCriteriaItemMap) but not fetched to workqueue | This is expected behavior - users must "fetch" from the UI to pull items from stack to their workqueue |
| AutoUnassign removes items immediately | Items removed from workqueue as soon as they no longer match criteria | Set AutoUnassign = false if you want items to remain in workqueue once assigned |
| Duplicate workqueues for users | Multiple workqueues created for same user/criteria combination | Known issue (TODO in code) - currently uses .First() to handle this |
| Performance degradation with many criteria | Task slows down with 20+ criteria | Run during off-hours, or use CriteriaKey to process specific criteria incrementally |
| Custom property sorts fail | Sort by custom property doesn't work | Ensure the custom property is also included as a filter in the criteria search terms |
| Items never removed from stack | Old items remain in WorkqueueCriteriaItemMap indefinitely | Ensure AutoUnassign is enabled, or manually clear stacks before reassignment |
| ActivityLog table growth | ActivityLog table grows large over time | Implement archival or retention policy for ActivityLog records |
Code References
- Task Definition:
MDClarityCore/Backend/Workqueues/Tasks/WorkqueueAssignmentTask.cs:11 - Task Field Definition:
MDClarityCore/Backend/Workqueues/Tasks/WorkqueueAssignmentTask.cs:52 - Execute Method:
MDClarityCore/Backend/Workqueues/Tasks/WorkqueueAssignmentTask.cs:20 - Assigner Interface:
MDClarityCore/Backend/Workqueues/IAssigner.cs:7 - Assigner Implementation:
MDClarityCore/Backend/Workqueues/Assigner.cs:18 - AssignAll Method:
MDClarityCore/Backend/Workqueues/Assigner.cs:45 - UnassignForCriteria Method:
MDClarityCore/Backend/Workqueues/Assigner.cs:206 - AssignItemsToStack Method:
MDClarityCore/Backend/Workqueues/Assigner.cs:244 - WorkqueueCriteriaSearch:
MDClarityCore/Backend/Workqueues/WorkqueueCriteriaSearch.cs:14 - WorkqueueAssignmentCriteria Model:
MDClarityCore/Models/Entities/WorkqueueAssignmentCriteria.cs:8 - Integration Tests:
MDClarityCore/MDClarityTest/Tests/IntegrationTests/Backend/Workqueues/WorkqueueAssignmentIntegrationTests.cs:26