Announcement

Collapse
No announcement yet.

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

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

  • 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


  • #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


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

      Hi, thanks for watching our video!In this tutorial we’ll walk you through:- What features will websocket provide you in EspoCRM?- How to create service for w...


      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


      • #4
        Originally posted by Vadym View Post
        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


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

          Hi, thanks for watching our video!In this tutorial we’ll walk you through:- What features will websocket provide you in EspoCRM?- How to create service for w...


          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 espcrm, 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


          • #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


            • #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


              • #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


              • #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


                • #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...
                  X