Using IFTTT and Velocity to Create bulk JIRA issues by iterating over a comma separated list
Asked by John Hamilton:

I'm using Confiforms to try and build a WebService Request to try and create a set of tasks based on the number of items that the user supplies into a Text field. The Text field format should be a comma separated list, and is stored in "TestScriptsToRun".

I have all the linkages working, and am able to submit single creates, but I can't get the bulk to work...

Here's my code.

```
{    "issueUpdates":[		#set($TSToRun = "[entry.TestScriptsToRun.split(,).toArray(')]")	#foreach ($script in $TSToRun)? ? {	  "fields": {         "project":         {            "key": "PRJT"         },         "summary": "Run $script for [entry.SourceStory.key]",	     "components": [{"name": "Software"}],         "description": "\n\n**Automatically Generated from Confluence**\n\n",	     "labels": [ "AutomatedTask" ], ? ? ?   "issuetype": {            "name": "Story"         }      }	  #if($script != $TSToRun.get())	},	  #end	#end	}  ]}

```

Here is the error I receive.

* Requested to https://\<serveraddress\>/rest/api/2/issue/bulk has resulted in an error. Response code = 400 (method ='POST'). Response contents: {"errorMessages":\["Unexpected close marker '}': expected '\]' (for ARRAY starting at \[Source: org.apache.catalina.connector.CoyoteInputStream@13dc366; line: 2, column: 4\])\\n at \[Source: org.apache.catalina.connector.CoyoteInputStream@13dc366; line: 5, column: 3\]"\]}

Thanks for your help!


Answers:
author: Alex Medved _ConfiForms_
body: Hi [@John Hamilton](/t5/user/viewprofilepage/user-id/5350871) and welcome to this community

In ConfiForms the template are processed as follows:

1. Evaluated as Velocity template
2. Processed for \[entry.field_name\] notations and expressions

In other words the
>
> ```
> #set($TSToRun = "[entry.TestScriptsToRun.split(,).toArray(')]")
> ```

Does not get the value from TestScriptsToRun field when this is processed as a Velocity template

Alex

comments: author: John Hamilton
body: [@nurac](/t5/user/viewprofilepage/user-id/1079): Thanks! Any ideas on how to get this where I want it? If \[entry.TestScriptsToRun\] looks like:

TEST_123_01, TEST_123_02, TEST123_03

is there a way to get the velocity template to work?


author: Alex Medved _ConfiForms_
body: Something like this

```
#foreach ($script in ${TestScriptsToRun.split(",")})  ${script} <br/>#end 
```

(of course not the \<br/\> in there... I just did a test with a PlainView for a quicker debugging method)

