<p>After you call <code>$criteria-&gt;first()</code>, you no longer have an <a href="https://craftcms.com/docs/templating/elementcriteriamodel" rel="nofollow">ElementCriteriaModel</a>, but an actual <a href="https://craftcms.com/docs/templating/entrymodel" rel="nofollow">EntryModel</a>, so probably more semantic to change to:</p>

<pre><code>$criteria = craft()-&gt;elements-&gt;getCriteria(ElementType::Entry);
$criteria-&gt;section = 'affiliates';          
$criteria-&gt;limit = null;
$criteria -&gt; search ='email:'.$email;
$entry = $criteria-&gt;first();

$entry-&gt;getContent()-&gt;setAttribute("optOut", "true");

craft()-&gt;entries-&gt;saveEntry($entry);
return $entry-&gt;title;
</code></pre>

<p>But my guess about your actual problem is you're getting a validation error.  You can verify and guard against that in your code by doing something like:</p>

<pre><code>if (craft()-&gt;entries-&gt;saveEntry($entry))
{
    return $entry-&gt;title;
}
else
{
    $errors = $entry-&gt;getErrors();
}
</code></pre>

CC BY-SA 3.0
2016-08-25T02:30:37.767Z
2016-08-25T02:30:37.767Z
Brad Bell
<p>I need to update the value of an entry attribute programatically. I've tried a couple things, but the record is not updated. The code I am using is below.</p>

<pre><code>      $criteria = craft()-&gt;elements-&gt;getCriteria(ElementType::Entry);
      $criteria-&gt;section = 'affiliates';          
      $criteria-&gt;limit = null;
      $criteria -&gt; search ='email:'.$email;
      $criteria = $criteria -&gt; first();

      $criteria-&gt;getContent()-&gt;setAttribute("optOut", "true");

      craft()-&gt;entries-&gt;saveEntry($criteria);
      return $criteria-&gt;title;
</code></pre>

<plugin-development><entry>
Updating an entry record programatically?
# Updating an entry record programatically?

(Asked by: username_0 on 2016-08-25)

I need to update the value of an entry attribute programatically. I've tried a couple things, but the record is not updated. The code I am using is below.
      $criteria = craft()->elements->getCriteria(ElementType::Entry);
      $criteria->section = 'affiliates';          
      $criteria->limit = null;
      $criteria -> search ='email:'.$email;
      $criteria = $criteria -> first();

      $criteria->getContent()->setAttribute("optOut", "true");

      craft()->entries->saveEntry($criteria);
      return $criteria->title;

**Tags:** plugin-development, entry

---

## 1 answers

### Answer by username_1 on 2016-08-25. Score: 4 | Accepted answer

After you call $criteria->first(), you no longer have an ElementCriteriaModel, but an actual EntryModel, so probably more semantic to change to:
$criteria = craft()->elements->getCriteria(ElementType::Entry);
$criteria->section = 'affiliates';          
$criteria->limit = null;
$criteria -> search ='email:'.$email;
$entry = $criteria->first();

$entry->getContent()->setAttribute("optOut", "true");

craft()->entries->saveEntry($entry);
return $entry->title;

But my guess about your actual problem is you're getting a validation error.  You can verify and guard against that in your code by doing something like:
if (craft()->entries->saveEntry($entry))
{
    return $entry->title;
}
else
{
    $errors = $entry->getErrors();
}


---

