Out of the box, Task Manager supports one action - Create Subcase. This example walks through how to add a custom action for a task. Specifically, it will show how to add an action named Get Stock Quote
, that will call a web service to get a stock quote. The quote will be logged as a note to the case.
The code for this example is located in
src/Dovetail.TaskManagerExamples/TaskExample
.
We'll start by defining a class containing the information that we need to receive from Task Manager:
public class GetStockQuoteOptions
{
public string Symbol { get; set; }
}
This class will be constructed and bound from the values that are resolved through the Task Manager API
.
Setup
-
Add the custom Task Action
Using Dovetail Admin, edit the
Task Action
user-defined list. Add an element namedGet Stock Quote
-
Add the custom task Action parameters
- Using Dovetail Admin, add a new user-defined list named
Get Stock Quote Params
- Add an element to this list named
Symbol
- Using Dovetail Admin, add a new user-defined list named
-
Refresh Dovetail Agent's cache
Using Dovetail Agent: Admin - cache - Refresh Cache
Task Manager Setup
-
Setup Task Property Templates
Using Dovetail Agent: Admin - Task Manager - Task Property Templates tab, click the
+
in the tab header to add a new template with the following information:Attribute Value Name Stock Symbol Action Get Stock Quote Data Type String Save To Symbol Click the
Create
button.
-
Setup Task
Using Dovetail Agent: Admin - Task Manager - Tasks tab, click the
+
in the tab header to add a new task with the following information:Attribute Value Name Get Stock Quote Status Active Action Get Stock Quote Workflow None Click the
Create
button. -
Setup Task Properties
- From the
Get Stock Quote
task page, click the+
in the Properties tab header. - Select the
Stock Symbol
property - Set the value to Specific Property of
GOOG
- Click the Create button
- From the
-
Setup Task Set
Using Dovetail Agent: Admin - Task Manager - Task Sets tab, click the
+
in the tab header to add a new task set with the following information:Attribute Value Name Get Stock Quote Status Active Click the
Create
button. -
Add the Task to the Task Set
- From the
Get Stock Quote
task set page, click the+
in the Tasks tab header. - Select the
Get Stock Quote
Task - Click the Add button
- From the
Create the Task Runner Policy
Now we need to create a policy so that the Task Manager API
knows how to handle our new GetStockQuote Action
. We can do this by implementing the ITaskRunnerPolicy
interface. This interface has two different methods to implement: Matches
and Execute
.
Let's look at the GetStockQuotePolicy
:
public class GetStockQuotePolicy : ITaskRunnerPolicy
{
private const string Action = "Get Stock Quote";
private readonly IObjectBinder _binder;
private readonly IClarifyContext _clarify;
private readonly IStockQuoteService _quotes;
public GetStockQuotePolicy(IObjectBinder binder, IClarifyContext clarify, IStockQuoteService quotes)
{
_binder = binder;
_clarify = clarify;
_quotes = quotes;
}
public bool Matches(TaskValueCollection values)
{
return Action.Equals(values.Task.Action, StringComparison.InvariantCultureIgnoreCase);
}
public void Execute(string caseId, TaskValueCollection values)
{
var options = _binder.Bind<GetStockQuoteOptions>(values);
var toolkit = new SupportToolkit(_clarify.Session);
var quote = _quotes.FindBySymbol(options.Symbol);
var logCaseNoteSetup = new LogCaseNoteSetup(caseId)
{
Notes = string.Format("{0} ({1}) is currently at {2}", quote.Name, quote.Symbol, quote.Price),
GenerateTimeBombs = true
};
toolkit.LogCaseNote(logCaseNoteSetup);
}
}
Matches
In our implementation we reference the Action
property of the Task
. There can be any number of Tasks being executed so we want to make sure we're only matching on a Task that is configured for the Action
that we want. In this case, we're making sure that the Action
is Get Stock Quote
.
Execute
The first line of this method is the most notable. Using the IObjectBinder
interface, we ask for an instance of our GetStockQuoteOptions
class using the given TaskValueCollection
. The Task Manager API
will construct and bind the values of this object for us (resolving Clarify paths, coercing values, etc.).
Once we have the object constructed, we then simply log a note to the Case
.
Register the Policy
We need to register the new policy into our container. We can do this through a simple scanning operation:
public class ExampleRegistry : Registry
{
public ExampleRegistry()
{
Scan(_ =>
{
_.TheCallingAssembly();
_.WithDefaultConventions();
_.AddAllTypesOf<ITaskRunnerPolicy>();
});
}
}
Run the Task Set
- Dovetail Agent: open one of your cases
- Using the
Workflow
menu select:Run Task Set
- Select the
Get Stock Quote
task set. - Click the
Run Task Set
button - Case History Menu - Show Details
- You should see a new history item of
Rule Action
ofAction Send Message to Carrier of rule Run Dovetail Task Set fired
- You should see a new history item of
Note Logged
with text ofAlphabet, Inc. (GOOG) is currently at 709.75