Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

Next challenges

Recent achievements

  • Global
  • Personal

Recognition

  • Give kudos
  • Received
  • Given

Leaderboard

  • Global

Trophy case

Kudos (beta program)

Kudos logo

You've been invited into the Kudos (beta program) private group. Chat with others in the program, or give feedback to Atlassian.

View group

It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

Add specific organization via ScriptRunner ( i need to check if customer is in a specific group)

Hello guys,

I search every where and check all discussion but I'm not able to find the answer/suggestion I need. I realize a big script running on listener and triggered by Issue creation on JSD 4.8.0 on Jira Core 8.8.0 and Adaptavist ScriptRunner for JIRA version 6.0.2-p5.

the script is checking if the reporter is present in a specific group and needs to add few "RequestPartecipants" ( the area manager in charge for that area) and add few Organization ( the local re-sellers). I don't find the way to add specific organization I would like to.

Here you can find a reduced version of the script, where in the INDIA case, I would like to add an organization ( customer organization is added by default ). in the same place you can find few rows of commented code that I tried but they are wrong. Is there anyone can provide me suggestion?

thanks in advance

Ivan

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.fields.CustomField
import com.onresolve.scriptrunner.db.DatabaseUtil
import com.atlassian.jira.event.issue.IssueEvent
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.issue.link.IssueLink
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.user.ApplicationUser
import java.util.ArrayList
import groovy.transform.BaseScript
import com.atlassian.servicedesk.api.ServiceDesk
import com.atlassian.servicedesk.api.ServiceDeskManager
import com.atlassian.servicedesk.api.organization.CustomerOrganization
import com.atlassian.servicedesk.api.organization.OrganizationService
import com.atlassian.servicedesk.api.organization.OrganizationsQuery
import com.atlassian.servicedesk.api.util.paging.LimitedPagedRequest
import com.atlassian.servicedesk.api.util.paging.LimitedPagedRequestImpl
import com.onresolve.scriptrunner.runner.customisers.PluginModule
import com.onresolve.scriptrunner.runner.customisers.WithPlugin
import com.atlassian.jira.bc.group.search.GroupPickerSearchService
import com.atlassian.crowd.embedded.api.Group
import groovy.transform.Field

@WithPlugin("com.atlassian.servicedesk")
@PluginModule
ServiceDeskManager serviceDeskManager
@PluginModule
OrganizationService organizationService

// Enable debugger

import org.apache.log4j.Logger
import org.apache.log4j.Level
def _log = Logger.getLogger("com.acme.CreateSubtask")
_log.setLevel(Level.DEBUG)

// end debug

def issue = event.issue
issue = issue as MutableIssue
def issueManager = ComponentAccessor.issueManager
def currentReporter = issue.reporter
_log.info("Begin : Current reporter = " + currentReporter.name)
def customFieldManager = ComponentAccessor.getCustomFieldManager()


CustomField organizationObj = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectsByName("Organizations").first() // Organizations custom field
def organizationCFValue = issue.getCustomFieldValue(organizationObj)
_log.info("Begin : Organizations custom field:" + organizationCFValue)

CustomField requestPartecipantsObj = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectsByName("Request participants").first() // Request Partecipant
def partecipatsCFValue = issue.getCustomFieldValue(requestPartecipantsObj)

def userManager = ComponentAccessor.getUserManager()
_log.info("Begin : Request Partecipant custom field:" + partecipatsCFValue)
_log.info("Begin : Begin : Script called")

GroupPickerSearchService groupSearch = ComponentAccessor.getComponent(GroupPickerSearchService.class)
List<Group> groupList = groupSearch.findGroups("")
ArrayList<ApplicationUser> userList = new ArrayList<ApplicationUser>()

// get all the organizations configured for that project
List<CustomerOrganization> organizationsToAdd
ArrayList<CustomerOrganization> agencyList = new ArrayList<CustomerOrganization>()
def organisationService = ComponentAccessor.getOSGiComponentInstanceOfType(OrganizationService)

