How to filter issues depending on a custom field value?

I want to fetch all the issues matching a particular customfield value on the create transition using groovy script.

There's a custom field called domain name in a project ASM.

On create transition I want to fetch all issues with particualr domain name and match that value with current issue. If it matches then copy the value of another field to the current issue.

Pls help

6 answers

0 vote

What have you tried so far? Break your task down in to component pieces and try to do them individually. I doubt anyone is going to write your script for you.

This is my code

ComponentManager componentManager = ComponentManager.getInstance();

customFieldManager = componentManager.getCustomFieldManager();



def public fetchemail(MutableIssue issue)

{

    ComponentManager componentManager = ComponentManager.getInstance();

    customFieldManager = componentManager.getCustomFieldManager();

    def Category log = Category.getInstance("com.onresolve.jira.groovy.PostFunction")

    CustomField cfemail = customFieldManager.getCustomFieldObjectByName( "Customer Email" );



    def cfemailv = issue.getCustomFieldValue(cfemail);

    String domain_name = cfemailv.substring(cfemailv.indexOf("@")+1, cfemailv.lastIndexOf("."));

    log.warn("substring: "+domain_name);

    log.warn ("value: " +cfemailv);



    CustomField cfdomain = customFieldManager.getCustomFieldObjectByName("Domain Name");

    def cfdomainv = issue.getCustomFieldValue(cfdomain);

    def test = issue.setCustomFieldValue(cfdomain,domain_name);

    log.warn("print value of d name" +cfdomainv);

    CustomField cfSType = customFieldManager.getCustomFieldObjectByName("Support Type")

    def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()

    //Searching issues in the project

    def context = new JiraServiceContextImpl(user)

    def searchRequestService = ComponentManager.getInstance().getSearchRequestService()

    def searchProvider = ComponentManager.getInstance().getSearchProvider()

    def sr = searchRequestService.getFilter(context,10000)

    def searchResult = searchProvider.search(sr?.getQuery(), user, PagerFilter.getUnlimitedFilter())

    def issueManager = ComponentManager.getInstance().getIssueManager()

    def issues = searchResult.getIssues().collect {issueManager.getIssueObject(it.id)}



    for ( MutableIssue myissue in issues ){



        def cfdNamev = myissue.getCustomFieldValue(cfdomain);

        log.warn("print value of domain name" +cfdNamev)

      if(test.equals(cfdNamev))

       {

           CustomField cfSupportType = customFieldManager.getCustomFieldObjectByName("Support Type");

           def cfSupportTypev = i.getCustomFieldValue(cfSupportType);

           issue.setCustomFieldValue(cfSType,cfSupportTypev);

           log.warn("support type value" +cfSupportTypev);

       }



    }

}
log.warn("start execution");
fetchemail(issue);

I have grabbed the domain name from email field. There is another field called Domain name.

I am comparing it in 'if condition' . It throws the following an error while comparing

2014-01-08 16:19:16,694 http-bio-8080-exec-19 WARN admin 979x13724x1 1s2wvdk 10.1.1.181 /browse/AD-136 [jira.jql.query.GenericClauseQueryFactory] The 'Domain Name' clause does not support the = operator.

This is my code:

ComponentManager componentManager = ComponentManager.getInstance();
 
customFieldManager = componentManager.getCustomFieldManager(); 
 
def public fetchemail(MutableIssue issue)
 
