Organization automation

Hi guys,

I do not have experience with scripting, and not sure if this can be done by some add-in, so I want to ask if anyone of you has done something similar or can recommend some approach:

  1. when a customer creates an issue in service desk portal, I want to be able (most likely with a post-function during transition) to get the domain out of his email, create Organization if not already existing and add the customer to it. If the Organization exists, just add the customer to it.
    I assume this has to be done via custom code. Any examples how?

  2. During one transition in a Service Desk project, I am creating issue in other project. During this transition I want to copy the Organization from the Service Desk project and save it in other custom field in the other project.
    Do you know any Add-in that can do this? I am using the JIRA Misc Workflow Extensions, but it does not support the Organization field.

This is on Cloud.

Thank you in advance for your response.

Regards,

Klime

 

 

5 answers

Hi,

You could achieve this using ScriptRunner for JIRA Cloud and the REST APIs for JSD.

In your post function on the Create Issue transition, you would need to write a script a bit like this:

def creatorEmailAddress = issue.fields.creator.emailAddress
def creatorDomain = creatorEmailAddress.split('@')[0]
 
// We need to retrieve a list of the existing organisations to compare
// the email domain
// NB - You might need to paginate this request, see the documentation
def orgReq = get("/rest/servicedeskapi/organization")
				.asObject(Map)
assert orgReq.status == 200
 
def existingOrganisations = orgReq.body.values
 
// I've assumed here that the name of the organisation will match the
// domain exactly, but you might need different logic here
def organization = existingOrganisations.find { it.name == creatorDomain }
 
// Create the organization if it doesn't exist
if (organization == null) {
    def createReq = post("/rest/servicedeskapi/organization")
						.header('Content-Type', 'application/json')
						.body([
							name: creatorDomain
						])
						.asObject(Map)
    assert createReq.status == 201
    organization = createReq.body
}
 
// Now we add the user
def addReq = put("/rest/servicedeskapi/organization/${organization.id}/user")
        .header('Content-Type', 'application/json')
        .body([
            usernames: [
                issue.fields.creator.name
            ]
        ])
        .asString()
assert addReq.status == 204

For your other question, you can definitely do that with ScriptRunner too. On the relevant transition you would need a script a little bit like this:

// Get the field ids
def fields = get('/rest/api/2/field')
        .asObject(List)
        .body as List<Map>

def organizationFieldId = fields.find { it.name == "Organization" }.id
def otherCustomFieldId = fields.find { it.name == "Copy of Organization" }.id


// This would need changing to meet your specific requirements
def targetIssueToUpdate = "EXAMPLE-1"
 
def organization = issue.fields[organizationFieldId]
 
