Hello! I need to change property isInactive for all contacts of account with status liquidated. On request espo/api/v1/Contact/id, I get a contact and it has property accountsColumns :
But in ORM I can't get this property.
My code:
PHP Code:
<?php
namespace Espo\Custom\Hooks\Account;
use Espo\ORM\Entity;
class LiquidationAccount extends \Espo\Core\Hooks\Base
{
public function afterSave(Entity $entity, array $options = array())
{
$GLOBALS['log']->warning('LiquidationAccount - хук работает');
# если статус организации ликвидирована
if ($entity->get('orgStatus')=='liquidated') {
$entityManager = $this->getEntityManager();
# получаем id организации
$account_id = $entity->get('id');
# получаем все персоны имеющие связь с данной организацией
$contacts_list = $entityManager-> getRepository('Account')->findRelated($entity, 'contacts');
# перебираем все персоны
if (!empty($contacts_list)){
foreach($contacts_list as $contact){
$contact->loadLinkMultipleField('accounts');
if($contact->get('accountsColumns')->$account_id-> isInactive != true){ //works always
$GLOBALS['log']->warning(json_encode($contact->toArray())); //no accountsColumns
$accountsColumns = new \stdClass();
$accountsColumns->$account_id = new \stdClass();
$accountsColumns->$account_id-> isInactive = true;
$paramsContact["accountsColumns"] = $accountsColumns;
$contact->set($paramsContact);
# сохраняем изменения предотвращая запуск рабочих потоков и хуков
$entityManager->saveEntity($contact, ['skipWorkflow' => true, 'skipHooks' => true]);
}
}
}
}
}
}
Comment