{
 
    ComponentManager componentManager = ComponentManager.getInstance();
 
    customFieldManager = componentManager.getCustomFieldManager();
 
    CustomField cfemail = customFieldManager.getCustomFieldObjectByName( "Customer Email" );
 
 
 
    def cfemailv = issue.getCustomFieldValue(cfemail);
 
    String domain_name = cfemailv.substring(cfemailv.indexOf("@")+1, cfemailv.lastIndexOf(".")); 
 
    CustomField cfdomain = customFieldManager.getCustomFieldObjectByName("Domain Name");
 
    def cfdomainv = issue.getCustomFieldValue(cfdomain);
 
    def test = issue.setCustomFieldValue(cfdomain,domain_name);
CustomField cfSType = customFieldManager.getCustomFieldObjectByName("Support Type")
 
    def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()

0 vote

You seem to have posted your code then removed it?

You are assigning the result of issue.setCustomFieldValue to "test" and then later testing it for equality, however that method has void return type, so "test" will always be null.

> The 'Domain Name' clause does not support the = operator.

Need some context for this.

Hey Jamie,

I have posted my code.

This is my code:

import  com.atlassian.jira.ComponentManager
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.CustomFieldManager;
import com.atlassian.jira.issue.fields.CustomField;
import com.atlassian.jira.issue.IssueManager;
import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.issue.MutableIssue
import org.apache.log4j.Category
import com.atlassian.jira.jql.builder.JqlQueryBuilder
import java.util.List
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.issue.util.IssueChangeHolder;
import com.atlassian.jira.user.util.*
import com.atlassian.crowd.embedded.api.User
import com.atlassian.jira.user.DelegatingApplicationUser
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.search.SearchProvider
import com.atlassian.jira.issue.search.SearchResults
import com.atlassian.query.Query
import org.apache.log4j.Category
import com.atlassian.jira.jql.builder.JqlQueryBuilder
import com.atlassian.jira.jql.builder.JqlClauseBuilder
import java.util.Date
import java.util.Calendar
import com.atlassian.jira.bc.JiraServiceContextImpl
import com.atlassian.jira.web.bean.PagerFilter
import com.atlassian.jira.issue.Issue

ComponentManager componentManager = ComponentManager.getInstance();
customFieldManager = componentManager.getCustomFieldManager();

def public fetchemail(MutableIssue issue)
{
    ComponentManager componentManager = ComponentManager.getInstance();
    customFieldManager = componentManager.getCustomFieldManager();
    def Category log = Category.getInstance("com.onresolve.jira.groovy.PostFunction")

//Search issue with custom field Domain name
     def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
    CustomField cf = customFieldManager.getCustomFieldObjectByName("Domain Name");
    String cfValue=(String)issue.getCustomFieldValue(cf );
    JqlClauseBuilder builder = JqlQueryBuilder.newClauseBuilder();
    Query query = builder.customField(cf.getIdAsLong()).like(cfValue).buildQuery();
    SearchProvider searchProvider = ComponentAccessor.getComponentOfType(SearchProvider.class);
    SearchResults searchResults = searchProvider.search(query, ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser(),PagerFilter.getUnlimitedFilter());
    List<Issue> issues = searchResults.getIssues();

    CustomField cfemail = customFieldManager.getCustomFieldObjectByName( "Customer Email" );

    def cfemailv = issue.getCustomFieldValue(cfemail);
    String domain_name = cfemailv.substring(cfemailv.indexOf("@")+1, cfemailv.lastIndexOf("."));
    log.warn("substring: "+domain_name);
    log.warn ("value: " +cfemailv);

    def cfdomainv = issue.getCustomFieldValue(cf);
    def test = issue.setCustomFieldValue(cf,domain_name);
    CustomField cfSType = customFieldManager.getCustomFieldObjectByName("Support Type");

    //Searching issues in the project
    for (myissue in issues ){

        def cfdNamev = myissue.getCustomFieldValue(cf);
        log.warn("print value of domain name" +cfdNamev)
      if(cfValue == cfdNamev)
       {
          // CustomField cfSupportType = customFieldManager.getCustomFieldObjectByName("Support Type");
           def cfSupportTypev = myissue.getCustomFieldValue(cfSType);
           issue.setCustomFieldValue(cfSType,cfSupportTypev);
           log.warn("support type value" +cfSupportTypev);
       }

    }

}
log.warn("start execution");
fetchemail(issue);

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

74 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