diff --git a/application/Espo/Core/Formula/Functions/RecordGroup/AttributeType.php b/application/Espo/Core/Formula/Functions/RecordGroup/AttributeType.php new file mode 100644 index 0000000000..11d57f9375 --- /dev/null +++ b/application/Espo/Core/Formula/Functions/RecordGroup/AttributeType.php @@ -0,0 +1,69 @@ +addDependency('entityManager'); + } + + public function process(\StdClass $item) + { + if (!property_exists($item, 'value')) { + throw new Error(); + } + + if (!is_array($item->value)) { + throw new Error(); + } + + if (count($item->value) < 3) { + throw new Error(); + } + + $entityType = $this->evaluate($item->value[0]); + $id = $this->evaluate($item->value[1]); + $attribute = $this->evaluate($item->value[2]); + + if (!$entityType) throw new Error("Formula record\\attribute: Empty entityType."); + if (!$id) return null; + if (!$attribute) throw new Error("Formula record\\attribute: Empty attribute."); + + $entity = $this->getInjection('entityManager')->getEntity($entityType, $id); + + if (!$entity) return null; + + return $this->attributeFetcher->fetch($entity, $attribute); + } +} diff --git a/tests/integration/Espo/Core/Formula/FormulaTest.php b/tests/integration/Espo/Core/Formula/FormulaTest.php index b386b80f70..ef6fbc5220 100644 --- a/tests/integration/Espo/Core/Formula/FormulaTest.php +++ b/tests/integration/Espo/Core/Formula/FormulaTest.php @@ -217,7 +217,7 @@ class FormulaTest extends \tests\integration\Core\BaseTestCase 'name' => 'Test', ]); - $m1 =$em->createEntity('Meeting', [ + $m1 = $em->createEntity('Meeting', [ 'name' => '1', 'status' => 'Held', 'parentType' => 'Account', @@ -281,4 +281,19 @@ class FormulaTest extends \tests\integration\Core\BaseTestCase $result = $fm->run($script, $contact); $this->assertEquals($c2->id, $result); } + + public function testRecordAttribute() + { + $fm = $this->getContainer()->get('formulaManager'); + $em = $this->getContainer()->get('entityManager'); + + $m1 = $em->createEntity('Meeting', [ + 'name' => '1', + 'status' => 'Held', + ]); + + $script = "record\\attribute('Meeting', '".$m1->id."', 'name')"; + $result = $fm->run($script, $contact); + $this->assertEquals('1', $result); + } }