Can we set a custom field as mandatory based on other custom field value in same transition screen ?

Sys Admin March 6, 2014

We have configured a transition Namely "Detail Analysis Completed". In this Transition there is 2 fields -

1) Global in nature? [ Field Value = 11425 ]

2) Target Project Name(s) [ Field value = 11426 ]

In "Global in nature?" field there is 2 values -

1) Y [ valueid = 11588 ]

2) N [ Value ID = 11589 ]

Now we Need to set "Target Project Name(s)" field as mandatory when "Global in nature?" field value is "Y". Please note both "Global in nature?" and "Target Project Name(s)" are in same transition screen. Do we need to set any post function/condition/validator for that? If yes, then could you please provide us steps ?

Previously we have succuessfully impleamented "Show a particular customfield based on another customfield value" by configuring customised plugin using js script. Please find the js script below for previous case -

jQuery(document).ready(function($) {
JIRA.bind(JIRA.Events.NEW_CONTENT_ADDED, function (e, context) {
    callChangeFunction();
});
callChangeFunction();
function  callChangeFunction(){
        showHidField();
        // dropdown custom field change function
    $("#customfield_10020").change(function() {
        showHidField();
    });
 
}
    function showHidField(){
        //drop down field selected value
        var dropDownFieldval =
        $.trim($("#customfield_10020 :selected").text());
            //test field1
            $("#customfield_10221").closest('div.field-group').hide();
        if(dropDownFieldval == 'New Machine Required'){
            $("#customfield_10221").closest('div.field-group').show();
        }
 
    }
});

Can we using same procedure for "set a customfield required based on another customfield value" ? If so then could you please provide us the steps?

3 answers

0 votes
Tarun Sapra
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
March 6, 2014

See this code snippet -

CustomFieldManager customFieldManager = componentManager.getCustomFieldManager()
String sourceCfId = "customfield_10030"
String targetCfId = "customfield_10030"
CustomField cfSource = customFieldManager.getCustomFieldObject(sourceCfId)
CustomField cfTarget = customFieldManager.getCustomFieldObject(targetCfId)
if (issue.getCustomFieldValue(cfSource) != "Major") {
    issue.setCustomFieldValue(cfTarget, "some new value")
}

and see here, to throw validation error, you just have to initialize InvalidInputException

https://jamieechlin.atlassian.net/wiki/display/GRV/Validators

Now you can check that the field "Target Project Name(s" is not empty and not null if the other custom field has a value 'Y'

If you now tweak the above code acc. to your requirements and put it in the validation condition, things should work.

Sys Admin March 6, 2014

I have configured below mentioned script in Script Validator simple script "Has custom field" validation -

Script workflow function : Simple scripted validator : Checks script:
CustomFieldManager customFieldManager = componentManager.getCustomFieldManager()
String sourceCfId = "customfield_11425"
String targetCfId = "customfield_11426"
CustomField cfSource = customFieldManager.getCustomFieldObject(sourceCfId)
CustomField cfTarget = customFieldManager.getCustomFieldObject(targetCfId)
if (issue.getCustomFieldValue(cfSource) = "Y") {
    issue.setCustomFieldValue(cfTarget, "some new value")
}

But after configuring this "Target Project's" Field set required for Both 'Y' and 'N' of "Global in nature?" field. This is not my requirement. My requirement is to set "Target Project's" field required only for "Y" value of "Global in nature?" field. Could please check my code and correct if any wrong

Tarun Sapra
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
March 7, 2014

First and foremost, your "if statement" is Wrong, you are assigning a variable instead of checking, as you have used single = operator in the if statement.

if(issue.getCustom.....("global in nature....") =='Y'){

if(issue.getCustom....("target...") == null){

invaliad exception object initialization

}

}

First check is whether the value is "Y" and then we check if the value is null and if it's null which it shouldn't be then we initialize the exception object

In the above code snippet I have used the custom field name as I don't know their IDs thus in your code stick to the IDs like you have posted above.

Sys Admin March 7, 2014

Hi Tarun,

As per your suggestion I have used below mentioned code in Script Runner Validator but after configuring this "Target Project" Field has been set mandatory for both Y and N value of "Global in nature?" Customfield. Please help. Another point we are using Jira 5.2.4.1 version. Code is as below -

CustomFieldManager customFieldManager = componentManager.getCustomFieldManager()
String sourceCfId = "customfield_11425"
String targetCfId = "customfield_11426"
CustomField cfSource = customFieldManager.getCustomFieldObject(sourceCfId)
CustomField cfTarget = customFieldManager.getCustomFieldObject(targetCfId)
if(issue.getCustomFieldValue("customfield_11425") == 'Y'){
if(issue.getCustomFieldValue("customfield_11426") == null){
invaliad exception object initialization
}
}

