
I wrote an article previously on how to conditionally make a detail read-only based on the page, or connected column for the detail. In this article I will show how to use column values of the selected row to conditionally allow delete, copy, or edit actions. In this scenario, the detail will have a column named UsrType (a lookup) and the value of that column will determine if the user can delete, copy, or edit the record.
In a detail, we can get the selected row by using the following:
// for a single selected item var item = this.getActiveRow(); // or for multiple selected items var items = this.getSelectedItems(); // side note, you can check to see if any are selected with if (this.isAnySelected()) {...}
The copy and edit functions are only allowed, or enabled, for a single selected row, so we’d only need to use getActiveRow, the delete function is allowed for multiple rows. Once we have that, we can read column values from that object, just like you would on a page, as follows:
var val = item.get("UsrType");
However, one thing to be aware of is that the columns available to read from that record completely depends on the columns in the details layout. Meaning, if our “Type” column isn’t in the column layout, we won’t be able to read that from the selected row. Since the user is able to change the details columns (unless you’re preventing that) we need to ensure that the column we want to read is always in the data for the detail list. We can do this by overriding the detail’s initQueryColumns function and adding our column to the EntitySchemaQuery that retrieves the data for the list.
initQueryColumns: function(esq) { this.callParent(arguments); if (!esq.columns.contains("UsrType")) { esq.addColumn("UsrType"); } }
Once we’ve ensured that our column will always be available in the data for the detail, the rest is easy. All we need to do is override the function we want to conditionally restrict:
- deleteRecords (note, plural name)
- copyRecord
- editRecord
Let’s say we want to prevent copying a row in the detail with a UsrType value of “Primary”. What we’ll do is check the type value, and then if it’s not “Primary” we’ll call the parent copyRecord function to perform the copy normally. The complete code we’d add would look like the following:
initQueryColumns: function(esq) { this.callParent(arguments); if (!esq.columns.contains("UsrType")) { esq.addColumn("UsrType"); } }, copyRecord: function() { var item = this.getActiveRow(), type = item && item.get("UsrType"); if (type && type.displayValue === "Primary") { Terrasoft.showInformation("You cannot copy the primary record."); } else { this.callParent(arguments); } }
The code above checks the type value, if it’s “Primary” it shows a message to the user, otherwise, it will call the parent function and the copy will happen normally.
Note, if you really wanted to ensure no new records were deleted or created based on conditions, you could also add code to prevent it on the back end as well, which is really more secure since it would also validate programmatically created or imported records as well by adding entity events.
Subscribe To Our Newsletter
Join our mailing list to receive the latest Infor CRM (Saleslogix) and Creatio (bpm'online) news and product updates!
You have Successfully Subscribed!