Answers.Body: <p>As <a href="https://craftcms.stackexchange.com/questions/20995/how-to-create-an-entry-from-plugin-controller#comment28156_20995">Damon mentions</a>, I'd highly recommend checking out the Guest Entries plugin.</p>
  
  <p>Specifically the private <a href="https://craftcms.stackexchange.com/questions/20995/how-to-create-an-entry-from-plugin-controller#comment28156_20995"><code>_populateEntryModel()</code></a> method which creates the <code>EntryModel</code> class and gives a good example of the properties that are necessary to set to before calling <code>EntriesService-&gt;saveEntry()</code>.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-06-29T05:03:33.117Z
Answers.Text: Thanks that helped me !! I have updated the question with the correct working code.
Answers.UserDisplayName: hashtagerrors
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-06-28T22:08:06.163Z
Answers.LastActivityDate: 2017-06-28T22:08:06.163Z
Answers.UserDisplayName: Brad Bell
Body: <p>Let's say I have a section 'foo', with following fields and slug:</p>
  
  <p>First Name (name)</p>
  
  <p>Last Name (lname)</p>
  
  <p>Plan (plan)</p>
  
  <p>Cost (cost)</p>
  
  <p>The field cost is dynamically created by a function already present in controller. I tried the following:</p>
  
  <pre><code>public function actionGetCost()
  {
      $fname = craft()-&gt;request-&gt;getParam('name');
      $lname = craft()-&gt;request-&gt;getParam('lname');
      $plan = craft()-&gt;request-&gt;getParam('plan');
  
      ...
  
      $entry = new EntryModel();
      $entry-&gt;sectionId = 16;
      date_default_timezone_set('America/New_York');
      $date = date('n-j-Y') ;
      $entry-&gt;postDate = $date;
      $entry-&gt;authorId = 1;
      $entry-&gt;enabled = true;
      $entry-&gt;setContentFromPost([
         'name' =&gt; $fname,
         'lname' =&gt; $lname,
         'plan' =&gt; $plan,       
         'cost' =&gt; $cost-&gt;id,
      ]);
  
      $save = craft()-&gt;entries-&gt;saveEntry($entry);
  
      if ($save){
        echo "saved" !
      }else{
        echo "&lt;pre&gt;";
        print_r($entry()-&gt;getAllErrors());
        echo "&lt;/pre&gt;";
      }
  }
  </code></pre>
  
  <p>What's wrong in the code? How can I do it?</p>
  
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2017-06-28T15:24:32.310Z
Comments.Text: Have you checked out the [Guest Entries](https://github.com/pixelandtonic/GuestEntries) plugin? If you go into the controllers and service files that could give you some insight.
Comments.UserDisplayName: Damon
Tags: <plugin-development>
Title: How to create an entry from plugin controller?
ThreadText: # How to create an entry from plugin controller?
  
  (Asked by: username_0 on 2017-06-28)
  
  Let's say I have a section 'foo', with following fields and slug:
  
  First Name (name)
  
  Last Name (lname)
  
  Plan (plan)
  
  Cost (cost)
  
  The field cost is dynamically created by a function already present in controller. I tried the following:
  
  ```
  public function actionGetCost()
  {
      $fname = craft()->request->getParam('name');
      $lname = craft()->request->getParam('lname');
      $plan = craft()->request->getParam('plan');
  
      ...
  
      $entry = new EntryModel();
      $entry->sectionId = 16;
      date_default_timezone_set('America/New_York');
      $date = date('n-j-Y') ;
      $entry->postDate = $date;
      $entry->authorId = 1;
      $entry->enabled = true;
      $entry->setContentFromPost([
         'name' => $fname,
         'lname' => $lname,
         'plan' => $plan,       
         'cost' => $cost->id,
      ]);
  
      $save = craft()->entries->saveEntry($entry);
  
      if ($save){
        echo "saved" !
      }else{
        echo "<pre>";
        print_r($entry()->getAllErrors());
        echo "</pre>";
      }
  }
  
  ```
  
  What's wrong in the code? How can I do it?
  
  **Tags:** plugin-development
  
  > **Comments:**
  > **username_1**: Have you checked out the [Guest Entries](https://github.com/pixelandtonic/GuestEntries) plugin? If you go into the controllers and service files that could give you some insight.  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_2 on 2017-06-28. Score: 1 | Accepted answer
  
  As [username_1 mentions](https://craftcms.stackexchange.com/questions/20995/how-to-create-an-entry-from-plugin-controller#comment28156_20995), I'd highly recommend checking out the Guest Entries plugin.
  
  Specifically the private [`_populateEntryModel()`](https://craftcms.stackexchange.com/questions/20995/how-to-create-an-entry-from-plugin-controller#comment28156_20995) method which creates the `EntryModel` class and gives a good example of the properties that are necessary to set to before calling `EntriesService->saveEntry()`.
  
  > **Comments:**
  > **username_0**: Thanks that helped me !! I have updated the question with the correct working code.  
  
  ---
  
  