Behaviour: How to make required a customfield depending on project?

Begoña Bonet April 12, 2016

I need to make required a customfield using Behaviour plugin depending on project. 

What do I need to include in my script for doing this?:

if(getActionName() == "Create"){
   getFieldByName("customfieldXXXX").setRequired(true)
}

2 answers

1 accepted

1 vote
Answer accepted
Kristian Walker _Adaptavist_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
April 13, 2016

Hi Begona,

You can achieve your requirement using the behaviours module of the ScriptRunner plugin by following the instructions below. This does not require any groovy code to be written and can be achieved by using the built in functionality provided by Behaviours.

Steps:

  1. Add a new behaviour and a mapping to the projects and issue types required.
    image2016-4-13 11:44:4.png
  2. Click the fields link for the new behaviour you added and on the screen that is displayed, select the workflow used by the project.
    image2016-4-13 11:45:49.png
  3. In the fields selection add in the field to be made required.
  4. On the field screen displayed click the Require link in brackets next to where it says Optional.
    image2016-4-13 11:47:21.png
  5. Click the Add One link next to where it says Conditions.
    image2016-4-13 11:48:32.png
  6. On the screen that is displayed ensure the When radio button is checked and the Worklow Action radio button is checked. In the Workflow Action dropdown select Create and click the Add button
    image2016-4-13 11:49:18.png
  7. Your behaviour should now look similar to the screen below and when you create an issue the field will be required.
    image2016-4-13 11:51:7.png

    image2016-4-13 11:51:32.png

I hope this helps

Kristian

Begoña Bonet April 13, 2016

My behaviour is mapped to 4 different projects and only in one of them the customfieldXXX is required in Create action. For the other projects this field must be optional.....I think I need to use an script...

Kristian Walker _Adaptavist_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
April 13, 2016

Hi Begona,

You should creaate a seperate behaviour for the one project that the field is required.

This will be a more stable approach and will not break if any apis change in future.

Kristian

Begoña Bonet April 13, 2016

I'll try Kristian but I think that I had problems when I used 2 different Behaviours for the same workflow..... perhaps I'm wrong....

 

Thanos Batagiannis _Adaptavist_
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 13, 2016

Hi Begona,

Just an update you can get from a Behaviour the projectId via the getIssueContext() which returns to you a IssueContextImpl

import com.atlassian.jira.component.ComponentAccessor
def projectManager = ComponentAccessor.getProjectManager()
def projectId = getIssueContext().getProjectId()
def project = projectManager.getProjectObj(projectId)
 
// get the name of the project
def projectName = project.name
log.debug("Project name : ${projectName}")

// or the key
def projectKey = project.key
log.debug("Project key : ${projectKey}")

Hope that answers your question.

Regards

Begoña Bonet April 13, 2016

I'm wrong and it has worked fine with 2 differents behaviours. Thanks!

Kristian Walker _Adaptavist_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
April 13, 2016

Glad I was able to help.

If this problem has been resolved can you please mark the answer that worked as accepted so that other users can easily find the solution in future.

Thanks

Kristian

Begoña Bonet April 13, 2016

Of course Kristina! Thanks!

0 votes
Tsol
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 12, 2016

You can create a script validator. Something like the following maybe is what you need

import org.apache.log4j.Category;
import com.atlassian.jira.ComponentManager;
import com.opensymphony.workflow.InvalidInputException;
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.comments.CommentManager;
import com.atlassian.jira.issue.fields.CustomField;
import com.atlassian.jira.util.ImportUtils;
import com.opensymphony.util.TextUtils
import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.project.Project;
// creates the project object
Project project = issue.getProjectObject();
//Get Project id
Long pr_id = project.getId();
if ( pr_id == xxxxx ){
 
CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager();

//Get Custom Field Value
CustomField customField_xxxxx = customFieldManager.getCustomFieldObject( xxxxx );
if (!issue.getCustomFieldValue( customField_xxxxx )) {
invalidInputException = new InvalidInputException("customfield_{id}", "Custom field is required.")
}
}
Begoña Bonet April 13, 2016

I've solved the problem but I'll test also your solution! Thanks!

Tsol
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 13, 2016

Np! if you need feedback feel free to ask.

Begoña Bonet April 13, 2016

Thanks Kostas!

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events