Tarun Sapra
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
March 7, 2014

Hi,

Did you read the link which I posted few comments back?

https://jamieechlin.atlassian.net/wiki/display/GRV/Validators

WHen I write in the code sample "invaliad exception object initialization"

I mean to initialize the object! not to write it same stuff in english

invalidInputException = new InvalidInputException("Validation failure")

Sys Admin March 8, 2014

Hi Tarun,

Sorry to bother you so many times. It is my pleasure that you try to help me so many times. But I think I did something wrong everytime. After adding below mentioned code as per your suggestion same problem occured. "Target Project Name(s)" field has been set mandatory for both Y and N value of "Global in nature?" field. Could you please change my code if I did anything wrong. Sorry I am not good at coding. Please provide me the exact code based on my below mentioned code. For your info "Global in nature?" field value is 11425 and "Target Project Name(s)" field value is 11426. Please find the code below -

CustomFieldManager customFieldManager = componentManager.getCustomFieldManager()
String sourceCfId = "customfield_11425"
String targetCfId = "customfield_11426"
CustomField cfSource = customFieldManager.getCustomFieldObject(sourceCfId)
CustomField cfTarget = customFieldManager.getCustomFieldObject(targetCfId)
if(issue.getCustomFieldValue("customfield_11425") == 'Y'){
if(issue.getCustomFieldValue("customfield_11426") == null){
invalidInputException = new InvalidInputException("Target Project Name(s)", "Target Project Name(s) is required when specifying "Global in nature?" of \"Y\"")
}
}

By the Way This operation working when I have used below mentioned SIL code. But if we need to use SIL code we need to purchase JJUPIN Plugin. Which is not allowed from our company. So please provide us simple script validator as usually you provide us. For your info I am providing you the SIL code below -

string errorMsg = "Target Project Name(s) field is required.";
if(customfield_11425 == "Y" && !hasInput("customfield_11426")) {
   return false, "customfield_11426", errorMsg;
}

Tarun Sapra
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
March 9, 2014

DId you see the logs? what error is being shown?

is the syntax of invalidInputException correct?

Also you can try the code inside the adminstrative script panel

Sys Admin March 9, 2014

Hi Tarun,

In the Jira log following error messages showing -

Script27.groovy: 1: unable to resolve class CustomFieldManager
 @ line 1, column 20.
   CustomFieldManager customFieldManager = componentManager.getCustomFieldManager()
                      ^

Script27.groovy: 4: unable to resolve class CustomField
 @ line 4, column 13.
   CustomField cfSource = customFieldManager.getCustomFieldObject(sourceCfId)
               ^

Script27.groovy: 5: unable to resolve class CustomField
 @ line 5, column 13.
   CustomField cfTarget = customFieldManager.getCustomFieldObject(targetCfId)

Tarun Sapra
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
March 9, 2014

Didn't you do the import? You have to import the classes

Tarun Sapra
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
March 9, 2014

import com.atlassian.jira.issue.MutableIssue;

import com.atlassian.jira.issue.CustomFieldManager;

import com.atlassian.jira.issue.fields.CustomField;

Sys Admin March 9, 2014

Hi Tarun,

I have import above mentioned classes. After that above mentioned errors not showing. But an error showing for "invalidInputException". Please provide the import line for this. I am sending you the error below -

Script48.groovy: 11: unable to resolve class InvalidInputException
 @ line 11, column 25.
   invalidInputException = new InvalidInputException("Target Project Name(s)", "Target Project Name(s) is required when specifying Global in nature? of \"Y\"")

Tarun Sapra
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
March 9, 2014

import com.opensymphony.workflow.InvalidInputException

Sys Admin March 9, 2014

Hi Tarun,

Previous error not showing now. But below mentioned error is showing -

