cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 

Community Tip - Need to share some code when posting a question or reply? Make sure to use the "Insert code sample" menu option. Learn more! X

Workflow - How to Throw Warning Message to User

marshall_brock
6-Contributor

Workflow - How to Throw Warning Message to User

Version: Windchill 12.1

 

Use Case: I'm attempting to throw a warning message to users when they hit "Complete Task" from a workflow activity when specific criteria is hit, with the goal being that the message is thrown once and then can be bypassed.


Description:

I have created a class on the application server called WorkflowHelper that has a private instance variable that gets initialized (if not already) whenever any workflow starts. It has a HashMap associated with it that stores the Change Task number as a key with a boolean "warnings thrown" value to indicate whether the warning message has been thrown once or not. 

 

The issue is that the code executed during the workflow transitions to set the boolean value to "true" once the warning is thrown is not saved to memory until either the workflow is terminated or the activity is completed. This causes the warning to be continually thrown during the activity and never properly updated, and also prevents the user from bypassing the warning, effectively making it an error. 

 

For reference, psuedo code from the activity in question is outlined below.

START Transition:

//gets instance of workflow helper and sets warnings thrown to false when activity is started

ext.customer.workflow.WorkflowHelper wfh = ext.customer.workflow.WorkflowHelper.getInstance();

wfh.setWarningsThrownFalse(primaryBusinessObject);

 

COMPLETE TASK Transition:

//get instance of workflowhelper

ext.customer.workflow.WorkflowHelper wfh = ext.customer.workflow.WorkflowHelper.getInstance();

//return string containing the warning message if business rule criteria is not met, takes PBO and boolean input of whether warnings have been thrown or not. Boolean is never properly updated to "true" until workflow is terminated or activity completes. 

String warningMessage = ext.customer.workflow.WorkflowHelper.executeBusinessRules(primaryBusinessObject, wfh.getWarningsThrown(primaryBusinessObject));

//if warning message isn't empty, throw message to user. it will return empty if boolean input value is true. 

if ( ! warningMessage.isEmpty() ) {

  wfh.setWarningsThrownTrue(primaryBusinessObject);

  throw new WTException(warningMessage);

8 REPLIES 8

Hi @marshall_brock 

I don't understand your question maybe. But have you tried to save the Boolean result to workflow variable? This way you can save the result and check if you need it, can't you?

.PetrH

I have tried using a workflow variable, both global and local to the activity. Perhaps it's a bug, but whenever I set the workflow variable and then throw the exception, the variable value is reverted. The activity transition code looked like the below, with "warningThrownVariable" being an example of the workflow variable.

 

START Transition:

//set warnings thrown variable to false at start of activity

warningThrownVariable = false;

 

COMPLETE TASK Transition:

//get warning message

String warningMessage = ext.customer.workflow.WorkflowHelper.executeBusinessRules(primaryBusinessObject);

//if warnings haven't been thrown, set warning thrown variable to true and throw warning message

if ( ! warningThrownVariable ) {

//this line to set warning thrown variable to true never seems to commit to memory  

warningThrownVariable = true;

  throw new WTException(warningMessage);

I might be missing something as well, but why not just use a workflow variable? You can initialize a Boolean in the workflow without needing to extend any classes or any of that. You can easily update the Boolean at any point in the workflow. 

Copied from my reply to the other response.

 

I have tried using a workflow variable, both global and local to the activity. Perhaps it's a bug, but whenever I set the workflow variable and then throw the exception, the variable value is reverted. The activity transition code looked like the below, with "warningVariable" being an example of the workflow variable.

 

START Transition:

//set warnings thrown variable to false at start of activity

warningThrownVariable = false;

 

COMPLETE TASK Transition:

//get warning message

String warningMessage = ext.customer.workflow.WorkflowHelper.executeBusinessRules(primaryBusinessObject);

//if warnings haven't been thrown, set warning thrown variable to true and throw warning message

if ( ! warningThrownVariable ) {

//this line to set warning thrown variable to true never seems to commit to memory  

warningThrownVariable = true;

  throw new WTException(warningMessage);

Ah, sorry I missed that other comment and response.

 

If I'm following your code, you always start with warningThrownVariable = false.

Somewhere else (not shown) it MAY get set to true.

But then if the variable is still false during task completion, you want to always set it to true?

 

Here's a couple of simple things to check (you probably already have done):

  • Check Syntax. Make sure there are no compile errors
  • Add system print lines in the if statement then check the logs to make sure the if statement is executing when you expect it to.
  • Make sure you've added the warningThrownVariable to the Workflow. 
  • Make sure you've added the warningThrownVariable to the Task, and that you've set it to Initialize From and Copy Into the Workflow variable

 

The variable is set to false in the "Start" transition of the task, so that the warning is thrown at least once per task. It is not reset again. It is shown being set to true in the "Complete Task" transition in my code prior to the exception being thrown. The goal is to throw the warning only once and allow for task completion afterwards, so I set it to true and then throw the exception. 

 

There is no issue with the syntax, the task runs and executes fine. The only issue is that the "warningThrownVariable" is never permanently set to true for some reason and that results in the exception being thrown every single time a user attempts to complete the task. 

That reminds me of something we did in a workflow recently. 

We made a task where the user was asked to confirm something. We wanted to try to make sure that they read the page and didn't just click "complete task." So we added a boolean variable in the task and added validation to require them to check the box for the workflow to proceed. 

Maybe that would get you close to what you need? Display the warning until the user checks the box. Once they have checked the box on the task, the warning won't appear again. 

 

That might not be exactly what you need, but hope it gives you some ideas.

This is exactly what we ended up having to settle on a few weeks ago. It's not ideal because the users can check the box and advance the workflow without ever seeing the warning thrown, but given the issues I've outlined, it's all we could do for now. 

 

I appreciate the suggestions nonetheless. 

Announcements


Top Tags