Answers.Body: <p>You can set the value and save like so:</p>
  
  <pre><code>craft()-&gt;on('entries.onSaveEntry', function(Event $event)
  {
      $entry = $event-&gt;params['entry'];
      if ($entry-&gt;section == 'mySection') {
          if ($entry-&gt;getContent()-&gt;myLightSwitchField == 1) {
              $entry-&gt;getContent()-&gt;setAttributes(array(
                  'myLightSwitchField' =&gt; 0
              ));
              craft()-&gt;entries-&gt;saveEntry($entry);
          }
      }
  });
  </code></pre>
  
  <p>However, you might consider using <code>onBeforeSaveEntry</code> so that you don't have to save the entry twice (and/or run the risk of creating an endless loop).</p>
  
  <pre><code>craft()-&gt;on('entries.onBeforeSaveEntry', function(Event $event)
  {
      $entry = $event-&gt;params['entry'];
      if ($entry-&gt;section == 'mySection') {
          $entry-&gt;getContent()-&gt;setAttributes(array(
              'myLightSwitchField' =&gt; 0
          ));
      }
  });
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-03-29T00:11:30.950Z
Answers.Text: Works perfectly.
Answers.UserDisplayName: Chris
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-03-28T22:48:53.863Z
Answers.LastActivityDate: 2016-03-28T22:48:53.863Z
Answers.UserDisplayName: Douglas McDonald
Body: <p>I have a simple plugin that fires some events entries.onSaveEntry based on if a lightswitch is turned on with that entry. What I would like to do as the last event in my plugin would be updating the value of the lightswitch with that entry to be turned off (or 0) so if you went back in to the entry it would be turned off again. </p>
  
  <p>Would appreciate any guidance. </p>
  
Tags: <plugin-development><events>
Title: Programatically change a lightswitch value on an entry on save
ThreadText: # Programatically change a lightswitch value on an entry on save
  
  (Asked by: username_0 on 2016-03-28)
  
  I have a simple plugin that fires some events entries.onSaveEntry based on if a lightswitch is turned on with that entry. What I would like to do as the last event in my plugin would be updating the value of the lightswitch with that entry to be turned off (or 0) so if you went back in to the entry it would be turned off again.
  
  Would appreciate any guidance.
  
  **Tags:** plugin-development, events
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2016-03-28. Score: 5 | Accepted answer
  
  You can set the value and save like so:
  
  ```
  craft()->on('entries.onSaveEntry', function(Event $event)
  {
      $entry = $event->params['entry'];
      if ($entry->section == 'mySection') {
          if ($entry->getContent()->myLightSwitchField == 1) {
              $entry->getContent()->setAttributes(array(
                  'myLightSwitchField' => 0
              ));
              craft()->entries->saveEntry($entry);
          }
      }
  });
  
  ```
  
  However, you might consider using `onBeforeSaveEntry` so that you don't have to save the entry twice (and/or run the risk of creating an endless loop).
  
  ```
  craft()->on('entries.onBeforeSaveEntry', function(Event $event)
  {
      $entry = $event->params['entry'];
      if ($entry->section == 'mySection') {
          $entry->getContent()->setAttributes(array(
              'myLightSwitchField' => 0
          ));
      }
  });
  
  ```
  
  > **Comments:**
  > **username_0**: Works perfectly.  
  
  ---
  
  