How to display foreign field values while changing the related field link?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • newbie
    Junior Member
    • Feb 2023
    • 13

    How to display foreign field values while changing the related field link?

    Hi, everyone.

    I am looking for a way to display foreign field values while changing the related field link.
    I have create new foreign field in entity, but the value never show while create / edit.
    Is there any solution for this need?

    Thank you

  • Vadym
    Super Moderator
    • Jun 2021
    • 345

    #2
    Hi newbie,

    Perhaps the Age field of the selected Account record is empty.
    Please provide a screenshot of the "John Doe Cahyono" record in detail view and a screenshot of Age field configurations to investigate this issue further.

    Best regards​

    Comment

    • esforim
      Active Community Member
      • Jan 2020
      • 2204

      #3
      I believe you have to use Websocket if you want instant update. Luckily there is a guide on this:

      Auf YouTube findest du die angesagtesten Videos und Tracks. Außerdem kannst du eigene Inhalte hochladen und mit Freunden oder gleich der ganzen Welt teilen.


      Otherwise I believe you calculating the Age using Formula? And your formula don't get re-update unless you Workflow or update the records. So that another issue you have to solve as well.

      Comment

      • newbie
        Junior Member
        • Feb 2023
        • 13

        #4
        Originally posted by Vadym
        Hi newbie,

        Perhaps the Age field of the selected Account record is empty.
        Please provide a screenshot of the "John Doe Cahyono" record in detail view and a screenshot of Age field configurations to investigate this issue further.

        Best regards​
        Hi Vadym, below are the screenshots for further investigation

        Comment

        • newbie
          Junior Member
          • Feb 2023
          • 13

          #5
          Originally posted by espcrm
          I believe you have to use Websocket if you want instant update. Luckily there is a guide on this:

          Auf YouTube findest du die angesagtesten Videos und Tracks. Außerdem kannst du eigene Inhalte hochladen und mit Freunden oder gleich der ganzen Welt teilen.


          Otherwise I believe you calculating the Age using Formula? And your formula don't get re-update unless you Workflow or update the records. So that another issue you have to solve as well.
          Hi esforim, i just want to show foreign value in the form while creating/editing record.
          The formula work only for before save the record, not work while creating/editing record

          Thanks for the guide, I have watched the video, but this is not what i expected

          Comment

          • esforim
            Active Community Member
            • Jan 2020
            • 2204

            #6
            If you want it to be even more Live I think you will be stuck in a limbo for a long time. I don't remember any thread trying to do what you doing.

            Reading it again, it look like you want it to show the moment you click that "Link Contact"

            Comment

            • czcpf
              Senior Member
              • Aug 2022
              • 160

              #7
              You have to define custom field view for the link and extend from views/fields/link, mandatorySelectAttributes or forceSelectAllAttributes and then write custom code in the on select handler to set the field in the parent view. I will try to post an example for you.

              I really wish “link type” fields in Espo could have some option in entityDefs to “pass” the linked entity’s other attributes to its view (besides just id and name). I mean, any the system knows the value of any foreign foreign field created as soon as the link is selected so having to set it manually on create and edit is cumbersome but such is life.

              Comment

              • czcpf
                Senior Member
                • Aug 2022
                • 160

                #8
                As promised, here is an example of how to accomplish this. In this example, I have an entity called DigitalImagingQaQcRecord. It has a link field called digitalImagingQaQcProtocol and three foreign fields kV, mA, and time which are fields of the digitalImagingQaQcProtocol​ link entity.

                Here is a snippet of custom/Espo/Custom/Resources/entityDefs/DigitalImagingQaQcRecord.json

                Code:
                "digitalImagingQaQcProtocol": {
                "type": "link",
                "view": "custom:views/digital-imaging-qa-qc-record/fields/digital-imaging-qa-qc-protocol"
                },
                "kV": {
                "readOnly": true,
                "type": "foreign",
                "link": "digitalImagingQaQcProtocol",
                "field": "kV",
                "view": "views/fields/foreign-float",
                "isCustom": true
                },
                "mA": {
                "readOnly": true,
                "type": "foreign",
                "link": "digitalImagingQaQcProtocol",
                "field": "mA",
                "view": "views/fields/foreign-float",
                "isCustom": true
                },
                "time": {
                "readOnly": true,
                "type": "foreign",
                "link": "digitalImagingQaQcProtocol",
                "field": "time",
                "view": "views/fields/foreign-float",
                "isCustom": true
                },​
                The important line is "view": "custom:views/digital-imaging-qa-qc-record/fields/digital-imaging-qa-qc-protocol", where I have defined a custom view for this link to achieve the desired behavior. This view will automatically determine the foreign fields of the parent entity and set them appropriately in UI. There isn't anything really to customize other than to name the view in accordance with your field name and place it in your client custom folder. I've also attached a short video of it all working. Cheers.

                client/custom/views/digital-imaging-qa-qc-record/fields/digital-imaging-qa-qc-protocol.js
                Code:
                define('custom:views/digital-imaging-qa-qc-record/fields/digital-imaging-qa-qc-protocol', ['views/fields/link'], function (Dep) {
                
                return Dep.extend({
                
                /**
                * foreignLinkAttributeHashMap
                *
                * @private
                * @type {Object[]|null}
                */
                _foreignLinksAttributeHashMap: [],
                
                
                setup: function () {
                
                Dep.prototype.setup.call(this);
                
                //setup foreign link hash map
                this._setupForeignLinkHashMap();
                
                // reRender the foreign field views anytime the link change event fires
                this.listenTo(this, 'change', () => {
                
                this._foreignLinksAttributeHashMap.forEach( function(obj,index,array) {
                
                let parentView = this.getParentView();
                let fieldView = null;
                
                let n = 0;
                
                while(n < 10) {
                if( parentView && parentView.getFieldView && parentView.getFieldView(obj.entityFieldName)) {
                fieldView = parentView.getFieldView(obj.entityFieldName);
                break;
                }
                n++;
                
                if( !parentView.getParentView || !parentView.getParentView() ) {
                break;
                }
                parentView = parentView.getParentView();
                }
                
                if( fieldView && fieldView.isRendered() ) {
                fieldView.reRender();
                }
                },this);
                
                });
                
                },
                
                /**
                * Set foreign fields value when the link is selected
                * @inheritDoc
                *
                * @param {module:model.Class} model A model.
                * @protected
                */
                select: function (model) {
                
                this._foreignLinksAttributeHashMap.forEach( function(obj,index,array) {
                
                let parentView = this.getParentView();
                let fieldView = null;
                
                let n = 0;
                
                while(n < 10) {
                if( parentView && parentView.getFieldView && parentView.getFieldView(obj.entityFieldName)) {
                fieldView = parentView.getFieldView(obj.entityFieldName);
                break;
                }
                n++;
                
                if( !parentView.getParentView || !parentView.getParentView() ) {
                break;
                }
                parentView = parentView.getParentView();
                }
                
                if(fieldView) {
                fieldView.model.set(fieldView.name,model.get(obj.linkFieldName),{silent:true});
                }
                
                
                },this);
                
                Dep.prototype.select.call(this,model);
                },
                
                /**
                * Set foreign fields to null when the link is cleared
                *
                * @inheritDoc
                */
                clearLink: function () {
                
                this._foreignLinksAttributeHashMap.forEach( function(obj,index,array) {
                
                let parentView = this.getParentView();
                let fieldView = null;
                
                let n = 0;
                
                while(n < 10) {
                if( parentView && parentView.getFieldView && parentView.getFieldView(obj.entityFieldName)) {
                fieldView = parentView.getFieldView(obj.entityFieldName);
                break;
                }
                n++;
                
                if( !parentView.getParentView || !parentView.getParentView() ) {
                break;
                }
                parentView = parentView.getParentView();
                }
                
                if(fieldView) {
                fieldView.model.unset(fieldView.name,{silent:true});
                }
                
                },this);
                
                Dep.prototype.clearLink.call(this);
                },
                
                
                /**
                * Setup Foreign Link Hash Map and mandatorySelect Attribute List.
                * @private
                * @returns {void}
                */
                _setupForeignLinkHashMap: function () {
                
                let thisObj = this;
                thisObj._foreignLinksAttributeHashMap = [];
                thisObj.mandatorySelectAttributeList = this.mandatorySelectAttributeList || [];
                
                //get list of foreign fields of the entity this field belongs to
                $.each( this.getMetadata().get(['entityDefs',this.entityType,'fields']),
                function( entityFieldName, entityFieldAttributes )
                {
                if(
                'type' in entityFieldAttributes && entityFieldAttributes.type === 'foreign'
                && 'link' in entityFieldAttributes && entityFieldAttributes.link === thisObj.name &&
                'field' in entityFieldAttributes
                ) {
                thisObj._foreignLinksAttributeHashMap.push({
                entityFieldName:entityFieldName,
                linkFieldName:entityFieldAttributes.field
                });
                thisObj.mandatorySelectAttributeList.push(entityFieldAttributes.field);
                
                }
                }
                );
                },
                
                });
                
                });​​
                Last edited by czcpf; 07-31-2023, 11:50 PM.

                Comment

              • czcpf
                Senior Member
                • Aug 2022
                • 160

                #9
                Yes, Once I’m done with my project I can try to do a pull request. I just don’t have time right now.

                Comment

                • czcpf
                  Senior Member
                  • Aug 2022
                  • 160

                  #10
                  I have edited my code above after realizing it will fail if the link is in a side panel. The latest edit above will go 10 levels above the link view to try to find a parentView that contains the foreign fields. Probably not the most efficient way to do it, but I can't seem to find documentation on how to find the master 'edit' view from within a fieldview such that I can call getFieldView from the topmost parent. This fix is a workaround.

                  Comment

                  Working...