Disclaimer: I am not a PHP developer, so please bare with me asking this question
Years ago I implemented a custom (afterSave) hook to calculate values for a custom field referencing the value of an auto-increment field (which is not accessable before save).
I now upgraded this espocrm to the latest version but now get an exception accessing the entity id value I used so far to update the database.
If the entity id is now private, what is the recommended way to modify a field value of a new record afterSave?
The function I used so far looks like this:
I know, for the example above I may also
Years ago I implemented a custom (afterSave) hook to calculate values for a custom field referencing the value of an auto-increment field (which is not accessable before save).
I now upgraded this espocrm to the latest version but now get an exception accessing the entity id value I used so far to update the database.
Code:
ERROR: Slim Application Error Type: Error Code: 0 Message: Cannot access protected property Espo\Modules\Crm\Entities\Contact::$id File: /var/www/espocrm/custom/Espo/Custom/Hooks/Contact/ContactNo.php Line: 23 Trace: #0 /var/www/espocrm/application/Espo/Core/Hook/GeneralInvoker.php(186): Espo\Custom\Hooks\Contact\ContactNo->afterSave()
The function I used so far looks like this:
PHP Code:
public function afterSave(Entity $entity, array $options): void
{
if ( $entity->isNew() ) {
$entityId = $entity->id;
$entityNew = $this->entityManager->getEntity('Contact', $entityId);
$entityNo = 'CO_' . str_pad($entityNew->get('contactId'), 5, '0', STR_PAD_LEFT );
$update = $this->entityManager->getQueryBuilder()->update()
->in('Contact')
->set(['contactNo' => $entityNo ])
->where([
'id' => $entityId
])
->build();
$this->entityManager->getQueryExecutor()->execute($update);
}
}
- use a Number field but there is a need to have a (unique) auto-increment field having the same numeric value
- use a calulated field but in https://docs.espocrm.com/user-guide/...x-expressions/ I do not have a PAD function which is expected here

Comment