Forums

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

Scriptrunner validator to check if custom field begins with certain text value

Deleted user September 23, 2019

I feel like this should be a simple task, but I can't find a good simple example anywhere as everyone else seems to be doing much more complex things and I am just getting started with Scriptrunner.

All I want to do is setup a transition validator which confirms that a custom field called "Service Account Username" begins with the characters "svc-" (without quotes).  The field is a single line text field.  If user enters anything else in the field except a string which begins with svc- then it would throw an error.

Ideally, this validator would only kick in for a specific request type called "New Service Account Request".

Thanks in advance.

1 answer

1 accepted

0 votes
Answer accepted
Antoine Berry
Community Champion
September 24, 2019

Hi @[deleted] ,

You can use this code in the validator : 

import com.atlassian.jira.component.ComponentAccessor
import com.opensymphony.workflow.InvalidInputException

int customFieldId = 13300
def customField = ComponentAccessor.getCustomFieldManager().getCustomFieldObject(customFieldId)
def customFieldValue = issue.getCustomFieldValue(customField)?:"" as String

int custReqFieldId = 10900
def custReqField = ComponentAccessor.getCustomFieldManager().getCustomFieldObject(custReqFieldId)
def custReqFieldValue = issue.getCustomFieldValue(custReqField)?:"" as String

def issueType = issue.getIssueType().getName()

log.error("Text customField value to check : " + customFieldValue)
log.error("issueType : " + issueType)
log.error("Other custom field value to check : " + custReqFieldValue)
log.error("Other custom field value class : " + custReqFieldValue.getClass())


if (!customFieldValue.startsWith("svc-") && issueType == "Service Request with Approvals" && custReqFieldValue.toString() == "opstest/89dc79df-4f65-4f9a-9c07-7de8112033ec"){
throw new InvalidInputException("customfield_"+customFieldId, "The Service Account Username field must start with svc-")
}

Antoine

Deleted user September 24, 2019

Antoine,

Thanks so much for your response.  I looked up the custom field ID for my Service Account Username field and it's 13300 so the only modification I made to your code was to replace the 12700 with 13300.  Are there any other modifications I needed to make?

One note:
I've created this validator on the Create Issue transition (the very start of the workflow before it hits the first status) as I assumed it would have to be caught right away. 

However, it's still not working as expected.  First question I have here, should I be creating this as a Simple Scripted Validator or a Custom Script Validator?

Note that I did try both, and it's not working as either...

...if I create it as a Simple Scripted Validator and try to create the request I am always given an error at the top of the screen.  The error displayed is the text I have in the Error Message field on the Simple Scripted Validator setup screen.

...if I create it as a Custom Script Validator then it allows me to submit the request regardless of if the Service Account Username field begins with svc- or not. 

Antoine Berry
Community Champion
September 25, 2019

Yes absolutely, that is the only needed change, and you should use a Custom Script Validator. 

I have updated the script to handle an empty custom field. Please look in the logs the value of "customFieldValue" and "issueType" so you can ensure that the if condition is met correctly.

I just tried it so and it is working properly, we will get there. :)

Deleted user September 25, 2019

image.png

Deleted user September 25, 2019

Just some other screenshots showing the Request Type, and an example where I only put "test" into the Service Account Username field but was able to submit the request.  As you can see in my previous screenshot, it doesn't look like any logging is configured for the validator.  Is that something I need to configure within the code?  If so, would you be able to provide the logging code? 

image.pngimage.pngimage.png

Antoine Berry
Community Champion
September 25, 2019

Yes, please check the above script, I updated it. :)

Deleted user September 25, 2019

Oops, didn't notice the update.  Okay, will copy in the new version and test it out momentarily!

Deleted user September 25, 2019

Unfortunately, it's still allowing me to submit a request regardless of what I enter into the Service Account Username field.  Log data provided:

image.png

Antoine Berry
Community Champion
September 25, 2019

I updated first answer's script, I was using the original post issuetype which was wrong.

Deleted user September 25, 2019

Ooooh, I see the issue now.  The Issue Type is Service Request with Approvals, but the Request Type is New Service Account Request....looks like the script is expecting the IssueType to equal RequestType...

Do you know the variable name for Customer Request Type?  Seems like we just need to replace
&& issueType == "New Service Account Request"
with

&& [VARIABLE FOR CUSTOMER REQUEST TYPE] == "New Service Account Request"

and we should be good

Deleted user September 25, 2019

The way I understand it is that you have a few Issue Types and then multiple Customer Request Types under each Issue Type.  We have many Request Types which are Issue Type = Service Request with Approvals.  I only want this validator to kick in if the request is specifically the New Service Account Request request type.  Does that make sense?

Antoine Berry
Community Champion
September 25, 2019

So the "New Service Account Request" is a custom field value ?

Deleted user September 25, 2019

I may be using the wrong terminology, apologies for that, but it is one of our request types:

image.png

 

I know if I want to setup a filter for only that request type I would use the following:

image.png

Deleted user September 25, 2019

