Article

Add virtual fields on the Page

Case description:

We need to add virtual fields to page like OpportunityPageV2 which will display the value of the field from the Contact object.

Algorithm of realization:

  1. You should create replacing client schema for your page.
  2. You should create localizable strings which will contain captions of the fields.





    Figure 1. Adding of localizable string

     
  3. You should create attributes for your fields like following:

    "GlbSignatureDeeds": {

        "dataValueType": Terrasoft.DataValueType.DATE,

        "type": Terrasoft.ViewModelColumnType.VIRTUAL_COLUMN

    }

    Enumeration dataValueType specifies the type that will contain the virtual field.



    Figure 2. Terrasoft.DataValueType values

  4. You should add code like following to diff for inserting your field into page:

    {
        "operation": "insert",
        "name": "GlbSignatureDeeds",
        "values": {
            "layout": {
                "colSpan": 12,
                "rowSpan": 1,
                "column": 0,
                "row": 11
            },
            "caption": {
                "bindTo": "Resources.Strings.DeedsSigned"
            },
            "bindTo": "GlbSignatureDeeds",
            "enabled": {
                "bindTo": "Contact"
            },
            "contentType": 0
        },
        "parentName": "GeneralInfoTabGridLayoutef9cc299",
        "propertyName": "items",
        "index": 16
    },

     

  5. You should add an attribute for binding object like Contact. In the attributes property of the view model, you need to add the name of the column for which the dependency is set. For this column, declare the property dependencies, which is an array of configuration objects, each of which contains the following properties: columns - an array of column names, from the values of which the value of the current column depends; methodName is the name of the handler method. 

    "Contact": {
        "lookupListConfig": {
            "columns": ["UsrSignatureDeeds", "UsrContractSigned"]
        },
        "dependencies": [
            {
                "columns": ["Contact"],
                "methodName": "onContactChanged"
            }
        ]
    },

     

  6. You should add the logic to fill the virtual field to “methods”. Usually a virtual field is associated with the field of another object or it is calculated.
    onContactChanged: function() {
        if (this.get("Contact")) {
            this.set("GlbSignatureDeeds", this.get("Contact").UsrSignatureDeeds);
        } else {
            this.set("GlbSignatureDeeds", null);
        }
    },

     

  7. You should call this method from onEntityInitialized method.

    onEntityInitialized: function() {
        this.callParent(arguments);
        this.onContactChanged();
    },

     

  8. If you want to save this virtual fields, youd should add code like following into save() and onSaved() methods:

    save: function() {
        this.set("NeedUpdateDateDeeds", this.changedValues && this.get("Contact") &&
            this.changedValues.hasOwnProperty("GlbSignatureDeeds"));
        this.callParent(arguments);
    },
    onSaved: function(response, config) {
        if (this.get("NeedUpdateDateDeeds")) {
            var args = arguments;
            var update = Ext.create("Terrasoft.UpdateQuery", {
                rootSchemaName: "Contact"
            });
            var contactId = this.get("Contact").value;
            var filter = Terrasoft.createColumnFilterWithParameter(this.Terrasoft.ComparisonType.EQUAL,
                "Id", contactId);
            update.filters.addItem(filter);
            if (this.get("NeedUpdateDateDeeds")) {
                update.setParameterValue("UsrSignatureDeeds", this.get("GlbSignatureDeeds"),
                    this.Terrasoft.DataValueType.DATE);
            }
            update.execute(function() {
                this.set("NeedUpdateDateDeeds", false);
                this.superclass.onSaved.call(this, args);
            }, this);
        } else {
            this.callParent(arguments);
        }
    },

     

 

Like 0

Like

Share

1 comments

I believe with this method that the field brought in from a linked entity does not work properly when you make a modification to the lookup field, but then cancel the change - you end up with the previous value before pressing cancel. i.e. in this example, the virtual field GlbSignatureDeeds would not be updated back to its original value when cancelling the changes.

 

I suggest modifying step 5 so instead the attribute is set up in the following way:

"Contact": {
    "lookupListConfig": {
        "columns": ["UsrSignatureDeeds", "UsrContractSigned"]
    },
    "onChange": "onContactChanged"
},

That way onContactChanged gets run when cancelling the changes made in an edit page.

 

As a bonus, that means you can also remove step 7, as the onContactChanged method is called automatically when loading a record for the first time.

Show all comments