Answers: <p>I figured out what was going on. The return from <code>hosts</code> var was a single string with comma separated hostnames <code>host1, host2</code>, I had to convert it to List  using the comma as delimiter. The following post <a href="https://stackoverflow.com/questions/7488643/how-to-convert-comma-separated-string-to-list">How to convert comma-separated String to List?</a> helped me with that task.</p>
  <p>Basically the last section now reads</p>
  <pre><code>                script: 
                  &quot;&quot;&quot;
                  List&lt;String&gt; list = Arrays.asList(hosts.split(&quot;\\\\s*,\\\\s*&quot;));
                  return list
                  &quot;&quot;&quot;
  </code></pre>
  
Answers: CC BY-SA 4.0
Answers: 2021-03-12T09:47:13.807Z
Answers: 2021-03-12T14:50:19.870Z
Answers: Filip Nikolov
Body: <p>I have been trying unsuccessfully to get DynamicReferenceParameter of the Active Choice plugin to show previously selected values as list, however it either shows them on one line or it treats each symbol as a new element in the list.</p>
  <pre><code>hosts_script = '''
  return['host1', 'host2', 'host3']
  '''
  
  properties([
      parameters([
        [$class: 'ChoiceParameter',
          choiceType: 'PT_MULTI_SELECT',
          description: 'Select one or more hosts',
          filterLength: 1,
          filterable: true,
          name: 'hosts',
          randomName: 'choice-parameter-5631314439613978',
          script: [
              $class: 'GroovyScript',
              fallbackScript: [
                  classpath: [],
                  sandbox: true,
                  script:
                      'return[\'Could not get hosts\']'
              ],
              script: [
                  classpath: [],
                  sandbox: true,
                  script: hosts_script,
              ]
          ]
      ],
      [$class: 'DynamicReferenceParameter', 
          choiceType: 'ET_UNORDERED_LIST', 
          description: 'These are the selected hosts', 
          name: 'Selected hosts', 
          omitValueField: false, 
          randomName: 'choice-parameter-5693388460832175', 
          referencedParameters: 'hosts',
          script: [
              $class: 'GroovyScript', 
              fallbackScript: [
                  classpath: [], 
                  sandbox: true, 
                  script: 
                      'return[\'Could not get hosts\']'
              ], 
              script: [
                  classpath: [], 
                  sandbox: true,
                  script: 
                      &quot;&quot;&quot;
                      //def list = hosts.replaceAll( '&quot;','\\'' )
                      //List list = hosts.collect{ '\\'' + it + '\\','}
                      //def list1 = new ArrayList(Arrays.asList(hosts))
                      List list = hosts.collect{ '\\'' + it + '\\','} 
                      //String list = hosts.inspect()
  
                      return list
                      &quot;&quot;&quot;
                ]
            ]
         ]
      ])
   ])
  </code></pre>
  <p>I can get this <code> • host1,host2</code></p>
  <p>Or this:</p>
  <pre><code> •'h',
   •'o',
   •'s',
   •'t',
   •'2',
  </code></pre>
  <p>Where I want this:</p>
  <pre><code> • host1
   • host2
  </code></pre>
  <p>At the very end you will see what I have been playing with ... the biggest problem for me is that is impossible to use <code>println</code> to troubleshoot.</p>
  
Tags: <jenkins><groovy><jenkins-plugins>
Title: Jenkins - Active Choice DynamicReferenceParameter not returning list
UserDisplayName: Filip Nikolov
ThreadText: # Jenkins - Active Choice DynamicReferenceParameter not returning list
  
  (Asked by: username_0 on 2021-03-11)
  
  I have been trying unsuccessfully to get DynamicReferenceParameter of the Active Choice plugin to show previously selected values as list, however it either shows them on one line or it treats each symbol as a new element in the list.
  
  ```
  hosts_script = '''
  return['host1', 'host2', 'host3']
  '''
  
  properties([
      parameters([
        [$class: 'ChoiceParameter',
          choiceType: 'PT_MULTI_SELECT',
          description: 'Select one or more hosts',
          filterLength: 1,
          filterable: true,
          name: 'hosts',
          randomName: 'choice-parameter-5631314439613978',
          script: [
              $class: 'GroovyScript',
              fallbackScript: [
                  classpath: [],
                  sandbox: true,
                  script:
                      'return[\'Could not get hosts\']'
              ],
              script: [
                  classpath: [],
                  sandbox: true,
                  script: hosts_script,
              ]
          ]
      ],
      [$class: 'DynamicReferenceParameter', 
          choiceType: 'ET_UNORDERED_LIST', 
          description: 'These are the selected hosts', 
          name: 'Selected hosts', 
          omitValueField: false, 
          randomName: 'choice-parameter-5693388460832175', 
          referencedParameters: 'hosts',
          script: [
              $class: 'GroovyScript', 
              fallbackScript: [
                  classpath: [], 
                  sandbox: true, 
                  script: 
                      'return[\'Could not get hosts\']'
              ], 
              script: [
                  classpath: [], 
                  sandbox: true,
                  script: 
                      """
                      //def list = hosts.replaceAll( '"','\\'' )
                      //List list = hosts.collect{ '\\'' + it + '\\','}
                      //def list1 = new ArrayList(Arrays.asList(hosts))
                      List list = hosts.collect{ '\\'' + it + '\\','} 
                      //String list = hosts.inspect()
  
                      return list
                      """
                ]
            ]
         ]
      ])
   ])
  
  ```
  
  I can get this  `• host1,host2`
  
  Or this:
  
  ```
   •'h',
   •'o',
   •'s',
   •'t',
   •'2',
  
  ```
  
  Where I want this:
  
  ```
   • host1
   • host2
  
  ```
  
  At the very end you will see what I have been playing with ... the biggest problem for me is that is impossible to use `println` to troubleshoot.
  
  **Tags:** jenkins, groovy, jenkins-plugins
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_0 on 2021-03-12. Score: 1
  
  I figured out what was going on. The return from `hosts` var was a single string with comma separated hostnames `host1, host2`, I had to convert it to List using the comma as delimiter. The following post [How to convert comma-separated String to List?](https://stackoverflow.com/questions/7488643/how-to-convert-comma-separated-string-to-list) helped me with that task.
  
  Basically the last section now reads
  
  ```
                  script: 
                  """
                  List<String> list = Arrays.asList(hosts.split("\\\\s*,\\\\s*"));
                  return list
                  """
  
  ```
  
  
  ---
  
  