So I did make the change you suggested, and it does work (aka - if I try to submit a New Service Account Request and do not put svc- in that field I get the error message at the top of the screen).  However, now all of the other Requests I have which are of Issue Type = Service Request with Approvals are now broken as they won't let me submit the requests even though Service Account Username field is not a field on the request.  So it seems like in order to get this working properly I need to have the validator check to ensure the Customer Request Type is New Service Account Request.

Deleted user September 25, 2019

Alright....I THINK I have this figured out :)

In searching through other forums/sites I found a reference stating that custom fields begin with a cf behind the scenes, which made me realize in my previous post that Customer Request Type is cf 10900.

So I added the following lines to the code to script:

int custReqFieldId = 10900
def custReqField = ComponentAccessor.getCustomFieldManager().getCustomFieldObject(custReqFieldId)
def custReqFieldValue = issue.getCustomFieldValue(custReqField)?:"" as String

AND line:

log.error("customer request type : " + custReqFieldValue)

 

Then submitting another request and checking the log I knew that the value for the Customer Request Type (for this request on my particular setup) for New Service Account Request is opstest/89dc79df-4f65-4f9a-9c07-7de8112033ec so I modified your line of code to read:
if (!customFieldValue.startsWith("svc-") && custReqFieldValue == "opstest/89dc79df-4f65-4f9a-9c07-7de8112033ec"){

Ran a couple tests and it looks like I'm in business now.  It will error out if I submit a New Service Account Request and the Service Account Username field does not begin with svc- but it will not error out if I submit any other request which is of Type = Service Request with Approvals. 

Full script:

import com.atlassian.jira.component.ComponentAccessor
import com.opensymphony.workflow.InvalidInputException

int customFieldId = 13300
def customField = ComponentAccessor.getCustomFieldManager().getCustomFieldObject(customFieldId)
def customFieldValue = issue.getCustomFieldValue(customField)?:"" as String

int custReqFieldId = 10900
def custReqField = ComponentAccessor.getCustomFieldManager().getCustomFieldObject(custReqFieldId)
def custReqFieldValue = issue.getCustomFieldValue(custReqField)?:"" as String

def issueType = issue.getIssueType().getName()

log.error("customFieldValue : " + customFieldValue)
log.error("issueType : " + issueType)
log.error("customer request type : " + custReqFieldValue)

if (!customFieldValue.startsWith("svc-") && custReqFieldValue == "opstest/89dc79df-4f65-4f9a-9c07-7de8112033ec"){
throw new InvalidInputException("customfield_"+customFieldId,"The Service Account Username field must start with svc-")
}

Deleted user September 25, 2019

so ultimately if others stumble upon this topic they'll need to lookup the custom field id of the field which they're checking for a string of characters (in my case it was 13300) and update the script

and then lookup the custom field id of their Customer Request Type field (in my case it was 10900) and update the script

then submit a request (the same request type they're setting this up for), in my case it was New Service Account Request and then go back into the workflow -> Transition that this check will apply to -> Validators to check the log results for what their custReqFieldValue is and update the script

I really hope this makes sense to anyone else who may stumble upon this :)

Antoine,
Can you update your suggested script at the beginning to match what I've provided above?  I'll then mark your answer as accepted.  Thanks SO MUCH for your help with this!!!

Deleted user September 25, 2019

I think I jumped the gun on this...the script I have is now allowing anything to go into the field, it's no longer stopping the submission if SA Username field doesn't begin with svc-

I'm leaving for a one week vacation today so I'll have to jump back on this when I return.

Antoine Berry
Community Champion
September 26, 2019

Hi @[deleted] ,

I updated the script as I understood your requirement. Keep in mind that this is specific to your configurations since you are using custom fields, but it should be a good starting point for anyone. Have a great week. :)

Deleted user October 4, 2019

Thanks again for your time with this.  I did try the updated script but it allowed me to submit a request with just "test" in the Service Account Username field (id 13300).  I checked the log and this is the output:

2019-10-04 16:28:43,175 ERROR [workflow.ScriptWorkflowFunction]: Text customField value to check : test
2019-10-04 16:28:43,178 ERROR [workflow.ScriptWorkflowFunction]: issueType : Service Request with Approvals
2019-10-04 16:28:43,224 ERROR [workflow.ScriptWorkflowFunction]: *************************************************************************************
2019-10-04 16:28:43,226 ERROR [workflow.ScriptWorkflowFunction]: Script function failed on issue: null, actionId: 1, file: <inline script>
groovy.lang.MissingMethodException: No signature of method: com.atlassian.servicedesk.internal.customfields.origin.VpOrigin.positive() is applicable for argument types: () values: []
Possible solutions: notify()
 at Script9.run(Script9.groovy:16)

 

Nevermind...found the error there,  the colon was outside the " " on the last log line.  Moved it back inside the " " and that resolved the error.  However the script is still allowing me to submit a ticket no matter what I put into the Service Account Username field...

Antoine Berry
Community Champion
October 7, 2019

Hi @[deleted] ,

What do the logs look like without the error ?

Deleted user October 7, 2019

Latest log entry from my test on Friday:

Time (on server): Fri Oct 04 2019 16:46:03 GMT-0400 (Eastern Daylight Time)

