Condition script that validates the request type

Ana Luiza Chagas
Contributor
April 22, 2024

Hi team

I'm trying to write a script on condition inside a scriptrunner post function for Jira Cloud that validates that the request type is the same as the name or ID entered.
I researched this topic but found solutions only for Jira Server.

Has anyone seen something similar for Jira Cloud?

Thank you for your help!

3 comments

Comment

Log in or Sign up to comment
Dave Rosenlund _Trundl_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
April 22, 2024

Hi, @Ana Luiza Chagas  👋

I am not an expert is this area and cannot really help you, sorry. However, I added a Scriptrunner tag to your post. This will help draw the right eyeballs to your post.

But you should also consider whether you are really asking a JSM question here, or a Scriptrunner question. If you feel it's a Scriptrunner question—or a question about any Atlassian add-on—your question is best placed in the App Central group

-dave

P.S. I can move it to App Central if you'd like. Just reply with a comment.

Ana Luiza Chagas
Contributor
April 23, 2024

Hi @Dave Rosenlund _Trundl_ 

Could you move a discussion to the central app please.

Thanks.

Like Dave Rosenlund _Trundl_ likes this
Dave Rosenlund _Trundl_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
April 23, 2024

Done, @Ana Luiza Chagas   😎

Like Ana Luiza Chagas likes this
Matt Doar _Adaptavist_
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.
April 23, 2024

https://docs.adaptavist.com/sr4jc/latest/features/workflow-extensions/post-functions/built-in-post-functions is a good place to start for more info about Jira ScriptRunner for Cloud. But let me ask for some details 

1. Which post function do you want to use? A built-in one or a completely custom one?

2 . Request type I understand, but it's not clear to me what you mean by "name or ID". Please could you add some more info about that

 

Like # people like this
Ana Luiza Chagas
Contributor
April 23, 2024

Hello @Matt Doar _Adaptavist_ 

1. My question would be about a condition within the scriptrunner post function, being the first topic in this document: https://docs.adaptavist.com/sr4jc/latest/features/workflow-extensions/post-functions/fields

2. I would like to know if I need to enter the name or ID of the request types in the customfield_10010 field in Jira Service Management.

I tried something like: (map(issue.fields.requesttype)?.name == "Password update"
But I was unsuccessful

Have you ever seen something like this?

Thanks for the help!


Nic Brough -Adaptavist-
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.
April 23, 2024

Hi Ana, 

There's a couple of questions here.   

Are you sure you mean a post-function and not a condition?  If so, then I assume your "condition" is a decision point for the following code that will update something.  Like "If request-type = 4, then change field X to Y, else copy summary to field Z"

I am not sure what you mean by "the request type is the same as the name or ID entered".  The issue you are running the post-function for will have the request type that was entered when the customer created the request, so I'm not sure what you are trying to compare it to.

But to get you started, the request type for an issue shows up as a custom field.

If you go to the Scriptrunner console and run the following code (this is one of the built-in example scripts), you will get a view of all the fields on the issue HELP-1.

def issueKey = 'HELP-1'

def result = get('/rest/api/2/issue/' + issueKey)
.header('Content-Type', 'application/json')
.asObject(Map)

if (result.status == 200){
return result.body.fields
} else {
return "Failed to find issue: Status: ${result.status} ${result.body}"
}
In my case, this returns all of the issue data, but includes a section that starts with:
"customfield_10010": {
    "_links": {
      "jiraRest": "https://nbrough-adaptavist-dev.atlassian.net/rest/api/2/issue/10001",
      "web": "https://nbrough-adaptavist-dev.atlassian.net/servicedesk/customer/portal/1/HELP-1",
      "self": "https://nbrough-adaptavist-dev.atlassian.net/rest/servicedeskapi/request/10001",
      "agent": "https://nbrough-adaptavist-dev.atlassian.net/browse/HELP-1"
    },
    "requestType": {
      "_expands": [
        "field"
      ],
      "id": "1",
      "_links": {
        "self": "https://nbrough-adaptavist-dev.atlassian.net/rest/servicedeskapi/servicedesk/1/requesttype/1"
      },
....
There's loads more content for this - I don't know which bit you are looking for



So you can get the request type (and other things about the request) by reading for customfield_10010

 

Like # people like this
Ana Luiza Chagas
Contributor
April 23, 2024

Hi  @Nic Brough -Adaptavist- 

The Script would be to check the content of the custom field customfield_10010
Ex: I have a request type called “Password Update” in Jira Service Management.

I would like the script to confirm whether the created item has the "Password Update" request type and if so, it continues the flow.

I tried something like: (map(issue.fields.requesttype)?.name == "Password update" but I was unsuccessful

Have you ever seen something like this?
Thanks for the help!

Nic Brough -Adaptavist-
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.
April 23, 2024

You can't stop the transition during a post-function, as post-functions are executed at the end of the transition process and are part of it.

 

Like Mohamed Benziane likes this
Ana Luiza Chagas
Contributor
April 24, 2024

Hi @Nic Brough -Adaptavist- 

I'm trying to make a post function at the end of the create an item event to clone this item to another project. Therefore, the condition would be important because if the item presents a certain request type (customfield_10010), the cloning condition would be false, causing the item to only be cloned in some cases.

As in this example condition that the item is only cloned if its type is equal to "Bug"
ex.PNG

Nic Brough -Adaptavist-
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.
April 25, 2024

Ok, thanks for clarifying all of that!

so I need to talk to the Cloud SR developers about their wording - they should not be using "Condition" here, because it has a different meaning in Jira-speak, and it confused me when reading the question (A Jira "condition" is something that stops you using a transition, not part of a transition)

Anyway, the SR-condition here uses "expressions", and as the Request Type looks a lot like a custom field (because it is implemented as one), then you should be able to use something like

issue.customfield_10010?.value == 'P42'

That assumes the ID of the Request Type is 42 (It was 1 in the response I got in the console script I gave before).  I do not think it will work with the name of the Request Type, but I have not tried it.

Ana Luiza Chagas
Contributor
May 22, 2024

Hi @Nic Brough -Adaptavist- 

I rescued this discussion to ask for help.

I tried implementing the post function in scriptrunner for jira cloud, using the example you mentioned last month: "issue.customfield_10010?.value == 'P42'"
condition.PNG

In this case, replacing them with the IDs of my project's request types was unsuccessful. I tried to open a ticket of the type I provided in the ID, but it doesn't work and triggers the following message in the log:image.PNG

 

Can you help me? 

Thanks your help!

 

TAGS
AUG Leaders

Atlassian Community Events