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

Ivan Ferro May 27, 2020

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

0 votes
Ivan Ferro June 3, 2020

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

0 votes
Joanna Choules
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.
June 1, 2020

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

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events