How to handle entry save in plugin when entryId is not set? Referring to my older question, I have the entryId working on an existing entry. However I've noticed a new entry doesn't have an entryId set (makes sense).

I have some custom information in text fields that needs to be saved after onSaveEntry is fired (which I have working). I have a custom model, record, controller and service setup. In my controller I retrieve these custom text fields including the entryId field from Craft. But because a new entry doesn't have an entryId set yet, my controller/service is not working as it should.

When the entry is saved and the page redirects to wherever, I also lose all the information in the text fields. So that's why I want to save my custom information directly after the entry is saved.

My question is, how do I handle this when I don't have the entry id. I don't want the user saving the entry first (to basically generate the entry id), then fill in the custom text fields and then save again (which works). But I think this is very devious.

This is my controller:

public function actionSaveRecord() {
   $this-&gt;requirePostRequest();

   if ($id = craft()-&gt;request-&gt;getPost('recordId')) {
          $model = craft()-&gt;plugin-&gt;getRecordById($id);
     } else {
        $model = craft()-&gt;plugin-&gt;newRecord($id);
   }

   $model-&gt;entryId = craft()-&gt;request-&gt;getPost('entryId');
   $model-&gt;customField = craft()-&gt;request-&gt;getPost('customField ');

   if ($model-&gt;validate()) {
       craft()-&gt;plugin-&gt;saveRecord($model);
   }
}


This is the service:

public function saveRecord(PluginModel &amp;$model)
{
    if ($id = $model-&gt;getAttribute('id')) {
        $pluginRecord = PluginRecord::model()-&gt;findById($id);

        if (!$pluginRecord) {
            throw new Exception(Craft::t('Can\'t find record with ID "{id}"', array('id' =&gt; $id)));
        }
    } else {
        $pluginRecord = new PluginRecord();
    }

    if ($model-&gt;validate()) {
        $attributes = array(
            'entryId' =&gt; $model-&gt;entryId,
            'customField' =&gt; $model-&gt;customField,
        );

        foreach ($attributes as $k =&gt; $v) {
            $pluginRecord -&gt;setAttribute($k, $v);
        }

        if ($pluginRecord -&gt;save()) {
            // update id on model (for new records)
            $model-&gt;setAttribute('id', $pluginRecord-&gt;getAttribute('id'));
            return true;
        } else {
            $model-&gt;addErrors($pluginRecord-&gt;getErrors());
            return false;
        }
    }
}


The rest of the plugin can be found here for reference: https://github.com/iNalgiev/webtexttool1
Had to browse through your plugin repo for a bit to try and wrap my head around what's going on, but I think you have the problem nailed down in that the issue is this: https://github.com/iNalgiev/webtexttool1/blob/1.0.0/WebtexttoolPlugin.php#L57

Trying to grab the entry ID from the cp.entries.edit.right-pane template hook for a new entry is going to return null because IDs aren't assigned until after the element is saved.

Off the top of my head, you've got two not-so-thought-out options.


Refactor your plugin logic so that the code that eventually calls your controller/service's saveRecord methods happen after the entry has been saved in onSaveEntry and you've got a valid entry ID.
Come up with some AJAXy way to calling Craft's saveEntry method from the cp.entries.edit.right-pane template hook that returns the entry ID so the rest of your logic can assume that the entry already exists.