How to add (copy) values from checkbox to labels (update not set) using automation

Adam Dulberg
Contributor
January 30, 2023

On both Cloud and Server I'm running into the same issue.

I am trying to copy the values of a checkbox (multi-select) to labels whenever a field value is changed for that field called "Test Checkbox". This is not a set or fields operation since this custom field is on a transition screen and can be sent back and forth (thus adding distinct values through the transition). On any pass forward through the transition, I'd like to add any new value. Not remove any past values to labels.

I've attempted to use smart values for the 'values to add' by following the guidance I've seen in other posts here like using list.join. That does not seem to work in Cloud and I'm unable to replicate it in Server.

I'm left with the additional fields Json response which I think is the right move, but am unable to figure out how to add any number of checkbox values at the same time (adding to the current labels array). Could someone help me understand the adjustment I need to make? (Been consulting this doc)

Screenshot 2023-01-30 181202.png

 

 

2 answers

1 accepted

2 votes
Answer accepted
Darryl Lee
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
January 31, 2023

Hey there @Adam Dulberg !

Every label you add has to be in a separate... "add object", so just expanding the list with .asJsonStringArray doesn't work, because I think you'd end up with:

"add": ["all", "of", "the", "labels"] 

So I ended up having to use Automation's list iterator operator (#) to go through each checkbox and put it into the expected format:

{
  "update": {
    "labels": [
      {{#issue.Checkboxes}}{"add": "{{.}}"}{{^last}},{{/}}{{/}}
    ]
  }
}  

This then should expand to:

{
  "update": {
    "labels": [
      {"add": "all"},{"add": "of"},{"add": "the"},{"add": "labels"}
    ]
  }
}   

Which worked in my testing.

OH, I'm reading about JSON functions and I think this should work but fails:

{
  "update": {
    "labels":
      {{issue.Checkboxes.asJsonObjectArray("add")}}
  }
}

Weirdly {{issue.Checkboxes.asJsonObjectArray("add")}} is returning an empty array [], even as other JSON functions seem to be working perfectly fine. From my logs:

asJsonStringArray: ["Bar","Bat"]
asJsonArray: [Bar,Bat]
asJsonObjectArray("add"): []
Hey @Bill Sheboy - I see you've successfully advised using asJsonObjectArray in the past. Can you think of why it's not working for me above?
Bill Sheboy
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.
January 31, 2023

Hi @Adam Dulberg and @Darryl Lee 

Darryl, when using asJsonObjectArray in this way you first specify the key you want from the structure (e.g. "value" for a checkbox field) and after that, use a replace() function to substitute for the desired field name.

I recommend writing the function call to the log to learn why it is not working as expected. 

You may also try the hard-coded approach you noted to set up the JSON expression, and replace {{.}} in your iterator with {{value}}

Kind regards,
Bill

Darryl Lee
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
January 31, 2023

Ooooh, thanks @Bill Sheboy! I was totally misunderstanding what the key asJsonObjectArray was doing. I thought it let you arbitrarily assign a key to every element of an array, and ALSO I mistakenly thought that {{issue.Checkboxes}} was an array since asJsonArray and asJsonStringArray, were perfectly happy to return lists of values without me having to specify the key.

And this after I already found and read your previous answer: Automation for Jira: JSON copy People custom field. (Clearly I didn't fully understand your answer. I actually was confused about the replace.)

Alas, now that I've got it "right" (at least in theory), Automation still doesn't like my JSON.

This:

{{issue.Checkboxes.asJsonObjectArray("value").replace("value","add")}}

Gives me this:

[{ "add": "Foo" },{ "add": "Bar" }]

Which should totally be acceptable here:

{
  "update": {
    "labels":
      {{issue.Checkboxes.asJsonObjectArray("value").replace("value","add")}}
  }

But nope. "Error while parsing additional fields. Not valid JSON."

I output the aforementioned to Audit logs, https://jsonlint.com/ validated the output as perfectly valid.

{   "update": {     "labels":       [{ "add": "Foo" },{ "add": "Bat" }]   } } 

Weird.

Darryl Lee
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
January 31, 2023

Also, I wonder if to be careful in case one of the checkbox options contained the word "value" you might want to use this as your replacement:

replace("\"value\"","\"add\"")

And there's still the edge case that one of the options is actually "value". 

But this is all academic until I can figure out what Automation doesn't like about my JSON.

Darryl Lee
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
January 31, 2023

Oooof, on a whim, I tried deleting all the leading space (indentation) out of the Additional fields, and bizarrely, that seemed to be what Automation didn't like about my JSON.

{
"update": {
"labels":
{{issue.Checkboxes.asJsonObjectArray("value").replace("\"value\"","\"add\"")}}
}
}

So I guess in the big debate over whether to indent with tabs or spaces, Automation falls squarely into the NEITHER camp. Boo. That is super hard to read.

Sorry to be mainly talking to myself here @Adam Dulberg but I do my best work talking to a Rubber Duck.

Anyways, the code above is I suppose "slightly" cleaner, accounts for any options that might include the word "value" in them (unless the option is exactly "value") and seems to work.

And so remember NOT to indent anything because that appears to screw the Automation engine up. Whew.

Like Bill Sheboy likes this
0 votes
Adam Dulberg
Contributor
January 31, 2023

@Darryl Lee the suggestion is excellent thank you my testing shows this to be successful. I'm tentatively thinking this works for larger needs which I'll affirm/accept the answer tomorrow. In theory, I'd think the JsonObjectArray would work but I too ran into errors. I couldn't list everything I tried, yet I did not try the iterator as you suggested. Curious to see what others say.

Suggest an answer

Log in or Sign up to answer