Update entry programatically Im trying to update an entry field programatically. I can load an entry by any field with a function i created

    /**
 * Returns an element by any attribute.
 *
 * @param int $elementId
 * @param string|null $type
 * @param string|null $localeId
 * @param string $localeId
 * @return BaseElementModel|null
 */
public function getElementByField($elementId, $elementType = null, $localeId = null, $field)
{
    if (!$elementId)
    {
        return null;
    }

    if (!$elementType)
    {
        $elementType = craft()-&gt;elements-&gt;getElementTypeById($elementId);

        if (!$elementType)
        {
            return null;
        }
    }

    $criteria = craft()-&gt;elements-&gt;getCriteria($elementType);
    $criteria-&gt;$field = $elementId;
    $criteria-&gt;locale = $localeId;
    $criteria-&gt;status = null;
    $criteria-&gt;localeEnabled = null;
    return $criteria-&gt;first();
}


but i cant save the new values of the fields i want.
This is my save code:

$entry = $this-&gt;getElementByField("3081001100390", ElementType::Entry, null, "variantSku");
    $entry-&gt;variantSku = 1;
    craft()-&gt;entries-&gt;saveEntry($entry);


And the error is  Property "Craft\EntryModel.variantSku" is not defined. I know the variantSku exists since im loading a product by that attribute and i can see it when dumping $entry->variantSku.

Complete error here: https://www.diigo.com/item/image/4u33k/972k

Did anyone achieved this?
After some research, i found a way to achieve this. Here is my full controller:

&lt;?php
  namespace Craft;

  class Module_ControlController extends BaseController {
   protected $allowAnonymous = true;
public function actionChangeStock()
{
    $entry = $this-&gt;getElementByField("3081001100390", ElementType::Entry, null, "variantSku");
    $entry-&gt;enabled = false;
    $entry-&gt;getContent()-&gt;setAttributes(array(
        'variantSku' =&gt; "Hello my friend!",
        'variantStock'  =&gt; "3"
    ));
    $success = craft()-&gt;entries-&gt;saveEntry($entry);
    die(var_dump($success));
}

/**
 * Returns an element by any attribute.
 *
 * @param int $elementId
 * @param string|null $type
 * @param string|null $localeId
 * @return BaseElementModel|null
 */
public function getElementByField($elementId, $elementType = null, $localeId = null, $field)
{
    if (!$elementId)
    {
        return null;
    }

    if (!$elementType)
    {
        $elementType = craft()-&gt;elements-&gt;getElementTypeById($elementId);

        if (!$elementType)
        {
            return null;
        }
    }

    $criteria = craft()-&gt;elements-&gt;getCriteria($elementType);
    $criteria-&gt;$field = $elementId;
    $criteria-&gt;locale = $localeId;
    $criteria-&gt;status = null;
    $criteria-&gt;localeEnabled = null;
    return $criteria-&gt;first();
}}