Add new record with custom createdById

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • MaxDau
    Member
    • Nov 2022
    • 38

    Add new record with custom createdById

    I would like to create a new entry via a hook / job. When I trigger the hook as user X, the createdById is the ID of user X, although I specified createdById of user Y when creating the entry.

    As an example, when I create a note.

    PHP Code:
    $this->entityManager->createEntity('Note', [
    "createdById" => "6387c5807e6d7db4a", //User Y
    "post" => $post,
    "type" => Note::TYPE_POST,
    "parentId" => $this->getId(),
    "parentType" => $this->getEntityType(),
    "relatedId" => $entity->getId(),
    "relatedType" => $entity->getEntityType()
    ]);
    In my case, the Id is 1 for Admin.
  • rabii
    Active Community Member
    • Jun 2016
    • 1250

    #2
    You can pass Options to the createEntity with specific options check this out https://docs.espocrm.com/development/orm/#store-entity, try this code below instead:

    Code:
    $options = [
    'skipHooks' => true, // skip all hooks; workflows, formula will be ignored
    'silent' => true, // workflows will be ignored, modified fields won't be changed
    'skipCreatedBy' => true, // createdBy won't be set with current user
    'createdById' => true, // override createdBy Probably this is the option you need the most to override createdById
    ];
    
    $this->entityManager->createEntity('Note', [
    "createdById" => "6387c5807e6d7db4a", //User Y
    "post" => $post,
    "type" => Note::TYPE_POST,
    "parentId" => $this->getId(),
    "parentType" => $this->getEntityType(),
    "relatedId" => $entity->getId(),
    "relatedType" => $entity->getEntityType()
    ], $options);​​
    Hope this helps
    Rabii
    Web Dev

    Comment


    • MaxDau
      MaxDau commented
      Editing a comment
      Perfect, thanks
Working...