How do you get fields other than id and name from a linked field ?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • czcpf
    Senior Member
    • Aug 2022
    • 160

    How do you get fields other than id and name from a linked field ?

    Hello,

    What is the easiest way to get additional fields from a link inside a view?

    For example, suppose I have something like this in my entityDef

    "noticeOfViolation": {
    "type": "belongsTo",
    "foreign": "violationResponses",
    "entity": "NoticeOfViolation",
    "audited": false,
    "isCustom": true
    },​

    some javascript code like this gives me the id and name of this linked field but how do I access other columns/fields of the linked entity inside a view file?

    We can access these by default:

    this.model.get('noticeOfViolationId')
    this.model.get('noticeOfViolationName')

    But what if I want to get the actual record noticeOfViolation and all of its fields ?

    How do I go about doing that?




  • telecastg
    Active Community Member
    • Jun 2018
    • 907

    #2
    The easiest way, is to include the fields that you want from the foreign entity in the foreign entity's small list layout and then they will be listed in the relationships panel of the detail view.

    Comment


    • czcpf
      czcpf commented
      Editing a comment
      telecastg Thanks for your suggestion. I was trying to find out what function (ie. controller, service) is called when creating new entities and just set the values I wanted to put there. I've been using readLoaderClassNameList and listLoaderClassNameList in entity recordDefs lately to handle setting link type additional fields and custom fields. It works wonderfully except those loaders are not called when creating `new` entities. Often times in the UI, I'm finding that I want to create related entities off bottom relationship panels and by default, the direct parent link gets set on the modal windows if it is a field in the small detail view of the child (even before saving). So often however, I want to show other parent link fields in the 'create new related' modal window. I don't think your solution solves this problem. It does work if you already have the child entity saved and you view the child entity though.

    • telecastg
      telecastg commented
      Editing a comment
      The problem is that the backend classes will not be invoked until after you have saved the new model.

      Perhaps this posting that describes how the functionality of the "Create" new button works can help:

      The idea of this thread is to create a "road map/guide" to help developers find where GUI sections or elements are defined within Espo code so customization projects can be implemented without having to spend a lot of time "finding your way around" the code (like many of us have done) whenever possible.
  • czcpf
    Senior Member
    • Aug 2022
    • 160

    #3
    telecastg thanks for the help. It got me on the right path. I'm posting what I finally figured out for anyone else that may need to do something like this.

    Scenario: You have a relationship panel displayed on some detail view and you want to set fields in the modal window that opens when you create new children from the parent detail screen (bottom panel + button). linkName is the name(s) of the child entity links of your parent. This works great!

    Step 1: Create a custom detail view
    custom/src/views/{your-custom-entity-name}/detail.js

    Code:
    define('custom:views/{your-custom-entity-name}/detail', ['views/detail'], function (Dep) {
    
    return Dep.extend({
    
    /**
    * When a related record created, use a function to obtain some attributes for a created entity.
    *
    * Example:
    * ```
    * {
    * 'linkName': function () {
    * return {
    * 'someAttribute': this.model.get('attribute1') + ' ' +
    * this.model.get('attribute2')
    * };
    * },
    * }
    * ```
    *
    * @type {Object}
    */
    relatedAttributeFunctions: {},​
    
    /**
    * When a related record created, attributes will be copied from a current entity.
    *
    * Example:
    * ```
    * {
    * 'linkName': {
    * 'attributeNameOfCurrentEntity': 'attributeNameOfCreatedRelatedEntity',
    * }
    * }
    * ```
    *
    * @type {Object}
    */
    relatedAttributeMap: {},​
    
    setup: function() {
    Dep.prototype.setup.call(this);
    console.log("custom:views/{your-custom-entity-name} detail this.model = ", this.model);
    },
    
    });
    });​

    You can use relatedAttributeFunctions or relatedAttributeMap depending on your needs. See the documentation in /site/client/src/views/detail.js

    Step 2: tell espocrm to use your custom detail view for your custom entity
    Custom/Resources/metadata/clientDefs/{your-custom-entity-name}.json

    Code:
    {
    ...
    "views": {
    "detail": "custom:views/notice-of-violation/detail"
    },
    ...
    }​
    One other detail, the above solution works for setting the value of the child attributes from the parent when creating new child entities. If your child attribute is “notStorable” and not a foreign field, you also need to set it using loaders for it to be shown when “viewing”/editing/listing the child entity (after creation). If it is a foreign field or a storable field, that is not necessary.

    loaders are set in the child entity recordDefs.json



    search the code base for these strings to see examples

    readLoaderClassNameList

    listLoaderClassNameList

    The classes you set for them are quite useful for setting calculated, notStorable custom fields with two limitations : 1 they aren’t called on the “create new” windows and pdfTemplates. For accessing custom notStorable non foreign fields in PDF templates you need to use pdfDefs.json

    dataLoaderClassNameList

    https://docs.espocrm.com/development/metadata/pdf-defs/

    For create new modals/windows use the solution I posted above.​

    Last edited by czcpf; 03-04-2023, 07:20 PM.

    Comment


    • telecastg
      telecastg commented
      Editing a comment
      Thanks for posting the solution.

      Participants like yourself make this forum very useful by actually posting solutions !

      Best Regards
Working...