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

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

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

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...

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

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....

 

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

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

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

Of course Kristina! Thanks!

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.")
}
}

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

Np! if you need feedback feel free to ask.

Suggest an answer

Log in or Sign up to answer
Atlassian Community Anniversary

Happy Anniversary, Atlassian Community!

This community is celebrating its one-year anniversary and Atlassian co-founder Mike Cannon-Brookes has all the feels.

Read more
Community showcase
Bridget Sauer
Published Thursday in Marketplace Apps

Calling all developers––You're invited to Atlas Camp 2018

 Atlas Camp   is our developer event which will take place in Barcelona, Spain  from the 6th -7th of   September . This is a great opportunity to meet other developers and get n...

88 views 0 5
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