<p>In a <a href="http://buildwithcraft.com/docs/plugins/introduction" rel="nofollow">custom plugin</a> you can hook into the <a href="http://buildwithcraft.com/docs/plugins/events-reference#entries-onSaveEntry" rel="nofollow">saveEntry</a> event:</p>

<pre><code>craft()-&gt;on('entries.saveEntry', function(Event $event)]
{

    $entry = $event-&gt;params['entry'];

    // GET ENTRY ID
    $entryId = $entry-&gt;id 

    // DO YOUR STUFF
    ...

}
</code></pre>

CC BY-SA 3.0
2015-06-09T18:57:40.413Z
Is there a way to limit the conditions where your event hook kicks in? I only want this to fire with a certain sectionId, if it's new, if it was posted by a certain form.
David Rampersad
CC BY-SA 3.0
2015-06-09T20:46:17.893Z
Not really, but there's no overhead using the craft->on() event binder method, and you can easily test for all of the things you mention inside the event handler.
Mats Mikkel Rummelhoff
CC BY-SA 3.0
2015-06-08T20:37:43.653Z
2015-06-09T09:03:53.030Z
2015-06-09T09:03:53.030Z
Clive Portman
<p>If you wanted to do this without having to write your own plugin, you can achieve this with a simple custom route and an extra parameter in your template.</p>

<p>Your hidden fields could look like:</p>

<pre><code>&lt;input type="hidden" name="action" value="entries/saveEntry"&gt;
&lt;input type="hidden" name="redirect" value="schedule/{id}"&gt;
</code></pre>

<p>And then in <code>config/routes.php</code> put</p>

<pre><code>'schedule/(?P&lt;id&gt;\d+)' =&gt; 'scheduleTemplate',
</code></pre>

<p>When craft saves the entry, it will redirect you to <code>schedule</code> with the ID of the entry that was just saved as the last segment. The custom route will pick up any digit on the end of the url and assign it to a variable called <code>id</code> which you can then use in your templates.</p>

<p><strong>Some behind the scenes clearance, based on your comment</strong>  </p>

<p>No they aren't twig variables as the replacement is doing through php (although apparently you can use Twig tags).</p>

<p>When you save an entry the new entry object gets passed in <code>redirectToPostedUrl</code> which, if there is an object passed will call the function <code>renderObjectTemplate</code> in the templates service, this will swap out any <code>{single}</code> or <code>{{twig}}</code> tags with the appropriate attribute and you will get the finished URL returned.</p>

<p>Here are two links to the class reference so you can see what I mean.</p>

<p><a href="http://buildwithcraft.com/classreference/controllers/BaseController#redirectToPostedUrl-detail" rel="nofollow">The <code>redirectToPostedUrl</code> function</a></p>

<p><a href="http://buildwithcraft.com/classreference/services/TemplatesService#renderObjectTemplate-detail" rel="nofollow">The <code>renderObjectTemplate</code> function</a></p>

CC BY-SA 3.0
2015-06-11T19:00:13.533Z
Thanks! Didn't know you could pass along entry parameters with single curly brackets. Doesn't look like they're twig variables, so is this getting handled by Craft's saveEntry action?
David Rampersad
CC BY-SA 3.0
2015-06-12T16:21:33.150Z
@DavidRampersad I've edited my answer with a couple of details about how it works, would have been too long for a comment otherwise and could have been hard to read!
Alec Ritson
CC BY-SA 3.0
2015-06-10T08:05:17.703Z
2015-06-12T16:20:27.687Z
2015-06-12T16:20:27.687Z
Alec Ritson
<p>Is there a way to save a new entry with a template form, then pass the ID of that new entry along to the redirect location? </p>

<p>The use case is, I'm interacting with a scheduling service and want to pass along the new craft entry ID in a query string to their API. The service has a webhook that fires on successful scheduling (and provides an appointment ID). </p>

<p>Currently, I'm getting the user's email address returned from the API, then <em>assuming</em> that user's most recent section/entry in craft is the one I'm looking for. After that, I can unambiguously handle reschedules and cancellations by referencing the appointment ID.</p>

<p>The form has the following inputs</p>

<pre><code>&lt;input type="hidden" name="action" value="entries/saveEntry"&gt;
&lt;input type="hidden" name="redirect" value="/schedule"&gt;
</code></pre>

<p>and I'd like the /schedule page to get a entryId passed into it</p>

<form><entry><redirect>
Get the ID of a new entry when it's saved
# Get the ID of a new entry when it's saved

(Asked by: username_0 on 2015-06-08)

Is there a way to save a new entry with a template form, then pass the ID of that new entry along to the redirect location? 
The use case is, I'm interacting with a scheduling service and want to pass along the new craft entry ID in a query string to their API. The service has a webhook that fires on successful scheduling (and provides an appointment ID). 
Currently, I'm getting the user's email address returned from the API, then assuming that user's most recent section/entry in craft is the one I'm looking for. After that, I can unambiguously handle reschedules and cancellations by referencing the appointment ID.
The form has the following inputs



and I'd like the /schedule page to get a entryId passed into it

**Tags:** form, entry, redirect

---

## 2 answers

### Answer by username_3 on 2015-06-10. Score: 5 | Accepted answer

If you wanted to do this without having to write your own plugin, you can achieve this with a simple custom route and an extra parameter in your template.
Your hidden fields could look like:



And then in config/routes.php put
'schedule/(?P\d+)' => 'scheduleTemplate',

When craft saves the entry, it will redirect you to schedule with the ID of the entry that was just saved as the last segment. The custom route will pick up any digit on the end of the url and assign it to a variable called id which you can then use in your templates.
Some behind the scenes clearance, based on your comment 
No they aren't twig variables as the replacement is doing through php (although apparently you can use Twig tags).
When you save an entry the new entry object gets passed in redirectToPostedUrl which, if there is an object passed will call the function renderObjectTemplate in the templates service, this will swap out any {single} or {{twig}} tags with the appropriate attribute and you will get the finished URL returned.
Here are two links to the class reference so you can see what I mean.
The redirectToPostedUrl function
The renderObjectTemplate function

> **Comments:**
> **username_0**: Thanks! Didn't know you could pass along entry parameters with single curly brackets. Doesn't look like they're twig variables, so is this getting handled by Craft's saveEntry action?  
> **username_3**: @DavidRampersad I've edited my answer with a couple of details about how it works, would have been too long for a comment otherwise and could have been hard to read!  

---

### Answer by username_1 on 2015-06-08. Score: 3

In a custom plugin you can hook into the saveEntry event:
craft()->on('entries.saveEntry', function(Event $event)]
{

    $entry = $event->params['entry'];

    // GET ENTRY ID
    $entryId = $entry->id 

    // DO YOUR STUFF
    ...

}

> **Comments:**
> **username_0**: Is there a way to limit the conditions where your event hook kicks in? I only want this to fire with a certain sectionId, if it's new, if it was posted by a certain form.  
> **username_2**: Not really, but there's no overhead using the craft->on() event binder method, and you can easily test for all of the things you mention inside the event handler.  

---