[InvalidInputException: [Error map: [{customfield_11426=Target Project Name(s) is required when specifying Global in nature? of "Y"}]] [Error list: [[]]]

Tarun Sapra
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
March 9, 2014

Isn't this the requirement? isn't this what you want, just use the code statement-

invalidInputException = new InvalidInputException("Validation failure")

instead of your stmt and it gives you an exception when validation fails. So it's working.

Tarun Sapra
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
March 9, 2014

Leave the script try this -

Has custom field equal to

(cfValues['Global in nature?'] == 'Y' && cfValues['Target Project Name(s)'] != null)

Sys Admin March 9, 2014

>> Isn't this the requirement? isn't this what you want, just use the code statement-

invalidInputException = new InvalidInputException("Validation failure")

instead of your stmt and it gives you an exception when validation fails. So it's working.

--> After using this script "Target Project(s)" has been set mandatory for both 'Y' and 'N' of "Global in nature?" Field. We need to set "Target Project(s)" mandatory only for 'Y' value.

>> Leave the script try this -

Has custom field equal to

(cfValues['Global in nature?'] == 'Y' && cfValues['Target Project Name(s)'] != null)

--> Same problem occured

Tarun Sapra
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
March 9, 2014

In this script -

(cfValues['Global in nature?'] == 'Y' && cfValues['Target Project Name(s)'] != null)

We clearly check only for value 'Y' and not for N, are you sure you don't have any extra validation checks in place or some extra script.

Sys Admin March 9, 2014

Hi Tarun,

There is no other validator configured

Tarun Sapra
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
March 11, 2014

were you able to fix it, as I still belive that it's

(cfValues['Global in nature?'] == 'Y' && cfValues['Target Project Name(s)'] != null)

and shouldn't be much trouble.

Sys Admin March 11, 2014

Hi Tarun,

Unfortunately not. After using this script "Target Project(s)" has been set mandatory for both 'Y' and 'N' of "Global in nature?" Field. We need to set "Target Project(s)" mandatory only for 'Y' value.

Tarun Sapra
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
March 11, 2014

Please delete the script which we discussed before, you don't need to use any script, just use this condition -

(cfValues['Global in nature?'] == 'Y' && cfValues['Target Project Name(s)'] != null)

as part of the Simple script validator

Sys Admin March 11, 2014

Hi Tarun,

I have added this condition. But the problem is as follows -

1) When I have add this in Validations as script validator "Target Project(s)" has been set mandatory for both 'Y' and 'N' of "Global in nature?" Field

2) Whwn I have add this in Condition as Script Condition ""Detail Analysis Completed" transition not showing from transition screen

Tarun Sapra
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
March 11, 2014

Ok, now it should work for sure....delete all the conditions, as we just need validation script no CONDITION script is required.

put this in the validation script.

cfValues['Global in nature?'] == 'N' || (cfValues['Global in nature?'] == 'Y' && cfValues['Target Project Name(s)'] != null)

Sys Admin March 11, 2014

Hi Tarun,

I have added above mentioned script in Script validator but Unfortunately same problem occurred. After using this script "Target Project(s)" has been set mandatory for both 'Y' and 'N' of "Global in nature?"

Tarun Sapra
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
March 11, 2014

is it Y or N or y or n, as the script it case senstive.

Sys Admin March 12, 2014

Hi Tarun,

It is Capital Letter 'Y' and 'N'

0 votes
Radu Dumitriu
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.
March 6, 2014
Sys Admin March 8, 2014

Hi Radu,

This operation working when I have used below mentioned SIL code. But if we need to use SIL code we need to purchase JJUPIN Plugin. Which is not allowed from our company. So could you please provide us simple script validator code ? For your info I am providing you the SIL code below -

string errorMsg = "Target Project Name(s) field is required.";
if(customfield_11425 == "Y" && !hasInput("customfield_11426")) {
return false, "customfield_11426", errorMsg;
}

0 votes
Tarun Sapra
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
March 6, 2014

i would suggest to refrain from using JQuery to do the task at hand, as after jira upgrade the code becomes hard to maintain, instead put the validation in the validation phase of the workflow transition using the scriptRunner, "has custom field value equal to" - https://jamieechlin.atlassian.net/wiki/display/GRV/Script+Runner

Sys Admin March 6, 2014

Hi Tarun

I have already used Scrprunner "Has custom ield eqal to" validation in the transtion. But after configuring this transition is not showing. Please Suggest

Tarun Sapra
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
March 6, 2014

Hi,

Please elaborate on - " But after configuring this transition is not showing."

Sys Admin March 6, 2014

I have add "Has custom ield eqal to" in "Detail Analysis Completed" transition condition using Script Runner simple scipt. I have mentioned following line -

cfValues['Global in nature?'] == 'Y'

But after doing this "Detail Analysis Completed" not showing in the screen. Could you please inform if I did anything wrong? If so then could please give me the exact steps

Tarun Sapra
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
March 9, 2014

Has custom field equal to

(cfValues['Global in nature?'] == 'Y' && cfValues['Target Project Name(s)'] != null)

Suggest an answer

Log in or Sign up to answer