ClassCastException into WorkflowScriptMethods sample.

Document ID : KB000008052
Last Modified Date : 14/02/2018
Show Technical Document Details
Issue:

This issue came for IM r14.1 with a custom workpoint code involved in a custom workpoint process triggered by an admin modify user task.

This could be reproduced using the OOTB

"IAM Suite\Identity Manager\tools\samples\WorkflowContext\ApprovalDecision\WorkflowScriptMethods.java"
triggered by the provided two OOTB scripts: "Is manager" and "Is not manager".

IsManager.png

IsNotManager.png

 

You can customize a simple WP process based on the OOTB SingleStepApproval one, by just adding an unconditional node after the Start point with the 2 transitions "Is manager" (com.ca.identitymanager.samples.WorkflowScriptMethods.approvalNotRequired) and
"Is not manager" (com.ca.identitymanager.samples.WorkflowScriptMethods.approvalRequired).

MyWPprocess.jpg

If you set the workflow process to be involved by instance at the modify user event level then no issue.


If you set the workflow process to be involved at the Admin task level as following then you get the issue:

TaskLevelConfig.jpg

TaskLevelDefinition.jpg
The following statement:
User user = ((UserEvent)evt).getUser();
fails with:
java.lang.ClassCastException: com.netegrity.ims.events.IMTaskEvent cannot be cast to com.netegrity.imapi.UserEvent

When the workflow process is involved at the Admin task level, the original part of problematic code is as following:

IMEvent evt= workflowContext.getEvent();
User user = ((UserEvent)evt).getUser();
String title = user.getAttribute("title");

 

Environment:
Observed with IM r12.6.X and r14.1.0.
Cause:

The following code line:

IMEvent evt= workflowContext.getEvent();
does not retrieve an instance of UserEvent in our case but an instance of IMTaskEvent.

This explains why the following code line:

User user = ((UserEvent)evt).getUser();

fails with java.lang.ClassCastException: com.netegrity.ims.events.IMTaskEvent cannot be cast to com.netegrity.imapi.UserEvent.

Resolution:

The goal is to retrieve the managed user object when the instance is of an IM task or in other terms when the workflow process is involved at the task level instead of at the event level.

The original following 2 lines in the sample code:

User user = ((UserEvent)evt).getUser();
String title = user.getAttribute("title");

need to be replaced by the following code:
 User user = null;
 String title = null;
 ManagedObject mo = null;
  if(evt instanceof UserEvent)
  {
   user = ((UserEvent)evt).getUser();
   title = user.getAttribute("title");
  }
  else if(evt instanceof IMTaskEvent)
  {
   mo = ((IMTaskEvent)evt).getPrimaryManagedObject();
   if(mo.get("title") != null && !mo.get("title").equals(""))
   {
    title = (String) mo.get("title");
   }
  }

 

Based on our WorkflowScriptMethods.java sample this change needs to be done twice in both approvalRequired and approvalNotRequired methods.