Answers.Body: <p>Instead of using <code>.first()</code> try using <code>.limit()</code>:</p>
  
  <pre><code>craft.entries.section('section').lightSwitchField('1').order('RAND()').limit(1)
  </code></pre>
  
  <p>I have just ran a test and this is working fine for me.</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>I am using this code without any errors:</p>
  
  <pre><code>{% set entry = craft.entries.section('lorem').lightSwitchField('1').order('RAND()').first %}
  
  {% if entry|length %}
      {{ entry.title }}
  {% endif %}
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-01-19T14:51:33.620Z
Answers.Text: I'm now getting "Object of class Craft\ElementCriteriaModel could not be converted to string". I'm assuming since **craft.entries** doesn't return the first model/entry found. Any idea around this without having to run a for loop on the reponse or variable[0]? **set entry = craft.entries.section('section').lightSwitchField('1').order('RAND()').limit(1).first()**
Answers.UserDisplayName: naboovalley
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-01-19T15:02:26.303Z
Answers.Text: @naboovalley I have edited my answer with the working test code I am using. So this searches the section for all entries that have the lightswitch ticked, outputs them randomly but shows the first one, and without using a for-loop. Is this what you are after?
Answers.UserDisplayName: Jamie Wade
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-01-19T15:47:53.397Z
Answers.Text: Your code is now exactly like my code from my question which isn't working for me. If I use the ".limit(1)" option instead I'm getting the correct result and then I can use **entry[0].title** instead of **entry.title** but it feels like a workaround.
Answers.UserDisplayName: naboovalley
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-01-19T15:51:03.657Z
Answers.Text: Actually this isn't working either for me. Maybe I need to take this to Craft support..
Answers.UserDisplayName: naboovalley
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-01-19T15:57:28.280Z
Answers.Text: @naboovalley Very strange, I literally took it back to basics when testing that code. Added the lightswitch field, ticked a few random entries and it was all working absolutely fine. If that code definitely isn't working for you then there must be something else going on here, I'll wait and see what other answers people come back with!
Answers.UserDisplayName: Jamie Wade
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-01-19T14:44:07.720Z
Answers.LastActivityDate: 2015-01-19T15:01:27.413Z
Answers.LastEditDate: 2015-01-19T15:01:27.413Z
Answers.UserDisplayName: Jamie Wade
Body: <p>I'm getting mixed results when using a lightswitch field together with <strong>order('RAND()')</strong> and I wonder if I'm doing something wrong.</p>
  
  <p>For example, this code gives me the correct results:</p>
  
  <pre><code>craft.entries.section('section').lightSwitchField('1').first()
  </code></pre>
  
  <p>... but this selects entries from all entries in my channel instead of just the ones with the lightswitch field switched on.</p>
  
  <pre><code>craft.entries.section('section').lightSwitchField('1').order('RAND()').first()
  </code></pre>
  
Tags: <entries><field><lightswitch>
Title: How can I get a lightswitch to work with order() in craft.entries?
ThreadText: # How can I get a lightswitch to work with order() in craft.entries?
  
  (Asked by: username_0 on 2015-01-19)
  
  I'm getting mixed results when using a lightswitch field together with **order('RAND()')** and I wonder if I'm doing something wrong.
  
  For example, this code gives me the correct results:
  
  ```
  craft.entries.section('section').lightSwitchField('1').first()
  
  ```
  
  ... but this selects entries from all entries in my channel instead of just the ones with the lightswitch field switched on.
  
  ```
  craft.entries.section('section').lightSwitchField('1').order('RAND()').first()
  
  ```
  
  **Tags:** entries, field, lightswitch
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2015-01-19. Score: 1
  
  Instead of using `.first()` try using `.limit()`:
  
  ```
  craft.entries.section('section').lightSwitchField('1').order('RAND()').limit(1)
  
  ```
  
  I have just ran a test and this is working fine for me.
  
  **EDIT**
  
  I am using this code without any errors:
  
  ```
  {% set entry = craft.entries.section('lorem').lightSwitchField('1').order('RAND()').first %}
  
  {% if entry|length %}
      {{ entry.title }}
  {% endif %}
  
  ```
  
  > **Comments:**
  > **username_0**: I'm now getting "Object of class Craft\ElementCriteriaModel could not be converted to string". I'm assuming since **craft.entries** doesn't return the first model/entry found. Any idea around this without having to run a for loop on the reponse or variable[0]? **set entry = craft.entries.section('section').lightSwitchField('1').order('RAND()').limit(1).first()**  
  > **username_1**: @username_0 I have edited my answer with the working test code I am using. So this searches the section for all entries that have the lightswitch ticked, outputs them randomly but shows the first one, and without using a for-loop. Is this what you are after?  
  > **username_0**: Your code is now exactly like my code from my question which isn't working for me. If I use the ".limit(1)" option instead I'm getting the correct result and then I can use **entry[0].title** instead of **entry.title** but it feels like a workaround.  
  > **username_0**: Actually this isn't working either for me. Maybe I need to take this to Craft support..  
  > **username_1**: @username_0 Very strange, I literally took it back to basics when testing that code. Added the lightswitch field, ticked a few random entries and it was all working absolutely fine. If that code definitely isn't working for you then there must be something else going on here, I'll wait and see what other answers people come back with!  
  
  ---
  
  