Answers.Body: <p>Yes, the post you linked to is what you need to do.</p>
  <p>I don't know where your <code>settings</code> variable is set so you may need to adjust a bit but something like this should get you started:</p>
  <pre><code>{# Start your element query #}
  {% set query = craft
      .entries()
      .section('purchases')
  %}
  
  {# Check if settings.userId, if so, apply it to the query #}
  {% if settings.userId %}
      {% set query = query.authorId(settings.userId) %}
  {% endif %}
  
  {# Run the query #}
  {% set purchases = query.all() %}
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-11-11T21:40:20.223Z
Answers.Text: Thanks, Oli. That does it.
Answers.UserDisplayName: 4midori
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-11-09T10:29:22.160Z
Answers.LastActivityDate: 2020-11-09T10:29:22.160Z
Answers.UserDisplayName: Oli
Body: <p>In Sprout Reports, I am creating a list of users in my settings file. I first need an option with value of null.</p>
  <pre><code>{% set options = [] %}
  {% set options = options|merge([{ label: 'All users', value: null }]) %}
  {% for user in users %}
    {% set fullname = user.firstName ~ &quot; &quot; ~ user.lastName %}
      {% set options = options|merge([{ label: fullname, value: user.id }]) %}
  {% endfor %}
  
  {{ forms.selectField({
      label: &quot;Choose one&quot;|t,
      name: &quot;userId&quot;,
      options: options,
      }) 
  }}
  </code></pre>
  <p>Then, in my index file (the report), I do something like this:</p>
  <pre><code>{% set purchases = craft.entries()
    .section('purchases')
    .authorId(settings.userId)
    .all()
  %}
  </code></pre>
  <p>My query runs fine if there is a value for userId, but when it is null, the query does not return any results.</p>
  <p><strong>What am I doing wrong?</strong></p>
  <p>Do I need to do <a href="https://craftcms.stackexchange.com/questions/11870/dynamic-relatedto-queries">something like this</a>?</p>
  
Tags: <entries><plugin-sproutreports>
Title: Passing null values to queries, Sprout Reports
ThreadText: # Passing null values to queries, Sprout Reports
  
  (Asked by: username_0 on 2020-11-07)
  
  In Sprout Reports, I am creating a list of users in my settings file. I first need an option with value of null.
  
  ```
  {% set options = [] %}
  {% set options = options|merge([{ label: 'All users', value: null }]) %}
  {% for user in users %}
    {% set fullname = user.firstName ~ " " ~ user.lastName %}
      {% set options = options|merge([{ label: fullname, value: user.id }]) %}
  {% endfor %}
  
  {{ forms.selectField({
      label: "Choose one"|t,
      name: "userId",
      options: options,
      }) 
  }}
  
  ```
  
  Then, in my index file (the report), I do something like this:
  
  ```
  {% set purchases = craft.entries()
    .section('purchases')
    .authorId(settings.userId)
    .all()
  %}
  
  ```
  
  My query runs fine if there is a value for userId, but when it is null, the query does not return any results.
  
  **What am I doing wrong?**
  
  Do I need to do [something like this](https://craftcms.stackexchange.com/questions/11870/dynamic-relatedto-queries)?
  
  **Tags:** entries, plugin-sproutreports
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2020-11-09. Score: 3 | Accepted answer
  
  Yes, the post you linked to is what you need to do.
  
  I don't know where your `settings` variable is set so you may need to adjust a bit but something like this should get you started:
  
  ```
  {# Start your element query #}
  {% set query = craft
      .entries()
      .section('purchases')
  %}
  
  {# Check if settings.userId, if so, apply it to the query #}
  {% if settings.userId %}
      {% set query = query.authorId(settings.userId) %}
  {% endif %}
  
  {# Run the query #}
  {% set purchases = query.all() %}
  
  ```
  
  > **Comments:**
  > **username_0**: Thanks, Oli. That does it.  
  
  ---
  
  