<p>You can use <a href="https://craftcms.com/docs/3.x/entries.html#querying-entries" rel="noreferrer">entry queries</a> to fetch the entries and the <a href="https://docs.craftcms.com/api/v3/craft-services-elements.html" rel="noreferrer">elements service</a> to save them. You can access the element service using <code>Craft::$app-&gt;elements</code>. Use <code>Craft::$app-&gt;elements-&gt;saveElement($entry);</code> to save an entry (or any other element).</p>
<p>The best place to put business logic is a service class that you can then call from your controllers. So something like this:</p>
<pre class="lang-php prettyprint-override"><code>use Craft;
use craft\elements\Entry;

public function updateEntriesWithCustomInfo()
{
    /** @var craft\services\Elements $elementService */
    $elementService = Craft::$app-&gt;elements;

    // add conditions here
    $entries = Entry::find()-&gt;limit(1500)-&gt;all();

    foreach ($entries as $entry) {
        // update your field
        $entry-&gt;setFieldValue('my_custom_field', 'some_value');
        $success = $elementService-&gt;saveElement($entry);
        if (!$success) {
            // saving failed, log error or abort
        }
    }
}
</code></pre>
<p>Keep in mind that this might take a while if you have to process 1500 entries at once. If this isn't a one-off, it would be better to put this in a <a href="https://craftcms.com/docs/3.x/extend/queue-jobs.html" rel="noreferrer">custom queue job</a>. Then your service method would just find the entries to update and add queue jobs with their IDs to the queue. The queue job can either process one element at a time or a batch of elements (e.g. in batches of 25), depending on your requirements.</p>

CC BY-SA 4.0
2021-09-14T10:15:35.940Z
thanks @MoritzLost
joomkit
CC BY-SA 4.0
2021-09-14T07:39:23.463Z
2021-09-14T07:39:23.463Z
MoritzLost
<p>I want to update some custom field values on my Entries with data from a third-party API, via a Plugin or Module.</p>
<p>How do I load the relevant Entries, and one-by-one set and save the new data in my Plugin/Module? There could be 1500+ Entries in each operation.</p>
<p>I don't mind at this stage if the logic is in a Controller or Service. Ideally, this work could be triggered from the CP.</p>
<p>I'm familiar with Element/Entry events for saving, etc. but can’t find a starting point outside the normal Element lifecycle.</p>

<plugin-development><modules>
How to loop through specific entries in plugin and edit and save
# How to loop through specific entries in plugin and edit and save

(Asked by: username_0 on 2021-09-13)

I want to update some custom field values on my Entries with data from a third-party API, via a Plugin or Module.

How do I load the relevant Entries, and one-by-one set and save the new data in my Plugin/Module? There could be 1500+ Entries in each operation.

I don't mind at this stage if the logic is in a Controller or Service. Ideally, this work could be triggered from the CP.

I'm familiar with Element/Entry events for saving, etc. but can’t find a starting point outside the normal Element lifecycle.

**Tags:** plugin-development, modules


---

## 1 answers

### Answer by username_1 on 2021-09-14. Score: 5 | Accepted answer

You can use entry queries to fetch the entries and the elements service to save them. You can access the element service using Craft::$app->elements. Use Craft::$app->elements->saveElement($entry); to save an entry (or any other element).
The best place to put business logic is a service class that you can then call from your controllers. So something like this:
use Craft;
use craft\elements\Entry;

public function updateEntriesWithCustomInfo()
{
    /** @var craft\services\Elements $elementService */
    $elementService = Craft::$app->elements;

    // add conditions here
    $entries = Entry::find()->limit(1500)->all();

    foreach ($entries as $entry) {
        // update your field
        $entry->setFieldValue('my_custom_field', 'some_value');
        $success = $elementService->saveElement($entry);
        if (!$success) {
            // saving failed, log error or abort
        }
    }
}

Keep in mind that this might take a while if you have to process 1500 entries at once. If this isn't a one-off, it would be better to put this in a custom queue job. Then your service method would just find the entries to update and add queue jobs with their IDs to the queue. The queue job can either process one element at a time or a batch of elements (e.g. in batches of 25), depending on your requirements.

> **Comments:**
> **username_0**: thanks @username_1  

---

