// Called OnActivate from the Work Request activity of sm_NDR. This will assign the drafter selected by the Drafting Lead.
Innovator inn = this.getInnovator();
string actId = this.getID();
// Retrieve the workflow item
Item wflItem = this.newItem("Workflow","get");
wflItem.setAttribute("select","source_id,source_type");
Item wflProc = wflItem.createRelatedItem("Workflow Process","get");
wflProc.setAttribute("select","name");
Item wflProcAct = wflProc.createRelationship("Workflow Process Activity","get");
wflProcAct.setAttribute("select","related_id");
wflProcAct.setProperty("related_id",this.getID());
wflItem = wflItem.apply();
if (wflItem.getItemCount() != 1 || wflItem.getProperty("source_id","").Length != 32 || wflItem.getPropertyAttribute("source_type","keyed_name","").Length < 1)
{
return inn.newError("Error retrieving workflow: "+wflItem.getErrorDetail());
}
//GET the Drafter from the NDR
Item ecoParent = this.newItem("sm_NDR", "get");
ecoParent.setID(wflItem.getProperty("source_id"));
ecoParent.setAttribute("select","drafter");
ecoParent=ecoParent.apply();
bool permissionWasSet = false;
Aras.Server.Security.Identity transIdentity = Aras.Server.Security.Identity.GetByName("Aras PLM");
try
{
permissionWasSet = Aras.Server.Security.Permissions.GrantIdentity(transIdentity);
//GET the Identity from the User
Item ecoDrafter = this.newItem("User","get");
ecoDrafter.setID(ecoParent.getProperty("drafter"));
ecoDrafter.setAttribute("select","owned_by_id");
ecoDrafter=ecoDrafter.apply();
Item getAssignment = this.newItem("Activity Assignment", "get");
getAssignment.setProperty("source_id", actId);
getAssignment.setAttribute("select", "id");
getAssignment = getAssignment.apply();
// This deletes the Identities for the Activity if the Workflow had to be stepped backwards (i.e. rework the document)
if(!getAssignment.isError())
{
for (int i = 0; i < getAssignment.getItemCount(); i++)
{
var assignmentId = getAssignment.getItemByIndex(i).getID();
Item removeAssignment = this.newItem("Activity Assignment", "delete");
removeAssignment.setAttribute("id", assignmentId);
removeAssignment.setAttribute("doGetItem", "0");
removeAssignment = removeAssignment.apply();
if(removeAssignment.isError())
{
return inn.newError("Error removing assignment: "+removeAssignment.getErrorDetail());
}
}
}
// Add the Drafter to the Activity Assignment
Item assignment = this.newItem("Activity Assignment", "");
assignment.setAction("add");
assignment.setPropertyAttribute("locked_by_id","is_null","1");
assignment.setProperty("source_id", this.getID());
string drafter = ecoDrafter.getProperty("owned_by_id"); // Get Drafter Identity ID
assignment.setProperty("related_id", drafter);
assignment.setProperty("voting_weight", "100");
Item result = assignment.apply();
if (result.isError())
{
return inn.newError("Workflow Assignment: Error adding assignment: " + result.getErrorDetail());
}
}
finally
{
if (permissionWasSet)
{
Aras.Server.Security.Permissions.RevokeIdentity(transIdentity);
}
}
return this;