Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Add a list of items as values to a custom field

Ashwin Balakrishnan
Contributor
March 9, 2020

I have created a custom field "customer name" which is of type "Select list (multiple choices)". I have a list of 1000 customer names to add to this list. I am sure adding it manually is the slow route. Is there a way to add this programmatically or through script.  We have scriptrunner installed in our Jira cloud. Can I make use of that to add of these 1000 names to the custom field ?

 

--Sunray

6 answers

1 vote
Payne
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 10, 2020

Here is a script that can be run in the Script Console to populate a select list:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.customfields.manager.OptionsManager
import com.atlassian.jira.issue.customfields.option.Option
import com.atlassian.jira.issue.customfields.option.Options
import com.atlassian.jira.issue.fields.config.FieldConfig
import com.atlassian.jira.issue.fields.config.FieldConfigScheme
import com.atlassian.jira.issue.fields.CustomField

CustomField customField = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectsByName("Toppings")[0];
List<String> values = ["Pepperoni","Sausage","Onions"]

if (customField != null) {
List<FieldConfigScheme> schemes = customField.getConfigurationSchemes();
if (schemes != null && !schemes.isEmpty()) {
FieldConfigScheme sc = schemes.get(0);
Map configs = sc.getConfigsByConfig();
if (configs != null && !configs.isEmpty()) {
FieldConfig config = (FieldConfig) configs.keySet().iterator().next();
OptionsManager optionsManager = ComponentAccessor.getOptionsManager();
Options options;
int nextSequence;
for(int i=0;i<values.size();i++)
{
options = optionsManager.getOptions(config);
nextSequence = options.isEmpty() ? 1 : options.getRootOptions().size() + 1;
optionsManager.createOption(config, null, (long) nextSequence, values[i]);
}
}
}
}
Ashwin Balakrishnan
Contributor
March 11, 2020

Thanks

0 votes
Ashwin Balakrishnan
Contributor
March 9, 2020

Hi,

 

I used the following code to add the asset 

 

curl -X PUT \
https://<<my jira url>>/rest/assetapi/asset \
-H 'Content-Type: application/json' \
-H 'cache-control: no-cache' \
-d '{
"origin": {
"appKey": "my-app",
"originId": "customername1"
},
"label": {
"value": "customername1"
}
}'

 

When I ran this curl , I am able to add the asset. The "customername1" shows up under the custom field that I had created. In a ticket, I can choose this asset. However, when I run jql query, I am not able to search for it.   Any reason why?

My custom field is "customer asset" so I am trying to do the jql search as 

"Customer Asset" = "customername1"  but the result is empty. 

0 votes
Phill Fox
Community Champion
March 9, 2020

Hi @Ashwin Balakrishnan 

The use case you describe is one of the options for which assets were developed. The important part to do is to preload your asset list in the system so that you can define the filter to select the options you want to display.

You will only need to define a filter if you have different lists of assets and need to distinguish between them. 

Once you have your list loaded you can link multiple entries to an issue. See 

https://www.dropbox.com/s/jakkfdsk96ez8cd/Screenshot%202020-03-09%2021.31.45.png?dl=0 

for an example with the list of UK Universities. 

 

Phill

Ashwin Balakrishnan
Contributor
March 9, 2020

How can I preload the asset list in the system ?

Phill Fox
Community Champion
March 9, 2020

Hi

 

You can do it in bulk using the instructions at https://developer.atlassian.com/cloud/assetsapi/rest/#api-asset-bulk-put 

this requires you to fill in some details in a curl command with the details for your asset. This will allow you to add 25 at a time so it is best to do this programmatically. 

This is an example of the command

curl --request PUT \
  --url '<yourhost>/rest/assetapi/asset/bulk' \
  --header 'Content-Type: application/json' \
  --data '{
  "putRequest": [
    {
      "origin": {
        "appKey": "com.myasset.app",  
        "originId": "5-113-51143-2032"
      },
      "label": {
        "value": "MacBook Pro 15\" 2016"
      },
      "type": {
        "appKey": "com.myasset.app",
        "originId": "5-113-51143-2032"
      },
      "assignee": {
        "accountId": "27505:c73cd17f-ae93-4f10-911c-d754d02420be",
        "email": "test@example.com"
      },
      "fields": [
        {
          "fieldId": "RAM",
          "value": "8GB DDR4 2400MHz"
        }
      ]
    }
  ]
}'

A couple of things to notice here. 

com.myasset.app is a reference to your appKey and can be any value.

originid is a UUID to uniquely identify your app and the type definition must match the settings defined in your assettype.

label is what will be seen in the interface 

assignee and fields are optional.

 

Phill

0 votes
Ashwin Balakrishnan
Contributor
March 9, 2020

Hi @Phill Fox 

Thanks for replying. I created a custom field with type as "Asset" . I think added this custom field to few screens. When I go to "contexts and default value", I get an option "Edit filters". When I click on "Edit Filters", it is asking "Choose a type".

Is there a way I can create a filter with all the customer names so I can choose this filter so these names show up in the field while creating a ticket ?

0 votes
Phill Fox
Community Champion
March 9, 2020

Hi @Ashwin Balakrishnan 

I assume that you use something to manage your list of customer names and want to be able to easily keep the list in sync when a new customer needs to be added or even a name changed. 

Whilst you could manage this with a select list (multiple choices) let me introduce a new option in Cloud to you which is to use the custom field of asset.  

This is defined at https://developer.atlassian.com/cloud/assetsapi/rest/ but here is a quick overview for you to consider if this is suitable for you. 

1. Define an asset type in which you are going to store your options. 

2. Create an asset of the asset type defined in step 1.

3. Make sure you have a list of options with a permanent id and load these in to your asset type defined in step 2. 

3. Add the asset to which ever screens you want to use it on.

 

Regards

 

Phill

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events