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

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

0 votes
Jenna Davis Community Champion Jul 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

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 Community Champion Jul 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.

 

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!

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 Community Champion Jul 26, 2017

I'm glad you got it working! :)

Suggest an answer

Log in or Sign up to answer
Community showcase
Published Nov 29, 2018 in Marketplace Apps

How to set up an incident workflow from the VP of Engineering at Sentry

Hey Atlassian community, I help lead engineering at Sentry, an open-source error-tracking and monitoring tool that integrates with Jira. We started using Jira Software Cloud internally last year, a...

1,128 views 0 8
Read article

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you