Is there a declarative way to display fields from a lookup on an entity's edit page
Is there a declarative way to display fields from a lookup on an entity's edit page? For example, if I have a Contact record whose Owner field is set and I want to display the Owner's Job Title as a read-only field on the Contact's edit page, is it possible to do without adding some code to the onEntityInitialized method to manually set another attribute which a field is based on to the value of the Job Title field? I can load the field with the record using lookupListConfig as below, but any way I try to specify that this field should be displayed on the page results in errors or nothing happening:
attributes: { "Owner": { lookupListConfig: { columns: ["JobTitle"] } } } // ... diff: [ // example of one attempted method to add the lookup field which doesn't work: { "operation": "insert", "parentName": "ContactGeneralInfoBlock", "propertyName": "items", "name": "OwnerJobTitle", "values": { "bindTo": "Owner.JobTitle", "layout": { "column": 0, "row": 4, "colSpan": 12 }, "enabled": false } } ]
Obviously as mentioned this could be done using code, but if it's possible to do declaratively that would definitely be my preferred option - is this possible?
Like
I don't know of a declarative way to do this. You'd need this in a few parts:
- Add some text virtual attributes to the page and then add those to the diff as readonly
- Populate those attributes when a lookup item is selected which could be done by adding the columns to the lookupListConfig columns. You'd need to wire up a change attribute for the lookup to retrieve those values and populate the attributes.
- onEntityInitialized you'd need to retrieve those values using an ESQ to populate the virtual attributes again.
Ryan
As a side comment, I'd love to be able to declaratively add fields like this, the system could just make them read-only if they are on a connected entity. That would be fantastic.
Hi Ryan, thanks for the reply. Yeah, I have a way to populate the fields in code, but I was just wondering if there was a declarative way to do so - as you say, it would be a great addition to Creatio and I hoped I was just missing something already there!
For anyone else searching for this, my solution slightly differed from yours in using the lookupListConfig to pull the lookup entity's additional field(s) when loading the record, which means you don't have to run an additional ESQ, which saves hits on the DB. It also uses the dependencies attribute rather than the change attribute, but that's just my personal preference as I believe they'd work the same:
attributes: { "Owner": { lookupListConfig: { columns: ["JobTitle"] } }, "OwnerJobTitle": { type: Terrasoft.ViewModelColumnType.VIRTUAL_COLUMN, dataValueType: Terrasoft.DataValueType.TEXT, dependencies: [ { columns: ["Owner"], methodName: "setPickMapFields" } ] } }, methods: { onEntityInitialized: function() { this.callParent(arguments); this.setPickMapFields(); }, setPickMapFields: function() { const owner = this.get("Owner"); if(owner) { this.set("OwnerJobTitle", owner.JobTitle.displayValue); } else { this.set("OwnerJobTitle", null); } } }, diff: [ { "operation": "insert", "parentName": "ContactGeneralInfoBlock", "propertyName": "items", "name": "OwnerJobTitle", "values": { "bindTo": "OwnerJobTitle", "layout": { "column": 0, "row": 4, "colSpan": 12 }, "enabled": false } } ]
Does the job, but it does lack the clarity that a declarative system would provide!
Harvey Adcock,
That's great Harvey, thanks for the update. I was under the impression that adding columns to the lookupListConfig would only load them on selection, great to know that those come along when the lookup data is loaded as well!
Ryan