The following log information was produced by this execution. Use statements like:log.info("...") to record logging information.

2019-10-04 16:46:03,183 ERROR [workflow.ScriptWorkflowFunction]: Text customField value to check : asdf
2019-10-04 16:46:03,184 ERROR [workflow.ScriptWorkflowFunction]: issueType : Service Request with Approvals
2019-10-04 16:46:03,185 ERROR [workflow.ScriptWorkflowFunction]: Other custom field value to check: opstest/89dc79df-4f65-4f9a-9c07-7de8112033ec
Antoine Berry
Community Champion
October 8, 2019

So I edited the first answer, I'm sure there would be a better way to test against the Customer Request Type but this should work.

Antoine

Deleted user October 9, 2019

Unfortunately this is the same thing I tried last week when I 'thought' I fixed the issue, but it still allows me to put any value into the Service Account Username field (id 13300) and submit the ticket without any errors.  Is there any other way to go about solving this problem?

Deleted user October 9, 2019

Here is the latest log entry.  It looks like it is correctly retrieving "opstest/89dc79df-4f65-4f9a-9c07-7de8112033ec" as the value of the Customer Request Type field, but for some reason it's not enforcing the check that Service Account Username field begins with svc-

 

Time (on server): Wed Oct 09 2019 16:35:26 GMT-0400 (Eastern Daylight Time)

The following log information was produced by this execution. Use statements like:log.info("...") to record logging information.

2019-10-09 16:35:26,889 ERROR [workflow.ScriptWorkflowFunction]: Text customField value to check : testtest
2019-10-09 16:35:26,890 ERROR [workflow.ScriptWorkflowFunction]: issueType : Service Request with Approvals
2019-10-09 16:35:26,890 ERROR [workflow.ScriptWorkflowFunction]: Other custom field value to check : opstest/89dc79df-4f65-4f9a-9c07-7de8112033ec
Deleted user October 9, 2019

Would it be possible to first check if Service Account Username (custom field id 13300) is NOT null, and if that test passes, then check if it begins with svc-

Currently the only request using the Service Account Username field is this new request I'm building so instead of trying to match the customer request type maybe we could have the first condition that Service Account Username has a value, and if so, then run the validation that it begins with svc-

Trying to think outside the box here to get this working... any other suggestions would be greatly appreciated.

Antoine Berry
Community Champion
October 10, 2019

Hi,

I updated first answer, you should probably test the customfield against its string value, it should probably work.

To clean things up I have added a log line to check the object type so we might use proper methods.

Antoine

Deleted user October 10, 2019

I think you did it.  Running through a few more tests now to confirm, but so far so good.  Looks like adding the toString() parameter may have been what we were missing from line 20?  As far as I can tell that's the only change made in latest suggestion, aside from the extra log error line.  I'll run a few more tests today and keep my fingers crossed!

if (!customFieldValue.startsWith("svc-") && issueType == "Service Request with Approvals" && custReqFieldValue.toString() == "opstest/89dc79df-4f65-4f9a-9c07-7de8112033ec"){

 

Below is the output of the latest log:

Time (on server): Thu Oct 10 2019 08:53:50 GMT-0400 (Eastern Daylight Time)

The following log information was produced by this execution. Use statements like:log.info("...") to record logging information.

2019-10-10 08:53:50,187 ERROR [workflow.ScriptWorkflowFunction]: Text customField value to check : svc-account
2019-10-10 08:53:50,187 ERROR [workflow.ScriptWorkflowFunction]: issueType : Service Request with Approvals
2019-10-10 08:53:50,188 ERROR [workflow.ScriptWorkflowFunction]: Other custom field value to check : opstest/89dc79df-4f65-4f9a-9c07-7de8112033ec
2019-10-10 08:53:50,188 ERROR [workflow.ScriptWorkflowFunction]: Other custom field value class : class com.atlassian.servicedesk.internal.customfields.origin.VpOrigin
Deleted user October 10, 2019

The latest script has passed every test I could throw at it.  Thank you SO MUCH for your help Antoine!  I never could have gotten this figured out without your help.

That being said, there are still two typos in the script you have...on lines 16 & 17, the : needs to be moved inside the double quotes.  Currently both of those lines have it outside the double quotes, which is causing an error in the logs.  Please make that change and then I'll mark the answer as accepted, and hopefully other people will come across this post and be able to utilize the script as well (assuming the tweak the script to match their custom field IDs)

Lines 16 & 17 should be:
log.error("Other custom field value to check : " + custReqFieldValue)
log.error("Other custom field value class : " + custReqFieldValue.getClass())

Antoine Berry
Community Champion
October 14, 2019

Hi @[deleted] , glad it finally worked out ! You are absolutely right about the quotes, I was sick the last few days and updated on the fly, it is fixed now.

Anyway I checked for a javadoc for VpOrigin custom field but cannot find anything, so I guess you will have to stick with the id...

Cheers

Antoine Berry
Community Champion
October 21, 2019

Hi @[deleted] ,

Are you satisfied with the edited answer? :)

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events