ServiceDesk serviceDeskProject
def serviceDeskId = serviceDeskProject?.id as Integer

// get the available organizations for that project
def organizationsQuery = organizationService.newOrganizationsQueryBuilder().serviceDeskId(serviceDeskId).build()

def familyNameDbValue
def getUsersAsArray = []
def users = []
def biUser = ""


biUser ="businnessUnit"

// Set Request Partecipant, BI, AM and Agency
for (Group group : groupList){
_log.info("GroupName Found:" + group.getName());
if(ComponentAccessor.groupManager.getUserNamesInGroup(group.getName()).contains(currentReporter.name)){

_log.info("I found User: " + currentReporter.name + " in GroupName: " + group.getName());
switch(group.getName()) {
case ["INDIA","JAPAN","CINA","PAKISTAN","BANGLADESH"]:
_log.info("CASE - 6");
// aggiungi partecipanti
getUsersAsArray = {
users = ["giuseppe", biUser]
_log.info("Users : "+ users);
users.each{
def user = userManager.getUserByName(it)
_log.info("Area Manager: Giuseppe");
if(user){userList.add(user)}
}
return userList
}
try {
_log.info("CASE - 6 : 1 = " + getUsersAsArray);
ArrayList<ApplicationUser> applicationUsers = getUsersAsArray()
_log.info("CASE - 6 : = " + applicationUsers);
requestPartecipantsObj.updateValue(null, issue, new ModifiedValue(partecipatsCFValue, applicationUsers), new DefaultIssueChangeHolder());
} catch (ignored) {
_log.info("CASE - 6 : none");
}

if( group.getName() == "INDIA"){
_log.info("Organization for this Area: LOCAL AGENCY");
def agencies = ["LOCAL AGENCY"]
def getAgentsAsArray = {
agencies.each{
// this piece of code is not correct and not working
//def organisationService = ComponentAccessor.getOSGiComponentInstanceOfType(OrganizationService)
//def organizationQuery = organisationService.newOrganizationsQueryBuilder().serviceDeskId(1).build()
//def agency = organisationService.getOrganizations(currentReported, organizationQuery)

}
}
return agencyList
// unpdate here the custom field
}
break
default:
_log.info("CASE - DEFAULT");
break
}
}
}

 

 

 

 

2 answers

Hi Ivan,

What specifically seems to be wrong with the code you've written? Does it throw any errors?

One thing I would note is that the organizations query builder requires you to specify a PagedRequest as well as the ID of a service desk:

import com.atlassian.servicedesk.api.util.paging.SimplePagedRequest

//...

def organizationQuery = organisationService
.newOrganizationsQueryBuilder()
.serviceDeskId(1)
.pagedRequest(SimplePagedRequest.paged(0, 100))
.build()

 (The first argument to paged is the starting index, and the second argument is the limit: I've chosen 0 and 100 here but you may need to change this to suit your own use case.)

Regards,

Joanna Choules, Adaptavist Product Support

Hi Johanna,

 

thanks for this suggestion but I think I was searching for another answer. Actually I know how to add customer organization to the ticket but what I do not know is how to add another organization where this customer is not involved. So, in my previous post the code is commented.

so, assuming this PSEUDO code:

IF ( Ivan is in Group Name "A" ) Then

add Organization B 

END_IF

 

please note that Ivan is not in Organization B

 

many thanks

Ivan

Suggest an answer

Log in or Sign up to answer
TAGS
Community showcase
Published in Marketplace Apps & Integrations

Bitbucket Smart Commits vs. Genius Commits - What's the difference?

If you already heard about Smart Commits in Bitbucket, know that you just stumbled upon something even better (and smarter!): Genius Commits by Better DevOps Automation for Jira Data Center (+ Server...

130 views 0 2
Read article

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you