Job should change status with a specific user

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ChrisSka83
    Senior Member
    • Apr 2023
    • 212

    Job should change status with a specific user

    Hello everyone,

    I have created a job that automatically sets a course date to “Finished” as soon as it is over.
    However, I wanted to achieve this so that a specific user is specified as the person making the change and not “System”.

    This line gets the specific user:
    PHP Code:
    $userId = '64624a9f8d37ec349'; // Benutzer-ID des gewünschten Benutzers einfügen 
    
    And this should specify the user as the changer:
    PHP Code:
    $entityManager->saveEntity($entry, [
                    'setBy' => $userId, // Benutzer wird als Änderer gesetzt
                ]); 
    
    Unfortunately, this has no effect. The only difference is that I do not receive a notification about this and it is not displayed in the stream.
    If I have this addition with the specific user, the status of “System” is changed and I receive a notification and the stream entry.

    I have the following code:

    PHP Code:
    <?php
    namespace Espo\Custom\Jobs;
    use Espo\Core\Utils\Database\DBAL\Connection;
    class UpdateKursEntryStatus extends \Espo\Core\Jobs\Base
    {
        public function run()
        {
            try {
            $entityManager = $this->getEntityManager();
            $repository = $entityManager->getRepository('Kurs');
            // Aktuelles Datum und Zeit in string umwandeln
            $currentTimeString = (new \DateTime())->format('Y-m-d H:i:s');
            // Einträge mit abgelaufener Endzeit finden
            $entries = $repository->where([
                'dateEnd<' => $currentTimeString, // Hier wird der String verwendet
                'status!=' => ['Beendet', 'Storniert', 'Noshow'] // Mehrere Werte ausschließen
            ])->find();
            // Benutzer-ID des Verantwortlichen (ersetze 'userId' mit der tatsächlichen ID)
            $userId = '64624a9f8d37ec349'; // Benutzer-ID des gewünschten Benutzers einfügen
            // Status der Einträge auf "beendet" setzen
            foreach ($entries as $entry) {
                $entry->set('status', 'Beendet');
                $entityManager->saveEntity($entry, [
                    'setBy' => $userId, // Benutzer wird als Änderer gesetzt
                ]);
            }
            return true;
        }  
            catch (\Exception $e) {
            $this->getLogger()->error('Job failed: ' . $e->getMessage());
            return false; // Fehlerhafte Verarbeitung
        }
        }
    }
    

    Where is the error here? I don't see it.
  • Kharg
    Senior Member
    • Jun 2021
    • 435

    #2
    setBy?

    You should use createdById instead.

    Comment


    • ChrisSka83
      ChrisSka83 commented
      Editing a comment
      That's what happens when you rely on ChatGPT ;-)
Working...