How to get a project key by project name in JMWE post function?

Inna S March 18, 2023

Hi, 

I'm trying to create an issue in a different project based on a field value in the issue.

The value matches the project name, but the issue creation expects the project key.

Is there a way to get the project key by project name and then pass it to the issue creation in JMWE function?

Thank you.

2 answers

1 accepted

2 votes
Answer accepted
David Fischer
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
March 18, 2023

Hi @Inna S ,

you can use code like this:

{%set projectName = "Test"%}
{%set projectKey = "/rest/api/2/project/search?query=:name" | callJira(
params={
name:projectName
}
) | field("values") | find("name",projectName) | field("key")
%}
{{projectKey}}
Inna S March 19, 2023

Hi @David Fischer , 

thank you for this tip.

But the name of the destination project is in one of the issue fields. 

I've tried several ways of referencing it, but to no avail.

Say, the project name is in the field 'Project Name', that is a drop down list type of the field. 

How do I get it into the 'set' statement?

David Fischer
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
March 19, 2023

Then the first line becomes:

{%set projectName = issue.fields["Project Name"].value %}
Like Kelly Arrey likes this
Inna S March 19, 2023

It still comes empty.

David Fischer
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
March 19, 2023

You need to test step by step. 

First check that you're getting the project name right, exactly as it is in the project. Test this:

{%set projectName = issue.fields["Project Name"].value %}
**{{projectName}}**

 This will allow you to check that you're getting the project name without leading or trailing spaces. Then compare it with the name of the project - note that case must also match. 

Then replace the first line of the script I provided earlier with the actual project name, to make sure you then get the project key. 

Let me know how it goes. 

Like Kelly Arrey likes this
Inna S March 19, 2023

@David Fischer This fisrt line comes empty.

When I post the actual value, the script works all right. But I can't seem to get the value of this custom field from the issue.

The field is of the type 'drop down' options.

Thank you very much for trying to help, @David Fischer 

David Fischer
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
March 19, 2023

When you test this:

{%set projectName = issue.fields["Project Name"].value %}
**{{projectName}}**

What do you get? If you get just **** then it means you're not accessing the field properly. Please check using the "Issue Fields" help tab at the bottom of the editor to check how to access the Project Name field.

Inna S March 20, 2023

Great! This was a custom field so it expects the reference in the form of ... customfield_xxxx, and not the display name of the field.

This helper is really great tool.

 

Thank you very much for your help, @David Fischer !

David Fischer
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
March 20, 2023

Actually, you can refer to the field by name, but you need to make sure the name is exactly accurate - including case, leading/trailing whitespace, etc.

Also, if the solution works, can you "accept" the answer so that other community members can benefit from it? Thanks.

Like # people like this
0 votes
Oday Rafeh
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.
March 18, 2023

Hi @Inna S 

You can try to get a project key by project name in a JMWE post function, by following these steps:

  • Open the workflow and click on the transition.
  • Click 'Post Functions' and 'Add Post Function'.
  • Choose 'Create Issue(s)' and click 'Add'.
  • In the 'Project' field, use this Nunjucks template:


{% set projectName = "Your Project Name" %}
{
% set project = issue.fields.project | first("name", projectName) %} {{ project.key }}


Replace "Your Project Name" with the actual name then Configure the issue creation settings.


Click 'Add' to save the post function.


This will create an issue in the specified project based on the project name.

Inna S March 19, 2023

@Oday Rafeh but I'm not looking for the current issue project key. 

Oday Rafeh
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.
March 20, 2023

Hi @Inna S 

Try this one please 

{% set projectName = "Your Project Name" %} 
{% set allProjects = jira.searchProjects({}) %}
{% set project = allProjects | first("name", projectName) %}
{{ project.key }}
Like Inna S likes this
Inna S March 20, 2023

Thank you @Oday Rafeh 

Like Oday Rafeh likes this

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
CLOUD
PRODUCT PLAN
PREMIUM
PERMISSIONS LEVEL
Site Admin
TAGS
AUG Leaders

Atlassian Community Events