<p>You're likely getting a validation error that you're not checking for.  Do something like this:</p>

<pre><code>if ($entry-&gt;validate())
{
    $service-&gt;saveEntry($entry);
}
else
{
    $errors = $entry-&gt;getErrors();
}
</code></pre>

<p><strong>Update:</strong></p>

<p>Do you get anything if you do:</p>

<pre><code>if (!$service-&gt;saveEntry($entry))
{
    $errors = $entry-&gt;getErrors();
}
</code></pre>

CC BY-SA 3.0
2015-06-11T00:36:28.500Z
@mmc501, did you ever come back from the holiday and figure this one out? Having the exact same problem now.
Matt Stein
CC BY-SA 3.0
2015-06-11T06:12:28.240Z
In my case I originally only had 2 entries - when I deleted those and added again everything worked fine - sorry - no ideal solution.
mmc501
CC BY-SA 3.0
2014-12-23T16:59:06.770Z
Cheers - added that it validates ok - placed that code in and some additional to send an email and it gets validated ok. Foxycart responds with a success when the data is sent.
mmc501
CC BY-SA 3.0
2014-12-23T20:38:48.560Z
But I forgot to add - the record still isn't being updated.
mmc501
CC BY-SA 3.0
2014-12-23T22:21:16.537Z
Updated answer.
Brad Bell
CC BY-SA 3.0
2014-12-23T22:40:14.287Z
Ok I have added that - I have just checked the craft.log and I see this:
[warning] [application] Craft\ContentModel->typeCategory failed validation: Type Category cannot be blank,
then: [trace] [system.db.CDbTransaction] Rolling back transaction. typeCategory is a required field for the product -  is this the issue?
mmc501
CC BY-SA 3.0
2014-12-23T22:41:13.660Z
You've got a required custom field with a handle `typeCategory`, then?
Brad Bell
CC BY-SA 3.0
2014-12-23T22:45:17.510Z
I just changed it to not required there and tested it - and the stock update worked fine. Should it be able to work with a required field?
mmc501
CC BY-SA 3.0
2014-12-23T22:46:48.120Z
Sure... but the definition of required means you need to give it a value. :)  i.e. `$entry->getContent()->setAttributes(array(
            'stock' => $newQuantity, 'typeCategory' => 'something'
        ));`
Brad Bell
CC BY-SA 3.0
2014-12-23T22:49:19.583Z
Ok, the typeCategory already had a value? Can the stock not simply be updated not touching any other fields? Or do I need to pull the existing typeCategory(category field type) and set it again?
mmc501
CC BY-SA 3.0
2014-12-23T22:53:14.253Z
Are you sure about that? If you do `$entry = $service->getEntryById($product_code); $value = $entry->typeCategory;` does it output anything? And if you load that entry in the CP, does typeCategory have a value?  Maybe there were already some existing entries and someone set `typeCategory` to required after the fact.
Brad Bell
CC BY-SA 3.0
2014-12-23T22:59:04.353Z
Yep it def has a value - and checked it in the Entry in the control panel. When I changed it to not required and processed a Foxycart order it updated the stock and kept the typeCategory value it originally had. That last point may have been the case - that specific entry was added before the field was made required. What's the best way around that - delete the entries and make the field required? Then all should be fine?
mmc501
CC BY-SA 3.0
2014-12-23T23:08:58.280Z
Yeah... either that or go through each entry that doesn't have a value and manually add one?
Brad Bell
CC BY-SA 3.0
2014-12-23T23:11:34.210Z
Cheers - they all had values though. I will delete, (only 2 test products) - thanks for the help - time to sign off for the holidays
mmc501
CC BY-SA 3.0
2014-12-23T16:50:18.797Z
2014-12-23T22:21:10.540Z
2014-12-23T22:21:10.540Z
Brad Bell
<p>I'm testing with the Foxycart Plugin to update the stock in my code.
In the FoxyCartController.php I'm able to filter through the XML feed ok and get the new Stock value but it's doesn't seem to be getting saved.</p>

<p>My current code which picks up the correct product and $newQuantity value ok - but it doesn't seem to save the $newQuantity value back to the stock field in the entry.</p>

<pre><code>//If you have custom code to run for each product, put it here:

