<p>You're almost there, as you can see on the <strong><em>SproutForms_EntriesService</em></strong> Class at line 197 we add a <strong><em>SproutForms_OnSaveEntryEvent</em></strong>. When this event is fired you can get the form entry submitted within the param 'entry'. All you need to do is add a listener to catch this event, please go ahead and add the following code to the init() function of the plugin of your custom field type:</p>

<pre><code>craft()-&gt;on('sproutForms.onSaveEntry', function (Event $event)
    {
        $entry = $event-&gt;params['entry'];

        // Let's assume that your field handle is "custom"
        $entry-&gt;setContentFromPost(array(
            'custom' =&gt; $entry-&gt;id,
            )
        );
        // save it again. (avoid an infinite loop using sproutForms()-&gt;entries-&gt;saveEntry)
        craft()-&gt;elements-&gt;saveElement($entry);
    });
</code></pre>

CC BY-SA 3.0
2017-01-31T22:15:57.733Z
2017-01-31T22:39:55.157Z
2017-01-31T22:39:55.157Z
Andre L.
<p>What is the right way to save the entry id of a sprout form submission to a custom field in that form? </p>

<p>I've been fiddling with the SproutForms_EntriesService, but is that the completely wrong way to go about it?</p>

<p><a href="https://craftcms.stackexchange.com/questions/17952/write-into-sprout-forms-from-another-plugin">This post was helpful</a>, but the setContentFromPost threw errors for me. I've had success with getContent though:</p>

<pre><code>$entry-&gt;getContent()-&gt;title = yourValue;
</code></pre>

<p>Still, I'm missing something:</p>

<p>Specifically, I've played with the saveEntry function, and I can save values before the event is validated (where the title is updated at line 156), but since I need the element ID, the value should be set after the entry has been successfully saved. But, if I move my code inside the successful new entry block at line 176, it's a dud: </p>

<p><strong>Starting @176</strong></p>

<pre><code>// Now that we have an element ID, save it on the other stuff
if ($isNewEntry)
{
$entryRecord-&gt;id = $entry-&gt;id;
  // my crappy code:
  $entryId = $entry-&gt;id;
  $entry-&gt;getContent()-&gt;formEntryId = $entryId;
  // end crappy code
}
</code></pre>

<p>I could be going about this totally wrong. You'll have to forgive the ignorance; I'm a Front End Dev with general Craft skills who's delving into plugin dev for the first time. So, I'm at the beginning, studying the Craft Plugin Mijingo Course and just straight up reading through plugin files…</p>

<p>Thanks for your help!</p>

<p>L</p>

<plugins><plugin-sproutforms>
Sprout Forms: Map Element ID to Content Field on Save Entry Event
# Sprout Forms: Map Element ID to Content Field on Save Entry Event

(Asked by: username_0 on 2016-12-27)

What is the right way to save the entry id of a sprout form submission to a custom field in that form? 
I've been fiddling with the SproutForms_EntriesService, but is that the completely wrong way to go about it?
This post was helpful, but the setContentFromPost threw errors for me. I've had success with getContent though:
$entry->getContent()->title = yourValue;

Still, I'm missing something:
Specifically, I've played with the saveEntry function, and I can save values before the event is validated (where the title is updated at line 156), but since I need the element ID, the value should be set after the entry has been successfully saved. But, if I move my code inside the successful new entry block at line 176, it's a dud: 
Starting @176
// Now that we have an element ID, save it on the other stuff
if ($isNewEntry)
{
$entryRecord->id = $entry->id;
  // my crappy code:
  $entryId = $entry->id;
  $entry->getContent()->formEntryId = $entryId;
  // end crappy code
}

I could be going about this totally wrong. You'll have to forgive the ignorance; I'm a Front End Dev with general Craft skills who's delving into plugin dev for the first time. So, I'm at the beginning, studying the Craft Plugin Mijingo Course and just straight up reading through plugin files…
Thanks for your help!
L

**Tags:** plugins, plugin-sproutforms

---

## 1 answers

### Answer by username_1 on 2017-01-31. Score: 1

You're almost there, as you can see on the SproutForms_EntriesService Class at line 197 we add a SproutForms_OnSaveEntryEvent. When this event is fired you can get the form entry submitted within the param 'entry'. All you need to do is add a listener to catch this event, please go ahead and add the following code to the init() function of the plugin of your custom field type:
craft()->on('sproutForms.onSaveEntry', function (Event $event)
    {
        $entry = $event->params['entry'];

        // Let's assume that your field handle is "custom"
        $entry->setContentFromPost(array(
            'custom' => $entry->id,
            )
        );
        // save it again. (avoid an infinite loop using sproutForms()->entries->saveEntry)
        craft()->elements->saveElement($entry);
    });


---

