How to create an entry from plugin controller? 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()-&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;";
    }
}


What's wrong in the code? How can I do it?
As Damon mentions, I'd highly recommend checking out the Guest Entries plugin.

Specifically the private _populateEntryModel() method which creates the EntryModel class and gives a good example of the properties that are necessary to set to before calling EntriesService-&gt;saveEntry().