We have case assignment rule defined and we want to make process data driven. Is there a way we can modify case assignment rule with some apex coding?
This can be done
You only get control before assignment rules run in before/after triggers so, you need to do the following
Option 1 (assumes you may want to keep some assignment rules)
- Create a new assignment rule that assigns to a queue called 'Pending Assignment Q'. Make it first in the sequence of rules
- Create a custom Boolean field on Case called
- Make the entry criteria for this assignment rule :
is_custom_assignment__c = true
- Create before insert trigger logic so that
is_pending_assignment__cis set to true if the new Case requires custom assignment logic. If it does not, then do nothing. Assignment rules 2...n will execute per normal.
- In before update trigger (which will be fired because the assignment rules will update the
Case.OwnerId), see if case is owned by
Pending Assignment Q. If yes, then apply your custom assignment logic. If not, do nothing.
Option 2 (assumes all assignment rules are custom)
Simply don't use Case Assignment rules and let your before insert trigger do all custom assignment by setting
Assignment rules are metadata, and cannot be altered directly via Apex (cropredy's answer is a workable alternative).
As far as I know, they can only be altered via the Metadata API, which you also can't use directly via Apex.
You should be able to access the Metadata API by using FinancialForce's Apex Metadata API Wrapper.
In the end, this is a horribly complex and convoluted way to go about things, and is likely to end in tears. I wouldn't recommend this approach to anyone. If cropredy's first solution doesn't work for you, a trigger is the appropriate tool for the job.