def updateReq = put("/rest/api/2/issue/${targetIssueToUpdate}")
                    .header('Content-Type', 'application/json')
                    .body([
                        fields: [
                            (otherCustomFieldId): estimates['Development Sub-Task']
                        ])
                    .asString()

// check that updating the issue worked
assert updateReq.status == 204

I hope that helps,

Jon

EDIT: fixed bugs in code

Thank you for your prompt answer Jon.

I will give it a try..

Thanks again

Hi Jon,

Tried the first script on Create transition, and I found following errors:

CorrelationId: 7b06ad8b-2f6f-424a-9352-6d666191f75a
RUN Script identifier: 3946e851-3ec9-4faa-84eb-3b4f0dc9e847 com.adaptavist.sr.cloud.workflow.RunScript ('Create' transition made for issue UTP-9) Took 100ms Logs:
2017-02-03 18:57:49,044 ERROR - Cannot get property 'emailAddress' on null object
2017-02-03 18:57:49,055 ERROR - Class: com.adaptavist.sr.cloud.workflow.RunScript, Config: [className:com.adaptavist.sr.cloud.workflow.RunScript, uuid:3946e851-3ec9-4faa-84eb-3b4f0dc9e847, description:Organization from Email, condition:, executionUser:ADD_ON, additionalCode:def creatorEmailAddress = issue.creator.emailAddress

Not quite sure how to bypass this...

Thanks in advance.

Regards,

Klime

Hi,

Try configuring the post function to run after the issue index event:

postfunction-ordering.png

Thanks,

Jon

Thanks Jon,

Tried the suggestion - moved the post-function after the indexing, but got again the same errors.

https://drive.google.com/open?id=0B3wqCCkDvaZNX196VWsyTW1UX3M
https://drive.google.com/open?id=0B3wqCCkDvaZNMVpFM3RwTGtJejQ

There are errors in the code window where the variables are defined.

Sorry again for bothering.

Thanks,
Klime

Apologies Kilme, the first line of the code needs to be:

def creatorEmailAddress = issue.fields.creator.emailAddress

Given you are not using this post function on the Create transition you should be able to move it back to the top of the list of post functions.

Also, some of the errors reported in the code editor are misleading, and we're working on removing them.

Thanks, Jon

Thanks again Jon,

I moved a little bit forward now, got the message as on the screenshot.

https://drive.google.com/open?id=0B3wqCCkDvaZNS2U5dkRyRzl1ZDQ

Tried few combinations in line23 (changed to id.name) and line35 (changed to issue.fields.creator.name), but got other errors, so I returned it as it was - as on the screenshot

Thanks,

Klime

Line 35 needs changing to:

issue.fields.creator.name

Hi Jon,

I have applied this change, and now if I execute with the Add-on user, I get this error:

2017-02-07 17:56:02,097 WARN - PUT request to https://scriptrunner.connect.adaptavist.com/jira/proxy/rest/servicedeskapi/organization/{organization.id}/user returned an error code: status: 401 - Unauthorized

 

When I go with the Initiating user, I get this one:

2017-02-07 18:03:59,645 WARN - PUT request to https://scriptrunner.connect.adaptavist.com/jira/proxy/rest/servicedeskapi/organization/{organization.id}/user returned an error code: status: 403 - Forbidden
body: {"error": "Add-on 'com.onresolve.jira.groovy.groovyrunner' blocked from impersonating the user because the access token does not have the required scope(s)"}
2017-02-07 18:03:59,647 INFO - PUT /rest/servicedeskapi/organization/{organization.id}/user asString Request Duration: 188ms
2017-02-07 18:03:59,650 ERROR - assert addReq.status == 204
       |      |      |
       |      403    false
       status: 403 - Forbidden

 

Any suggestions on this one?

Thanks,

Klime

Hi Klime,

Looks like you found another bug in my code!

That PUT request line should look like:

def addReq = put("/rest/servicedeskapi/organization/${organization.id}/user")

with a $ symbol before the open curly brace.

That should fix the problem I think, as long as you run this as the ScriptRunner Add-on user, but you'll need to make sure that that user already has permission to create organisations in JSD.

Thanks, Jon

Jon, just to say thank you for all of your effort on trying to help me with this.

I got the same errors with the latest suggestion, and the client unfortunately gave up from this feature.

Thank you again.

Regards,

Klime

 

Hi Klime,
Nice automation and a nice answer from Jon. We want to include an organisation example in our documentation for ScriptRunner for server and your first use case is really interesting.


I have a question though. What if the customer's email is a gmail or hotmail ? This will create a Gmail organization and if another customer with a gmail account creates an issue then you will end up having a Gmail organization and it's members being completely strangers. 

Was a full tutorial created for this by chance?

Hi Thanos,

You can use this case freely in your documentation, however, I did not make it work in my environment.

Jon did a great job trying to help - I am sure it is some minor change, but the client gave up from this.

Thank you all for your engagement.

Regards,

Klime

Suggest an answer

Log in or Join to answer
Community showcase
Teodora [Botron]
Published Feb 15, 2018 in Marketplace Apps

Jira Inferno: The Nine Circles of Jira Administration Hell

If you spend enough time as a Jira admin - whether you are managing a single, mid-sized instance, a large enterprise one or juggling multiple instances at once - you will eventually find yourself in ...

1,138 views 6 19
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
Atlassian Team Tour

Join us on the Team Tour

We're bringing product updates and pro tips on teamwork to ten cities around the world.

Save your spot