how to validate custom field(user picker filed) with simple script validator

Raju Adluru
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.
September 5, 2013

Hi

Using simple script validation, i want to validate if a custom field(user picker field) is empty, show error message.

I tried below code, it always shows msg, even when customfield has user in it.

cfValues['customfield_10000'] = null

I tried many combinations of above with custom field name etc, still same issue.

Thanks

5 answers

1 accepted

1 vote
Answer accepted
JamieA
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.
September 5, 2013

With a simple scripted validator, you just need:

cfValues['customfield_10000']
If you want to ensure it's empty use:
! cfValues['customfield_10000']
 
 
 
 
Raju Adluru
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.
September 6, 2013

Jamie

I tried with this script -

cfValues['customfield_10000']

it gives message even there is value in custom field( user picker field)

this script -

! cfValues['customfield_10000']

does not give any message, if there is no user in custom field

Raju Adluru
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.
September 6, 2013

Jamie

Thanks for quick response, sorry if there was any confusion.

Let me explain the scenario :

while doing the transition, check the validation

If CR user( customfiel_10000) is empty, show msg to user to select a user in custom field, and dont allow the tranisition.

If CR user already there in field, dont show a msg, allow the transition.

7 votes
Georges Moubarak
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.
April 15, 2015

@Jamie Echlin [Adaptavist],

I am using the script validator on the create transition and I want to build my condition based on the value of a user picker custom field and another select list custom field. I have two problems, the first is how to capture the value of the custom field on create transition and how to compare the value of the user picker custom field to a certain value. My script should be in this form:

if (cfValues['Some Custom Field'] == 'Some Value' and cfValues['user picker field'] != 'xxx') return false; else return true;

1 vote
RambanamP
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.
September 5, 2013

i think you have to try some think like this

import com.atlassian.jira.ComponentManager
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.crowd.embedded.api.User

 
MutableIssue issue = issue
ComponentManager componentManager = ComponentManager.getInstance()
CustomFieldManager customFieldManager = componentManager.getCustomFieldManager()
CustomField customField = customFieldManager.getCustomFieldObject("User Custom field Name")

def fieldVal = issue.getCustomFieldValue(customField)
   if(fieldVal == null ){
  //throw error message here
   }

Raju Adluru
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.
September 5, 2013

Hi R Prasad

Thanks for the reply, i tried with this code, i still get error msg, when custom filed is NOT null!!

RambanamP
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.
September 5, 2013

can you share what is the error message you are getting?

Raju Adluru
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.
September 5, 2013

Hi Prasad

I am using JIRA 6.0.2, here is the catalina.out error.

on screen, it shows message " A Code reviewer must be assgined before intiating code review" even when code reviewer is present in the field( not null), not able to tranistion issue with message.

2013-09-06 11:34:39,294 http-bio-9000-exec-41 ERROR xxxxxx 694x76840x1 1tnseum 197.1.18.83 /secure/CommentAssignIssue.jspa [atlassian.jira.workflow.OSWorkflowManager] Caught exception while attempting to perform action 881 from workflow 10641 on issue 'DEV-74'

[InvalidInputException: [Error map: [{}]] [Error list: [[A Code reviewer must be assgined before intiating code review]]]

at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)

at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)

at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)

at java.lang.reflect.Constructor.newInstance(Unknown Source)

at org.codehaus.groovy.reflection.CachedConstructor.invoke(CachedConstructor.java:77)

at org.codehaus.groovy.reflection.CachedConstructor.doConstructorInvoke(CachedConstructor.java:71)

at org.codehaus.groovy.runtime.callsite.ConstructorSite.callConstructor(ConstructorSite.java:42)

at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callConstructor(AbstractCallSite.java:190)

at com.onresolve.jira.groovy.canned.workflow.validators.SimpleScriptedValidator.doScript(SimpleScriptedValidator.groovy:66)

RambanamP
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.
September 5, 2013

is there any validators/functions configured on that transitions?

JamieA
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.
September 5, 2013

NB - rambanam's answer relates to using a standalone groovy script. With the "simple scripted validator" you just provide a statement that evaluates to either true or false, and provide the error message. You don't need to worry about throwing the exception.

Raju Adluru
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.
September 6, 2013

@Prasad

There are no other validations on this transition. this is onky validation i was adding as a simple script validation in that transition.

@ Jamie

oh ok, let me try with groovy script

Raju Adluru
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.
September 6, 2013

i tried with Prasad code as a behaviour, it did not show any msg to user, when custom field is empty.

0 votes
Hans-Hermann Hunfeld October 27, 2013

Hey Jamie,

thanks for being quick, but unfortunately this don t work! I adapted your suggestion to the one below as CF-Type of "Patch Type" is single select list, the CF "CEMLI" is of Type "Select List (multiple choices)".

cfValues['Patch Type'].value == 'Patch' && ! cfValues['CEMLI'].value

This expression blocks everything, i assume the expression doesn t match to the multi-select list?!

Update: Tested with only

cfValues['Patch Type'].value == 'Patch'

doesn t block anything... *confused*

JamieA
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.
October 27, 2013

Try the two parts separately in the admin -> built-in scripts -> condition tester.

That I was quick was just pure coincidence by the way.

Suresh August 24, 2014

I have multi select list custom field. If i select perticular option script should make two fields required on same screen.How we can achive this.

JamieA
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.
August 24, 2014

It's just a variation on the above. What have you tried so far?

0 votes
Hans-Hermann Hunfeld October 27, 2013

Maybe it s a good place to add my question here as we want to check something very similar:

If one custom field (Patch Type) is of a specific value (Patch) another custom field (CEMLI) is not allowed to be empty. In case the expression is true prevent of the transition (Create Issue) and show a message (Please choose according CEMLI) to the user.

cfValues['Patch Type'] == 'Patch' && ! cfValues['CEMLI']

The result of this is that no issue can be created as the message is shown at any combination of values. Anybody an idea what to be changed?

JamieA
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.
October 27, 2013

If Patch Type is a select list, it should be:

cfValues['Patch Type'].value == 'Patch'

Suggest an answer

Log in or Sign up to answer