Often a workflow can be arranged to be automated based on internal values.
Sometimes there are situations where you need to respond after a time or based on some other external stimuli.
In this case we can vote for a workflow from a server method.
The following code allows you to find and vote for a workflow activity.
This code is intended to be run from a scheduled server method to advance a workflow activity along the "Release" path after the "effective_date" has been reached.
Since the method would normally be run without user interaction I have not returned errors or stopped the process on error. In the actual code this is implemented in there is error reporting that I have removed from here for simplicity.
Innovator inn =
this.getInnovator();
DateTime
dt = DateTime.Now;
string
targetDate = String.Format("{0:s}",dt);
DateTime
dtNow = DateTime.UtcNow;
string
tmpPtrn = "yyyy-MM-ddTHH:mm:ss";
string
tmpDt = dtNow.ToString(tmpPtrn, DateTimeFormatInfo.InvariantInfo);
I18NSessionContext sCntxt = inn.getI18NSessionContext();
string
now = sCntxt.ConvertUtcDateTimeToNeutral(tmpDt, tmpPtrn);
//now = sCntxt.ConvertFromNeutral(now,"date",tmpPtrn);
Item mcrs =
this.newItem("MyItem","get");
mcrs.setProperty(
"effective_date",now);
mcrs.setPropertyCondition(
"effective_date","le");
mcrs.setProperty(
"state","Pending Release");
mcrs = mcrs.apply();
if
( mcrs.isEmpty() ){
// there are no qualifying MyItems.
return this;
}
// Build the voting request
StringBuilder voteXml =
new StringBuilder("");
voteXml.Append(
"<Item type=\"Activity\" action=\"EvaluateActivity\">");
voteXml.Append(
" <Activity>{0}</Activity>");
voteXml.Append(
" <ActivityAssignment>{1}</ActivityAssignment>");
voteXml.Append(
" <Paths>");
voteXml.Append(
" <Path id=\"{2}\">{3}</Path>");
voteXml.Append(
" </Paths>");
voteXml.Append(
" <DelegateTo>0</DelegateTo>");
voteXml.Append(
" <Tasks />");
voteXml.Append(
" <Variables />");
voteXml.Append(
" <Authentication mode=\"\" />");
voteXml.Append(
" <Comments>{4}</Comments>");
voteXml.Append(
" <Complete>1</Complete>");
voteXml.Append(
"</Item>");
// Grant 'Aras PLM' permissionsa
Aras.Server.Security.Identity plmIdentity = Aras.Server.Security.Identity.GetByName(
"Aras PLM");
Boolean
PermissionWasSet = Aras.Server.Security.Permissions.GrantIdentity(plmIdentity);
for
( int i = 0; i < mcrs.getItemCount(); i++){
Item mcr = mcrs.getItemByIndex(i);
Item wFlow =
this.newItem("Workflow","get");
wFlow.setAttribute(
"levels","3");
wFlow.setAttribute(
"select","related_id(*)");
wFlow.setProperty(
"source_id",mcr.getID());
wFlow.setPropertyCondition(
"related_id","is not null");
wFlow = wFlow.apply();
if ( wFlow.isEmpty()){
// there is no workflow associated with this MCR
// this is an abberation.
}
string activityID = "";
string assignID = "";
string pathID = "";
string vte = "Release";
string comment = "Released automatically as effective date has fallen due";
// Retrieve the "Pending Release" activity Node
try{
Item prActs = wFlow.getItemsByXPath(
"//Item[@type='Activity'][name='Pending Release']");
Item prAct = prActs.getItemByIndex(0);
activityID = prAct.getID();
Item actAssigns = prAct.getItemsByXPath(
"./Relationships/Item[@type='Activity Assignment']");
Item paths = prAct.getItemsByXPath(
"./Relationships/Item[@type='Workflow Process Path'][name='Release']");
for ( int x = 0; x < paths.getItemCount(); x++){
Item path = paths.getItemByIndex(x);
if ( path.getProperty("name") == "Release"){
pathID = path.getID();
}
}
for ( int j = 0; j < actAssigns.getItemCount(); j++){
Item actAssign = actAssigns.getItemByIndex(j);
if ( actAssign.getRelatedItem().getProperty("name") == "Aras PLM"){
assignID = actAssign.getID();
}
}
if ( assignID == "" ){
assignID = actAssigns.getItemByIndex(0).getID();
}
// Submit the vote
Item vote = inn.newItem();
vote.loadAML(String.Format(voteXml.ToString(),activityID,assignID,pathID,vte,comment));
vote = vote.apply();
if ( vote.isError() ){
// respond to an unsuccessful vote.
}
else {
// respond to a successful vote.
}
}
catch (Exception e){
}
}
if
(PermissionWasSet) Aras.Server.Security.Permissions.RevokeIdentity(plmIdentity);
return
inn.newResult(processErrors.ToString());