The following code examples demonstrate task-related operations. For an overview of task function, see Tasks.
Task instances can be created only from subclasses of the base CmTask class. The following Java™ and C# examples show how to create a CmTask subclass. Two of the system property definitions of the subclass are set with default values, and a custom property definition is added.
Java Example
// Fetch base task class definition from the server.
ClassDefinition objClassDef = Factory.ClassDefinition.fetchInstance(store, ClassNames.CM_TASK, null);
// Create subclass of the CmTask class.
ClassDefinition objSubClassDef = objClassDef.createSubclass();
// Set up locale and name subclass.
LocalizedString objLocStr = Factory.LocalizedString.createInstance();
objLocStr.set_LocalizedText("Flood Claim Task Subclass");
objLocStr.set_LocaleName(store.get_LocaleName());
objSubClassDef.set_DisplayNames(Factory.LocalizedString.createList());
objSubClassDef.get_DisplayNames().add(objLocStr);
objSubClassDef.set_SymbolicName("fcTaskSubclass");
// Set default values on PreCondition and PostCondition property definitions.
PropertyDefinitionList objPropDefs = objSubClassDef.get_PropertyDefinitions();
ListIterator iter = objPropDefs.listIterator();
PropertyDefinitionString objPropDef = null;
String objPropDefSymbolicName = null;
// Iterate property definitions until PreCondition and PostCondition found.
for (int i=0; i < objPropDefs.size(); i++)
{
objPropDefSymbolicName = ((PropertyDefinition) objPropDefs.get(i)).get_SymbolicName();
if (objPropDefSymbolicName.equalsIgnoreCase("PreCondition"))
{
objPropDef = (PropertyDefinitionString) objPropDefs.get(i);
Property prop = objPropDef.getProperties().get("PropertyDefaultString");
prop.setObjectValue("Coordinator SATISFIES (VersionStatus=1)" );
}
if (objPropDefSymbolicName.equalsIgnoreCase("PostCondition"))
{
objPropDef = (PropertyDefinitionString) objPropDefs.get(i);
Property prop = objPropDef.getProperties().get("PropertyDefaultString");
prop.setObjectValue("Coordinator SATISFIES (ClaimStatus='CLOSED')" );
}
}
/* Add existing custom property definition to the subclass.
* The intent of the property definition is to name an instance of the subclass. */
PropertyTemplateString objPropTemplate = Factory.PropertyTemplateString.fetchInstance
(store, new Id("{7C6FF644-285F-44DD-B7B7-5A6080CE1291}"), null);
// Create property definition from property template.
objPropDef = (PropertyDefinitionString)objPropTemplate.createClassProperty();
// Add new property definition to subclass.
objPropDefs.add(objPropDef);
// Save task subclass to the server.
objSubClassDef.save(RefreshMode.REFRESH);
C# Example
// Fetch base task class definition from the server.
IClassDefinition objClassDef = Factory.ClassDefinition.FetchInstance(store,
GuidConstants.Class_CmTask, null);
// Create subclass of the ICmActivity class.
IClassDefinition objSubClassDef = objClassDef.CreateSubclass();
// Set up locale and name subclass.
ILocalizedString objLocStr = Factory.LocalizedString.CreateInstance();
objLocStr.LocalizedText = ("Flood Claim Task Subclass");
objLocStr.LocaleName = store.LocaleName;
objSubClassDef.DisplayNames = Factory.LocalizedString.CreateList();
objSubClassDef.DisplayNames.Add(objLocStr);
objSubClassDef.SymbolicName = "fcTaskSubclass";
// Set default values on PreCondition and PostCondition property definitions.
IPropertyDefinitionList objPropDefs = objSubClassDef.PropertyDefinitions;
String objPropDefSymbolicName = null;
// Iterate property definitions until PreCondition and PostCondition found.
foreach (IPropertyDefinition objPropDef in objPropDefs)
{
objPropDefSymbolicName = objPropDef.SymbolicName;
if (objPropDefSymbolicName.Equals("PreCondition"))
{
IProperty prop = objPropDef.Properties.GetProperty("PropertyDefaultString");
prop.SetObjectValue("Coordinator SATISFIES (VersionStatus=1)");
}
if (objPropDefSymbolicName.Equals("PostCondition"))
{
IProperty prop=objPropDef.Properties.GetProperty("PropertyDefaultString");
prop.SetObjectValue("Coordinator SATISFIES (ClaimStatus='CLOSED')" );
}
}
/* Add existing custom property definition to the subclass.
* The intent of the property definition is to name an instance of the subclass. */
IPropertyTemplateString objPropTemplate = Factory.PropertyTemplateString.FetchInstance(
store, new Id("{7C6FF644-285F-44DD-B7B7-5A6080CE1291}"), null);
// Create property definition from property template.
IPropertyDefinitionString objPropDefString = (IPropertyDefinitionString)objPropTemplate.CreateClassProperty();
// Add new property definition to subclass.
objPropDefs.Add(objPropDefString);
// Save task subclass to the server.
objSubClassDef.Save(RefreshMode.REFRESH);
The following Java and C# examples show how to create an instance from a task subclass.
Java Example
// Create instance from task subclass created in previous example.
CmTask task = Factory.CmTask.createInstance(store, "FloodClaimTaskSubclass");
// Set the Coordinator property with a Document object.
Document activityCoordinator = Factory.Document.getInstance(store, ClassNames.DOCUMENT,
new Id("{486F9C7F-DD8C-4EB9-A7CC-98A3A4DAD1EB}"));
task.set_Coordinator(activityCoordinator);
// Set custom property for naming the instance.
task.getProperties().putValue("taskName", "Flood Claim Task Instance");
// Save task instance to the server.
task.save(RefreshMode.REFRESH);
C# Example
// Create instance from task subclass created in previous example.
ICmTask task = Factory.CmTask.CreateInstance(store, "FloodClaimTaskSubclass");
// Set the Coordinator property with a Document object.
IDocument activityCoordinator = Factory.Document.GetInstance(store, ClassNames.DOCUMENT,
new Id("{486F9C7F-DD8C-4EB9-A7CC-98A3A4DAD1EB}"));
task.Coordinator = activityCoordinator;
// Set custom property for naming the instance.
task.Properties["taskName"] = "Flood Claim Task Instance";
// Save task instance to the server.
task.Save(RefreshMode.REFRESH);
The following Java and C# examples show how to promote the state of a task instance.
Java Example
// Fetch task instance created in previous example.
CmTask task = Factory.CmTask.fetchInstance(store,
new Id("{D0544111-24FC-4B9E-A905-DFFDBEC733B7}"), null);
// Print current state, promote task, print new state.
try
{
System.out.println("Current state is " + task.get_TaskState().toString());
task.changeState(LifecycleChangeFlags.PROMOTE);
task.save(RefreshMode.REFRESH);
System.out.println("New state is " + task.get_TaskState().toString());
}
catch (Exception e)
{
System.out.println("Error: " + e.getMessage());
}
C# Example
// Fetch task instance created in previous example.
ICmTask task = Factory.CmTask.FetchInstance(store,
new Id("{4B8AEBA2-8F3F-4010-AAAC-6BFF64695677}"), null);
// Print current state, promote task, print new state.
try
{
System.Console.WriteLine("Current state is " + task.TaskState);
task.ChangeState(LifecycleChangeFlags.PROMOTE);
task.Save(RefreshMode.REFRESH);
System.Console.WriteLine("New state is " + task.TaskState);
}
catch (Exception e)
{
System.Console.WriteLine("Error: " + e.Message);
}