Check if the value of a CustomField was changed in a validator (ScriptRunner)

Justin Volden July 20, 2017

I have a CustomField that is of a multiline Wiki rendered type. I want to validate that the default value (set in a project scope on the field) has been changed/edited/added to to allow a specific transition. how can I do this using a ScriptRunner validator, or other ScriptRunner utility?

1 answer

1 vote
Jenna Davis
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
July 20, 2017

Hello, 

You would just need to set up a simple scripted validator on the transition to do this. 

Something like this within the condition should work:

cfValues["Your Field's Name"] != 'The Default Value'

As long as the default value is the same between different issues, this should work fine. You should also be able to set up a behaviour on your workflow transition to do something similar if you'd prefer to do that.  

Hope this helps! Let me know if you have any questions.

Regards, 

Jenna

Justin Volden July 20, 2017

I did try that, but it always resulted in a false for some reason. I'm wondering if it is because there is wiki markup in the default value, or if I am perhaps not handling multiple lines/line breaks correctly. 

Jenna Davis
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
July 24, 2017

You'll have to include the wiki markup characters (like, h1 or {quote}) inside of your default value. 

The newline characters are likely also throwing it off, you might be able to use something like:

System.getProperty("line.separator")

To make sure you're getting the correct newline charater. I have not tested this though. You'll just have to play around with the formating of the default value.

 

Justin Volden July 25, 2017

Here is where I am at:

 

ScriptRunner workflow function - Simple scripted validator (condition apply).
import org.apache.log4j.Logger
import org.apache.log4j.Level
  
def log = Logger.getLogger("com.acme.CreateSubtask")
log.setLevel(Level.DEBUG)


String defaultText =  "h1. Change Plan" + System.getProperty("line.separator") + "----" + System.getProperty("line.separator") + 
    "||Step||Estimated start time||Duration||Activity||Implementer||Expected Result||Risk Level||" + System.getProperty("line.separator") + "| | | | | | | |" + System.getProperty("line.separator") +
    "----" + System.getProperty("line.separator") + "h1. Rollback Plan" + System.getProperty("line.separator") + "----" + System.getProperty("line.separator") + "<insert plan text here>"

String currentText = cfValues["Change Plan"] if(currentText != defaultText) { log.debug "True" log.debug currentText log.debug defaultText return true } else { log.debug "False" log.debug currentText return false }

 Here is the log output:

017-07-25 11:24:44,283 DEBUG [acme.CreateSubtask]: True
2017-07-25 11:24:44,283 DEBUG [acme.CreateSubtask]: h1. Change Plan ---- ||Step||Estimated start time||Duration||Activity||Implementer||Expected Result||Risk Level|| | | | | | | | | ---- h1. Rollback Plan ---- <insert plan text here>
2017-07-25 11:24:44,284 DEBUG [acme.CreateSubtask]: h1. Change Plan ---- ||Step||Estimated start time||Duration||Activity||Implementer||Expected Result||Risk Level|| | | | | | | | | ---- h1. Rollback Plan ---- <insert plan text here>

 

It's still resolving that the text does not match. Not sure where to go to figure out what throwiung this off. 

 

Any thoughts of what I could try to determine what difference it is finding? Thank you!

Justin Volden July 25, 2017

I ended up solving this by tokenizing the strings. After that the comparison seems to be working as expected:

 

String defaultText = "h1. Change Plan" + System.getProperty("line.separator") + "----" + System.getProperty("line.separator") + 
"||Step||Estimated start time||Duration||Activity||Implementer||Expected Result||Risk Level||" + System.getProperty("line.separator") + "| | | | | | | |" + System.getProperty("line.separator") +
"----" + System.getProperty("line.separator") + "h1. Rollback Plan" + System.getProperty("line.separator") + "----" + System.getProperty("line.separator") + "<insert plan text here>"
defaultText = defaultText.tokenize()

String currentText = cfValues["Change Plan"]
currentText = currentText.tokenize()

currentText != defaultText

 

Oddly enough, if I print both tokenized strings to the log they look identical, but it works so.... 

Jenna Davis
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
July 26, 2017

I'm glad you got it working! :)

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events