<p>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: <a href="https://github.com/iNalgiev/webtexttool1/blob/1.0.0/WebtexttoolPlugin.php#L57" rel="nofollow noreferrer">https://github.com/iNalgiev/webtexttool1/blob/1.0.0/WebtexttoolPlugin.php#L57</a></p>

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

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

<ol>
<li><p>Refactor your plugin logic so that the code that eventually calls your controller/service's <code>saveRecord</code> methods happen after the entry has been saved in <a href="https://craftcms.com/docs/plugins/events-reference#entries-onSaveEntry" rel="nofollow noreferrer">onSaveEntry</a> and you've got a valid entry ID.</p></li>
<li><p>Come up with some AJAXy way to calling Craft's <code>saveEntry</code> method from the <code>cp.entries.edit.right-pane</code> template hook that returns the entry ID so the rest of your logic can assume that the entry already exists.</p></li>
</ol>

CC BY-SA 3.0
2016-11-02T00:00:19.413Z
2016-11-02T00:00:19.413Z
Brad Bell
<p>Referring to my older <a href="https://craftcms.stackexchange.com/q/17251/5730">question</a>, I have the entryId working on an <strong>existing</strong> entry. However I've noticed a new entry doesn't have an entryId set (makes sense).</p>

<p>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.</p>

<p>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.</p>

<p>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.</p>

<p>This is my controller:</p>

<pre><code>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);
   }
}
</code></pre>

<p>This is the service:</p>

<pre><code>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;
        }
    }
}
</code></pre>

<p>The rest of the plugin can be found here for reference: <a href="https://github.com/iNalgiev/webtexttool1" rel="nofollow noreferrer">https://github.com/iNalgiev/webtexttool1</a></p>

<controller><services><saveentry>
How to handle entry save in plugin when entryId is not set?
# How to handle entry save in plugin when entryId is not set?

(Asked by: username_0 on 2016-10-23)

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->requirePostRequest();

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

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

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

This is the service:
public function saveRecord(PluginModel &$model)
{
    if ($id = $model->getAttribute('id')) {
        $pluginRecord = PluginRecord::model()->findById($id);

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

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

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

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

The rest of the plugin can be found here for reference: https://github.com/username_0/webtexttool1

**Tags:** controller, services, saveentry


---

## 1 answers

### Answer by username_1 on 2016-11-02. Score: 1

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/username_0/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.

1. 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](https://craftcms.com/docs/plugins/events-reference#entries-onSaveEntry) and you've got a valid entry ID.
2. 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.


---

