<p>The accepted answer works but it's for elements in general, of which an entry is just one. Instead, you can skip checking if the element is an entry (<code>if ($event-&gt;element instanceof Entry)</code>) and just use the <code>Entry</code> class itself (it extends <code>Element</code>) like so:</p>

<pre><code>use craft\elements\Entry;
use craft\events\ModelEvent;

Event::on(
    Entry::class,
    Entry::EVENT_AFTER_SAVE,
    function(ModelEvent $event) {
        $entry = $event-&gt;sender;
        $isNew = $event-&gt;isNew;

        if ($isNew) {
            // do your thing
        }
    }
);
</code></pre>

CC BY-SA 4.0
2019-12-05T18:31:37.400Z
I wish this was the accepted answer - more efficient, thanks.
iamkeir
CC BY-SA 4.0
2019-12-06T17:13:22.427Z
I wish it was in Craft's docs! :p Took me forever to figure out the appropriate events to use for simple tasks.
Ryan
CC BY-SA 4.0
2019-12-08T17:56:40.147Z
Yeah, the events are hard to track down.
iamkeir
CC BY-SA 4.0
2022-03-02T11:41:19.820Z
also Working for me, Thank you
Ra3d K
CC BY-SA 4.0
2019-06-24T20:03:35.567Z
2019-06-24T20:33:05.247Z
2019-06-24T20:33:05.247Z
Ryan
<p>Following @lenka suggestion, I was able to make the script work this way:</p>

<pre><code>use craft\services\Elements;
use craft\events\ElementEvent;
use craft\elements\Entry;

Event::on(
    Elements::class, 
    Elements::EVENT_AFTER_SAVE_ELEMENT, 
    function(ElementEvent $event) {
        if ($event-&gt;element instanceof Entry) {
            $entry = $event-&gt;element;
            if($event-&gt;isNew){
                $entry-&gt;reference= 'AV' . (string)$entry-&gt;id;
                Craft::$app-&gt;elements-&gt;saveElement($entry);    
            }
        }
});
</code></pre>

CC BY-SA 4.0
2022-03-02T11:40:59.533Z
Working for me, Thanks
Ra3d K
CC BY-SA 4.0
2019-01-06T14:46:57.120Z
2019-06-24T19:42:57.437Z
2019-06-24T19:42:57.437Z
Masiorama
<p>I'm trying to build my first little plugin.
I'm stuck with a problem: I need to modify the value of the specific field <code>reference</code>, right after the saving of the entry because the new value is supposed to be based on the same entry <code>id</code>.</p>

<p>This works to get the Entry object and modify the reference:</p>

<pre><code>Event::on(
            Element::class, 
            Element::EVENT_AFTER_SAVE, 
            function(Event $event) {
                if ($event-&gt;sender instanceof Entry) {
                    $idLast = Entry::find('id')
                        -&gt;section('annunci')
                        -&gt;orderBy('elements.dateUpdated desc')
                        -&gt;one();
                    $newRef = 'AV' . $idLast['id'];
                    // assign new reference ? At this point the record is already saved!!!
                    $event-&gt;sender-&gt;setFieldValue('reference', $newRef);
                    // then what?
                }
        });
</code></pre>

<p>I can  manage to retrieve the last saved entry, grab its id, and then create my <code>$newRef</code>, but after that, how am I supposed to resave the entry and update the entry record with the new value?</p>

<p>Second question: I would like to intercept the event only on the first save (aka on creation), and avoiding recalculating the field value on every successive saving. Is this possible? </p>

<p>Thanks in advance for any help, much appreciated!</p>

CC BY-SA 4.0
2019-01-06T11:49:43.047Z
Look at https://craftcms.stackexchange.com/questions/27951/how-can-i-use-the-entry-uid-in-the-entry-uri/27960#27960 It can help.
lenka
CC BY-SA 4.0
2019-01-06T14:45:05.060Z
@lenka thank you very much, it did help indeed!
Masiorama
<plugin-development><craft3><entry><events>
How to modify and save a field in entry on EVENT_AFTER_SAVE in a plugin?
# How to modify and save a field in entry on EVENT_AFTER_SAVE in a plugin?

(Asked by: username_0 on 2019-01-05)

I'm trying to build my first little plugin.
I'm stuck with a problem: I need to modify the value of the specific field reference, right after the saving of the entry because the new value is supposed to be based on the same entry id.
This works to get the Entry object and modify the reference:
Event::on(
            Element::class, 
            Element::EVENT_AFTER_SAVE, 
            function(Event $event) {
                if ($event->sender instanceof Entry) {
                    $idLast = Entry::find('id')
                        ->section('annunci')
                        ->orderBy('elements.dateUpdated desc')
                        ->one();
                    $newRef = 'AV' . $idLast['id'];
                    // assign new reference ? At this point the record is already saved!!!
                    $event->sender->setFieldValue('reference', $newRef);
                    // then what?
                }
        });

I can  manage to retrieve the last saved entry, grab its id, and then create my $newRef, but after that, how am I supposed to resave the entry and update the entry record with the new value?
Second question: I would like to intercept the event only on the first save (aka on creation), and avoiding recalculating the field value on every successive saving. Is this possible? 
Thanks in advance for any help, much appreciated!

**Tags:** plugin-development, craft3, entry, events

> **Comments:**
> **username_1**: Look at https://craftcms.stackexchange.com/questions/27951/how-can-i-use-the-entry-uid-in-the-entry-uri/27960#27960 It can help.  
> **username_0**: @username_1 thank you very much, it did help indeed!  

---

## 2 answers

### Answer by username_0 on 2019-01-06. Score: 4 | Accepted answer

Following @username_1 suggestion, I was able to make the script work this way:
use craft\services\Elements;
use craft\events\ElementEvent;
use craft\elements\Entry;

Event::on(
    Elements::class, 
    Elements::EVENT_AFTER_SAVE_ELEMENT, 
    function(ElementEvent $event) {
        if ($event->element instanceof Entry) {
            $entry = $event->element;
            if($event->isNew){
                $entry->reference= 'AV' . (string)$entry->id;
                Craft::$app->elements->saveElement($entry);    
            }
        }
});

> **Comments:**
> **username_4**: Working for me, Thanks  

---

### Answer by username_2 on 2019-06-24. Score: 6

The accepted answer works but it's for elements in general, of which an entry is just one. Instead, you can skip checking if the element is an entry (if ($event->element instanceof Entry)) and just use the Entry class itself (it extends Element) like so:
use craft\elements\Entry;
use craft\events\ModelEvent;

Event::on(
    Entry::class,
    Entry::EVENT_AFTER_SAVE,
    function(ModelEvent $event) {
        $entry = $event->sender;
        $isNew = $event->isNew;

        if ($isNew) {
            // do your thing
        }
    }
);

> **Comments:**
> **username_3**: I wish this was the accepted answer - more efficient, thanks.  
> **username_2**: I wish it was in Craft's docs! :p Took me forever to figure out the appropriate events to use for simple tasks.  
> **username_3**: Yeah, the events are hard to track down.  
> **username_4**: also Working for me, Thank you  

---

