How to modify and save a field in entry on EVENT_AFTER_SAVE in a plugin? 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-&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?
                }
        });


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!
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-&gt;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-&gt;sender;
        $isNew = $event-&gt;isNew;

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