Just a heads up: On March 24, 2025, starting at 4:30pm CDT / 21:30 UTC, the site will be undergoing scheduled maintenance for a few hours. During this time, the site might be unavailable for a short while. Thanks for your patience.
×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) }
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:
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...
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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....
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
I'm wrong and it has worked fine with 2 differents behaviours. Thanks!
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.") } }
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
I've solved the problem but I'll test also your solution! Thanks!
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Np! if you need feedback feel free to ask.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.