Announcement

Collapse
No announcement yet.

Calculated Fields: can I use a Class to return value of a field

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Calculated Fields: can I use a Class to return value of a field

    I am working from this page about calculated fields in the documentation.

    I have defined a custom field in a custom entity as follows:

    PHP Code:
    {
        
    "fields": {
            
    "myCustomField": {
                
    "type""text",
                
    "required"false,
                
    "notStorable"true,
                
    "utility"true,
                
    "readOnly"true,
                
    "customizationDisabled"true
            
    }
        }

    In the documentation, it provides guidance about adding a "select" property that uses a Complex Expression to return the value. Complex Expressions convert into SQL select queries.

    I need to return a value that is extracted from another related (not linked) entity. The value to return for this calculated field is generated from the value of a DB field. So a SELECT with JOIN will not provide the required value.

    My Question:

    Is there a way to declare/use a Custom Class instead of a Complex Expression that returns the value for a field? I need access to the full Entity record to reference the related Entity record.

  • #2
    You can use additional read/list loaders instead. https://docs.espocrm.com/development...rclassnamelist

    Classes will be called to load additional fields before read and edit operations.

    Comment


    • #3
      Example: https://github.com/espocrm/espocrm/b...eeting.json#L5

      Comment


      • #4
        Thanks yuri

        This seems to be the perfect solution. I have implemented but it is not working. How can I troubleshoot.

        Here is my code:

        in `custom/Espo/Modules/MyModule/Resources/metadata/recordDefs/MyEntity.json`

        Code:
        {
            "readLoaderClassNameList": [
                "Espo\\Modules\\MyModules\\Classes\\MyEntityLoader"
            ]
        }
        in testing, this def shows up when I access $metadata->get(['recordDefs', 'MyEntity']);

        in `custom/Espo/Modules/MyModule/Classes/MyEntityLoader.php`

        PHP Code:
        namespace Espo\Modules\MyModule\Classes;

        use 
        Espo\ORM\Entity;
        use 
        Espo\Core\FieldProcessing\Loader;
        use 
        Espo\Core\FieldProcessing\Loader\Params;

        class 
        MyEntityLoader implements Loader
        {
            public function 
        process(Entity $entityParams $params): void
            
        {
                
        $entity->set('myCustomField''someValue');
            }

        In testing, I put a logger into the `process` method of Loader class to check whether class is loading and it is not.

        To test whether it is working, I am getting the entity -> $rec = $entityManager->getEntityById('MyEntity', '<id>') and trying both $rec->get('MyCustomField') and $rec->getValueMap(). First returns null, and value map array does not contain it.

        Comment


        • #5
          It will work only when you read/edit entity via API (called from the Record service). It won't work when you just fetch entity from the entity manager in some place.

          Note that you can just call this logic manually where you fetch an entity.

          Comment


          • #6
            Ah yes, I recall this with the hooks in recordDefs as well.

            The purpose of adding this custom field in this manner was to make it available as part of the "parent" entity when generating an email from an Email Template.

            The insert fields on the Compose Email modal uses the API as follows: /api/v1/Email/insertFieldData?parentId=<entity_id>&parentType=<M yEntity>&to=

            Does this API call use the record service to obtain the field/value map of MyEntity?

            Will the recordDefs approach discussed above make available the custom field?

            If not, how would suggest that I make available this custom field value for email template usage purposes?

            Comment


            • #7
              > Does this API call use the record service to obtain the field/value map of MyEntity?

              No. But I just added it for the next 8.3 version: https://github.com/espocrm/espocrm/c...77065969ad75c9

              Feel free to patch it manually.

              Comment

              Working...
              X