<p>As per your comments, the title field is required and causing a validation error. This can be resolved simply by setting the title.</p>

<pre><code>$entry-&gt;title = 'The Title';
</code></pre>

<p>You can output the validation errors to be notified of any issues with saving the entries as follows.</p>

<pre><code>if ($result === false) {
    \Craft:dd($entry-&gt;getErrors());
}
</code></pre>

CC BY-SA 4.0
2018-11-13T22:16:09.427Z
2018-11-13T22:16:09.427Z
Ben Croker
<p>I try to finish a plugin which imports data into my craftcms project. I already created a console based method/service, which I trigger/run in my shell. Inside my method(s) I receive data <code>(XML or JSON)</code> I parse my data and try to create and fill an entry of a specific channel I already created.</p>

<p>I tried <code>"saveElement()"</code> which doesn't work.</p>

<p>I found some tuts and informations for craftcms v2 (for example: <a href="https://docs.craftcms.com/api/v2/craft-entriesservice.html#public-methods" rel="nofollow noreferrer">https://docs.craftcms.com/api/v2/craft-entriesservice.html#public-methods</a>)</p>

<p>Now i am stuck and i can not find any informations on how to solve this with craftcms v3.</p>

<p>Here is my last version of code after hours of different trys :(</p>

<pre><code>$section = Craft::$app-&gt;sections-&gt;getSectionByHandle('testentry');
$entryTypes = $section-&gt;getEntryTypes();
$entryType = $entryTypes[0];
// Save Entry
//$entry = new EntryModel();
$entry = new \craft\elements\Entry();
$entry-&gt;sectionId = $section-&gt;id;
$entry-&gt;typeId = $entryType-&gt;id;
//$entry-&gt;locale = Craft::$app-&gt;i18n-&gt;getPrimarySiteLocaleId();
//$entry-&gt;authorId = 1;  // TODO:  Set author
$entry-&gt;enabled = true;
$entry-&gt;postDate = $post['post_date'];
$entry-&gt;slug = $post['post_name'];
//      $entry-&gt;getContent()-&gt;title = $post['post_title'];
//      $entry-&gt;setContentFromPost(array(
//          'body' =&gt; $postContent,
//          'categoryCareer' =&gt; NULL,
//      ));

if (Craft::$app-&gt;elements-&gt;saveElement($entry)) {
    $result = true;
} 
else {
    echo 'Could not save the Job entry.'."\n";
    $result = false;
}
</code></pre>

CC BY-SA 4.0
2018-11-12T20:35:40.080Z
This looks mostly correct but there is likely a validation issue with your entry, required fields for example. Does outputting `$entry->getErrors()` reveal anything?
Ben Croker
CC BY-SA 4.0
2018-11-13T17:36:50.600Z
Oh no, i am so damn stupid.
You are right, the last structure should work, but i did not display any errors anymore..... the title is required.

So after i saw the error i was able to import several entries.
Thanks a lot !!
Thomas
CC BY-SA 4.0
2018-11-13T17:44:46.537Z
Now i am able to continue, have to find out how to import and store images with an entry and that's it
Thomas
CC BY-SA 4.0
2018-11-13T22:16:35.553Z
Glad to hear it, I've added this as an answer so that you can mark it as solved.
Ben Croker
<plugin-development><entries><plugins><import>
Craft 3 plugin to import channel entries
# Craft 3 plugin to import channel entries

(Asked by: username_0 on 2018-11-12)

I try to finish a plugin which imports data into my craftcms project. I already created a console based method/service, which I trigger/run in my shell. Inside my method(s) I receive data (XML or JSON) I parse my data and try to create and fill an entry of a specific channel I already created.
I tried "saveElement()" which doesn't work.
I found some tuts and informations for craftcms v2 (for example: https://docs.craftcms.com/api/v2/craft-entriesservice.html#public-methods)
Now i am stuck and i can not find any informations on how to solve this with craftcms v3.
Here is my last version of code after hours of different trys :(
$section = Craft::$app->sections->getSectionByHandle('testentry');
$entryTypes = $section->getEntryTypes();
$entryType = $entryTypes[0];
// Save Entry
//$entry = new EntryModel();
$entry = new \craft\elements\Entry();
$entry->sectionId = $section->id;
$entry->typeId = $entryType->id;
//$entry->locale = Craft::$app->i18n->getPrimarySiteLocaleId();
//$entry->authorId = 1;  // TODO:  Set author
$entry->enabled = true;
$entry->postDate = $post['post_date'];
$entry->slug = $post['post_name'];
//      $entry->getContent()->title = $post['post_title'];
//      $entry->setContentFromPost(array(
//          'body' => $postContent,
//          'categoryCareer' => NULL,
//      ));

if (Craft::$app->elements->saveElement($entry)) {
    $result = true;
} 
else {
    echo 'Could not save the Job entry.'."\n";
    $result = false;
}

**Tags:** plugin-development, entries, plugins, import
> **Comments:**
> **username_1**: This looks mostly correct but there is likely a validation issue with your entry, required fields for example. Does outputting `$entry->getErrors()` reveal anything?  
> **username_0**: Oh no, i am so damn stupid. You are right, the last structure should work, but i did not display any errors anymore..... the title is required.  So after i saw the error i was able to import several entries. Thanks a lot !!  
> **username_0**: Now i am able to continue, have to find out how to import and store images with an entry and that's it  
> **username_1**: Glad to hear it, I've added this as an answer so that you can mark it as solved.  

---

## 1 answers

### Answer by username_1 on 2018-11-13. Score: 0 | Accepted answer

As per your comments, the title field is required and causing a validation error. This can be resolved simply by setting the title.
$entry->title = 'The Title';

You can output the validation errors to be notified of any issues with saving the entries as follows.
if ($result === false) {
    \Craft:dd($entry->getErrors());
}


---

