Set approval status in post-save script

Description
DCwom
Posts: 36
Joined: Mon Sep 29, 2014 7:00 pm

Set approval status in post-save script

Postby DCwom » Fri Oct 24, 2014 7:53 pm

I'm trying to reset the approved status of approvers on a workitem to waiting with a post-save script, but I can't get the right syntax or methods to do this. I've tried the following code with no luck.

var workitem_current = workitem_history.get(workitem_history.size() - 1);
var approvals = workitem_current.getApprovals();
for (var i = 0; i < approvalsSize; i++) { // Loops walk through list of approvers and look for "approved" approval states.
var approver = approvals.get(i);
var user = approver.getUser();
if(approver.status.id == "approved") {
//workItem.setApprovalStatus(user, OPT_WAITING);
//workItem.setApprovalStatus.user.OPT_WAITING;
//workItem.setApprovalStatus.setStatus(OPT_WAITING);
workItem.setApprovalStatus.setUser(user);
//workItem.setApprovalStatus.setStatus(OPT_WAITING);
//workItem.removeApprovee(user);
//workItem.addAprovee(user);
}
}

The last two commented lines (add/RemoveAprovee didn't do anything but did not fail when ran).

Does anyone see what I'm doing wrong?

martins
Posts: 181
Joined: Thu Nov 05, 2009 3:24 pm

Re: Set approval status in post-save script

Postby martins » Mon Oct 27, 2014 7:19 am

With "workItem.getApprovals()" you get a Collection of IApprovalStruct. This struct holds the user and the status of an approval.

When you iterate over this collection you could check status for each user, e. g.:

Code: Select all

for (var i = approvals.iterator(); i.hasNext();) {
   var approval = i.next();
   if(approval.getStatus().getId()=="approved") {
      // do something....
   }
}

To change the approval state you have to use method "setStatus" (not "setUser"!).

Btw. if you intend to do this at a status change you better use workflow function "ResetApprovalsState". This requires just a few mouse clicks to become active and no programming at all.

Martin

DCwom
Posts: 36
Joined: Mon Sep 29, 2014 7:00 pm

Re: Set approval status in post-save script

Postby DCwom » Mon Oct 27, 2014 1:39 pm

martins wrote:To change the approval state you have to use method "setStatus" (not "setUser"!).

Btw. if you intend to do this at a status change you better use workflow function "ResetApprovalsState". This requires just a few mouse clicks to become active and no programming at all.

Martin


Martin thanks for the help with the iterator. I still can't seem to set the status though, I think I'm stuck on the enum syntax I just can't get it to compile. I've tried:
approval.setStatus("waiting");
approval.setStatus(OPT_NUM);
approval.setStatus = OPT_WAITING;
approval.setStatus = "waiting";

Dennis

martins
Posts: 181
Joined: Thu Nov 05, 2009 3:24 pm

Re: Set approval status in post-save script

Postby martins » Mon Oct 27, 2014 2:05 pm

Hello Dennis,

I think

Code: Select all

approval.setStatus( workItem.getProject().getApprovalStatusEnum().wrapOption("waiting") );
should do what you are looking for.

Martin

DCwom
Posts: 36
Joined: Mon Sep 29, 2014 7:00 pm

Re: Set approval status in post-save script

Postby DCwom » Mon Oct 27, 2014 6:30 pm

Martin thanks for the help, I was way off with the enum thing. I'll post my working code in case someday someone is looking for something similar.

Code: Select all

// This is a post.save script that resets approvals to waiting if the title or description are changed.
// CAVET: The script only works after the 2nd edit of the work item.
var returnvalue = "";
var commentFlag = false;
try {
   //Get history for the workITEM to get its previous parameters
   var workitem_history = trackerService.getProjectsService().getDataService().getObjectHistory(workItem);  // Get the previous history to compare
   var workitem_current =  workitem_history.get(workitem_history.size() - 1); // To find most recent WI parameters decrement by 1   
   var workitem_previous = workitem_history.get(workitem_history.size() - 2); // To find previous WI need to decrement by 2 instead of 1 because this is post-save
   var approvals = workitem_current.getApprovals();
   var approvalsSize = approvals.size();
      if (approvalsSize != 0){                                    // If no approvals are set don't do anything      
         var wiDescription = workitem_current.getDescription();  // set to variable so the if statement isn't insanely long
         var owiDescription = workitem_previous.getDescription();
         // We can't use the .equals check for the title because it isn't a text object like the description
         if ((!wiDescription.equals(owiDescription)) || (workitem_current.getTitle() != workitem_previous.getTitle())) {  // Check the title & description for changes
            newWI = workItem.getApprovals();                  // Use the current workItem to make updates to the statuses   
            for (var i = newWI.iterator(); i.hasNext();) {
               var approval = i.next();
               if(approval.getStatus().getId()=="approved") {  // Find any approved & change them to waiting, leave disapproved alone
                  approval.setStatus( workItem.getProject().getApprovalStatusEnum().wrapOption("waiting") );
                  var commentFlag = true;
               }
            }
         }
         if (commentFlag) { // if any comments were reset then add comment to the WIs comment section
            var comment = ("Approvals have been set back to waiting due to title or description change");
            var title =  "NOTICE: Approvals set back to Waiting";
            workItem.createComment(com.polarion.core.util.types.Text.plain(comment),title,null).save()         
         }
      }
} catch (e) {
//var comment = "NO HISTORY was found on save";  //For debugging create a comment for the workITEM
//var title =  "NO HISTORY";
//workItem.createComment(com.polarion.core.util.types.Text.plain(comment),title,null).save()
}
workItem.save();   // Have to save changes since this is a post save script
returnvalue;


Return to “Polarion Application Lifecycle Management (ALM)”

Who is online

Users browsing this forum: No registered users and 8 guests