$service = craft()-&gt;entries;

    $entry = $service-&gt;getEntryById($product_code);

    if ($entry != null) {
        $attrs = $entry-&gt;getContent();

        $newQuantity = $attrs['stock'] - $product_quantity;


        $entry-&gt;getContent()-&gt;setAttributes(array(
            'stock' =&gt; $newQuantity
        ));

        $service-&gt;saveEntry($entry);

        //return $entry;
    } else {
        //return null;
    }
</code></pre>

CC BY-SA 3.0
2016-01-14T03:32:38.500Z
Figured out a solution. Described here: http://craftcms.stackexchange.com/questions/11661/programmatically-update-entry-field/13210#13210
Brian Mathews
<plugin-development><entries><plugins><entry>
Plugin Controller - Save Entry
# Plugin Controller - Save Entry

(Asked by: username_0 on 2014-12-23)

I'm testing with the Foxycart Plugin to update the stock in my code.
In the FoxyCartController.php I'm able to filter through the XML feed ok and get the new Stock value but it's doesn't seem to be getting saved.
My current code which picks up the correct product and $newQuantity value ok - but it doesn't seem to save the $newQuantity value back to the stock field in the entry.
//If you have custom code to run for each product, put it here:

$service = craft()->entries;

    $entry = $service->getEntryById($product_code);

    if ($entry != null) {
        $attrs = $entry->getContent();

        $newQuantity = $attrs['stock'] - $product_quantity;


        $entry->getContent()->setAttributes(array(
            'stock' => $newQuantity
        ));

        $service->saveEntry($entry);

        //return $entry;
    } else {
        //return null;
    }

**Tags:** plugin-development, entries, plugins, entry

> **Comments:**
> **username_1**: Figured out a solution. Described here: http://craftcms.stackexchange.com/questions/11661/programmatically-update-entry-field/13210#13210  

---

## 1 answers

### Answer by username_2 on 2014-12-23. Score: 4 | Accepted answer

You're likely getting a validation error that you're not checking for.  Do something like this:
if ($entry->validate())
{
    $service->saveEntry($entry);
}
else
{
    $errors = $entry->getErrors();
}

Update:
Do you get anything if you do:
if (!$service->saveEntry($entry))
{
    $errors = $entry->getErrors();
}

> **Comments:**
> **username_0**: Cheers - added that it validates ok - placed that code in and some additional to send an email and it gets validated ok. Foxycart responds with a success when the data is sent.  
> **username_0**: But I forgot to add - the record still isn't being updated.  
> **username_2**: Updated answer.  
> **username_0**: Ok I have added that - I have just checked the craft.log and I see this: [warning] [application] Craft\ContentModel->typeCategory failed validation: Type Category cannot be blank, then: [trace] [system.db.CDbTransaction] Rolling back transaction. typeCategory is a required field for the product -  is this the issue?  
> **username_2**: You've got a required custom field with a handle `typeCategory`, then?  
> **username_0**: I just changed it to not required there and tested it - and the stock update worked fine. Should it be able to work with a required field?  
> **username_2**: Sure... but the definition of required means you need to give it a value. :)  i.e. `$entry->getContent()->setAttributes(array(             'stock' => $newQuantity, 'typeCategory' => 'something'         ));`  
> **username_0**: Ok, the typeCategory already had a value? Can the stock not simply be updated not touching any other fields? Or do I need to pull the existing typeCategory(category field type) and set it again?  
> **username_2**: Are you sure about that? If you do `$entry = $service->getEntryById($product_code); $value = $entry->typeCategory;` does it output anything? And if you load that entry in the CP, does typeCategory have a value?  Maybe there were already some existing entries and someone set `typeCategory` to required after the fact.  
> **username_0**: Yep it def has a value - and checked it in the Entry in the control panel. When I changed it to not required and processed a Foxycart order it updated the stock and kept the typeCategory value it originally had. That last point may have been the case - that specific entry was added before the field was made required. What's the best way around that - delete the entries and make the field required? Then all should be fine?  
> **username_2**: Yeah... either that or go through each entry that doesn't have a value and manually add one?  
> **username_0**: Cheers - they all had values though. I will delete, (only 2 test products) - thanks for the help - time to sign off for the holidays  
> **username_3**: @username_0, did you ever come back from the holiday and figure this one out? Having the exact same problem now.  
> **username_0**: In my case I originally only had 2 entries - when I deleted those and added again everything worked fine - sorry - no ideal solution.  

---

