I just found the problem....
I changed the name of my filemanager element so the part
$entry = new stdClass();
$entry- > attachments = $draftitemid;
It should be:
$entry = new stdClass();
$entry- > myelementname= $draftitemid;
Sneaky Moodle, sometimes doesn't even give an indication where you made a mistake