title: JMeter - Variable set in preprocessor is not available to sampler
body: <p>I have a relatively simple JMeter test plan setup as shown here:
  <a href="https://imgur.com/c8BIzBB" rel="nofollow noreferrer">https://imgur.com/c8BIzBB</a></p>
  <p>The relevant part of this is the BeanShell Preprocessor (shown as <em>Setup element data</em>) and its relationship to the HTTP Request sampler (shown as <em>POST /elements</em>). Both of these are inside a Loop Controller (shown as <em>Do a few times</em>).</p>
  <p>The Preprocessor gets an array of data stored on the <code>bsh.shared</code> object and randomly selects one item. It then sets a variable called <code>elementTypeId</code>.</p>
  <p>When I run this test, <code>elementTypeId</code> gets logged (and I therefore assume is set) correctly. However, the first time around, the variable is not set correctly and still appears as <code>${elementTypeId}</code>. Further samples appear to be set but use the n-1th value.</p>
  <p>The first, failing sample is shown here: <a href="https://imgur.com/Gj2YAje" rel="nofollow noreferrer">https://imgur.com/Gj2YAje</a></p>
  <p>The final sample (and logged values) are shown here: <a href="https://imgur.com/OW5HSsS" rel="nofollow noreferrer">https://imgur.com/OW5HSsS</a></p>
  <p><strong>Setup element data - BeanShell Preprocessor code:</strong></p>
  <pre><code>import java.util.Random;
  import com.eclipsesource.json.*;
  
  Random rand = new Random();
  int idx = rand.nextInt(bsh.shared.elementTypes.size());
  
  JsonValue elementType = bsh.shared.elementTypes.get(idx);
  String elementTypeId = String.valueOf(elementType.get(&quot;id&quot;).asInt());
  log.info(elementTypeId);
  vars.put(&quot;elementTypeId&quot;, elementTypeId);
  </code></pre>
  <p>It looks to me as if the sampler is firing before the preprocessor has set the variable - which seems counter to what <em>should</em> be happening.</p>
  <h2>Update following UBIK's answer</h2>
  <p>When I disable the SetQueryParams PreProcessor, it appears that the variable is set correctly (although the request fails as it needs a query parameter to be added).</p>
  <p><strong>SetQueryParams PreProcessor:</strong></p>
  <pre><code>import org.apache.jmeter.protocol.http.sampler.HTTPSamplerBase;
  
  if (sampler instanceof HTTPSamplerBase &amp;&amp; 
  sampler.getMethod().equalsIgnoreCase(&quot;post&quot;)) {
      // add query parameter to url
      var customPath = sampler.getPath() + '?' + sampler.getQueryString();
      sampler.setPath(customPath);
  
      // remove query parameter from body
      arguments = sampler.getArguments();
      while (arguments.getArgumentCount() &gt; 1) {
          arguments.removeArgument(arguments.getArgumentCount() - 1);
      }
      sampler.setArguments(arguments);
  }
  </code></pre>
  <p>Is it possible to have both preprocessors applied to the POST request?</p>