![Screenshot 2023-10-28 at 09.28.46.png](https://community.atlassian.com/t5/image/serverpage/image-id/288891i250284E2E3275BD4/image-size/large?v=v2&px=999 "Screenshot 2023-10-28 at 09.28.46.png")

Alex


author: John Hamilton
body: Thanks Alex! I got the above working in PlainView, now I'm working on the second condition (the if statement) that will allow me to push add in the correct closing bracket for the JSON, but only for the last item in the split.

Here's what I've got. I can't get the equality to fire...

```
#foreach ($script in ${TestScriptsToRun.split(",")})?  ${velocityCount} ${script}<br/>? ?[entry.TestScriptsToRun.split(",").get()] <br/>? ?#if(${script} == ${TestScriptsToRun.split(",").get()})      "Addition at the end" <br/>   #end#end
```

Appreciate your help!


author: Alex Medved _ConfiForms_
body: I dont think there is a velocityCount - as Confluence uses a pretty old version of Velocity templates <https://velocity.apache.org/engine/1.7/user-guide.html>

The best option is to iterate through and get the last one set to some extra variable you define on top of the foreach loop


author: John Hamilton
body: Awesome! Took a while, but I finally got it working, and it seems to create the right JSON. Unfortunately, now I'm getting a different error.

```
Requested to https://<server>/rest/api/2/issue/bulk has resulted in an error. Response code = 400 (method ='POST'). Response contents: {"errorMessages":["Unexpected character ('?' (code 194)): expected a valid value (number, String, array, object, 'true', 'false' or 'null')\n at [Source: org.apache.catalina.connector.CoyoteInputStream@26a77e56; line: 3, column: 3]"]}
```

Not sure what to do here. To troubleshoot, I copied the script from the "No Format" box to the "PlainView" box with no changes. The JSON that is produced works in a JSON parser and validator against the JIRA API.

I have success when I create a single issue without any of the Velocity scripting (through the create single issue call), but I can't get it to work through this call.

Here's my current code:

```
#set ($myLast = "")#foreach ($script in ${TestScriptsToRun.split(",")}) #set ($myLast = $script)#end{    "issueUpdates":[ #foreach ($script in ${TestScriptsToRun.split(",")})? ? {   "fields": {         "project":         {            "key": "PRJT"         },         "summary": "Run $script for [entry.SourceStory.key.escapeJSON]",      "components": [{"name": "Software"}],         "description": "\n\n**Automatically Generated from Confluence**\n\n",      "labels": [ "AutomatedTask" ], ? ? ?   "issuetype": {            "name": "Story"         }      }   #if(${script} != ${myLast}) },   #end #end }  ]}
```

Any idea what I've missed?


author: Alex Medved _ConfiForms_
body: Could be a last "," ?

Jira is known to be very tentative about the JSON posted... and vague about the error messages

Alex


author: John Hamilton
body: The error states it's getting some form of

```
'?' (code 194)
```

in the file. I have no idea how it would get that.

I put the JIRA API JSON Schema and my resulting JSON into a parser, and it all looks just fine... I don't see any extra commas anywhere.

Here's the raw output from the "Plainview" box.

```
{ "issueUpdates":[     { "fields": { "project": { "key": "PLGN" }, "summary": "Run PLGN_999_99 for PLGN-3002", "components": [{"name": "Software"}], "description": "\n\n**Automatically Generated from Confluence**\n\n", "labels": [ "AutomatedTask" ],       "issuetype": { "name": "Story" } } },     { "fields": { "project": { "key": "PLGN" }, "summary": "Run PLGN_999_98 for PLGN-3002", "components": [{"name": "Software"}], "description": "\n\n**Automatically Generated from Confluence**\n\n", "labels": [ "AutomatedTask" ],       "issuetype": { "name": "Story" } } } ] }

```

And here's what it looks like when prettied up a little.

```
{
  "issueUpdates": [
    {
      "fields": {
        "project": {
          "key": "PLGN"
        },
        "summary": "Run PLGN_999_99 for PLGN-3002",
        "components": [
          {
            "name": "Software"
          }
        ],
        "description": "\n\n**Automatically Generated from Confluence**\n\n",
        "labels": [
          "AutomatedTask"
        ],
        "issuetype": {
          "name": "Story"
        }
      }
    },
    {
      "fields": {
        "project": {
          "key": "PLGN"
        },
        "summary": "Run PLGN_999_98 for PLGN-3002",
        "components": [
          {
            "name": "Software"
          }
        ],
        "description": "\n\n**Automatically Generated from Confluence**\n\n",
        "labels": [
          "AutomatedTask"
        ],
        "issuetype": {
          "name": "Story"
        }
      }
    }
  ]
}
```


author: Alex Medved _ConfiForms_
body: Could you post the same json directly using the Jira REST API?


author: John Hamilton
body: Do you have a recommended way of doing that?


author: Alex Medved _ConfiForms_
body: For example as shown in the official examples by Atlassian <https://developer.atlassian.com/server/jira/platform/jira-rest-api-examples/>


author: John Hamilton
body: After a little trial and error, I was able to get it to post successfully using the command line. I have created stories in JIRA from the raw JSON. I got it to work with both the single line, and with the "pretty" formatted line. Everything looks right on the individual issues.

Unfortunately, still no luck using the Confiforms Webservice Call...

Is there any way to pickup the exact JSON that's being sent by Confiforms?

Any other ideas?


author: Alex Medved _ConfiForms_
body: The best way is to check with Confluence logs - that should be logged there

Alex


author: John Hamilton
body: Unfortunately, we can't find it in the Confluence logs. We've got Debug turned on for the form, and there was additional debugging info, but couldn't find the JSON that was sent. Any other ideas?


author: Alex Medved _ConfiForms_
body: Hi

If you have enabled DEBUG level logs for ConfiForms as per <https://wiki.vertuna.com/display/CONFIFORMS/How+to+enable+debug+logging+for+ConfiForms+app> then the logging is very verbose - it logs the JSON it is about to post and logs a lot of details if the request has failed

You should see something like from a ConfiForms logger  

```
"Could not complete request, <SOME_MESSAGE_HERE> Used input: '<YOUR_JSON>'"
```

Alex


author: John Hamilton
body: First problem solved, it was barfing on tab characters in the input. So, if anybody else gets Code 194 errors, it's likely due to somebody (in this case me) using tab characters instead of spaces. Really, I should know better by now!

Onto the second problem: It appears as though we aren't authenticating with the JIRA instance through the webservice call. The admins don't believe this is doable in our security context, so I'm stuck again, and may be forced to using the "Create JIRA Issue" call (which I know works, but only creates a single issue).

Any ideas on how to take a single form submission and create multiple form entries (to trigger multiple "Create JIRA Issue" IFTTT calls?

If not, I may have to resort to "Here's your Excel Template. Fill it out, and upload..."


author: Alex Medved _ConfiForms_
body: You can call any API endpoint of the app connected through the application link using the <https://wiki.vertuna.com/display/CONFIFORMS/Configuring+ConfiForms+IFTTT+actions+and+rules#ConfiguringConfiFormsIFTTTactionsandrules-ApplinkService>


author: John Hamilton
body: That did it! Works awesome!


Tags: confiforms

confluence-server

jira

server