When you bind an object property on a Freedom UI page in Creatio, the designer adds an attribute to the page that the UI element is bound to. You’ll use this attribute name to programmatically set or get the value of the field on the page. However, in current versions of Creatio Freedom UI pages (8.0.7 is the latest release at the time of this article) the name of this attribute is randomly generated making it difficult to write code to work with these attributes. Hopefully, this will change in a future release and the attribute names will match the object property names. However, this article will look at how to determine the name of an attribute, given the object property name, and also how to determine the name of the object property, given the attribute name.
Here’s some context to describe the issue. If I have a object property named “UsrStringField” on the Account object, and I’ve added that to the account page. If I need to set this field via code, I would do the following:
request.$context.UsrStringField = "Some Value";
That works, as long as “UsrStringField” is the name of the attribute the UI element is bound to. However, the page designer creates this attribute when you add the element to the page and you have no control over how it is named. The issue is that the designer will add the attribute as some random name, such as “StringAttribute_5oebwy2”, you can see this in the page’s viewModelConfig. This means, instead of referencing the field name as the code above does to set or get the value, you now need to use this random name instead, as follows:
request.$context.StringAttribute_5oebwy2 = "Some Value";
This is no good at all (and sort of blows my mind that it is even like this in the first place 🤷🏼♂️). Hopefully, this will change and the attribute name will match the field name in a future release. For now, I find myself editing the attribute name and the UI element it is bound to manually so the attribute name is the same as the field name. However, if your code needs to determine this at runtime, you can use the methods below.
Getting the Attribute Given The Field Name
If you know the field name and you want to determine it’s attribute name at runtime you can use the following. In the example below, my field name is UsrStringField.
var attr = request.$context.getAttributeNameByViewItemName("UsrStringField"); // now I can use that to set the value as follows: request.$context[attr] = "Some Value";
As a side note, if you need to determine the data source name that the field is in you can use the following:
var attr = request.$context.getAttributeNameByViewItemName("UsrStringField"); var dsName = request.$context.getBoundDataSourceNameByAttributePath(attr) // for the account page this would be "PDS" although the names of the data sources can vary from page to page
Getting the Field Name Given the Attribute
This is the opposite of the above. In this case, I know the attribute name and I want to get the underlying model property field name it’s bound to. In this case, my attribute name is “StringAttribute_5oebwy2” and the field is “UsrStringField”:
var propName = request.$context.viewModelConfig.attributes.StringAttribute_5oebwy2.viewItemName; console.log("Property name is " + propName); // outputs "UsrStringField"