/ Development  

The mind-blowing secret behind the irresistible 'Title' BB

Hi there AppWorks fans,

Welcome to a new installment of AppWorks tips.

Last month we saw an error passing by on our project related to the ‘Title’ BB for our ‘Case’ entity. What happened? That’s a question I’ll answer in this post. During the research of the problem, I make an interesting, unaware conclusion. I moved the discussion about the situation with some AppWorks comrades in crime, but also they were not aware of it. So, it’s time to expose it all…The secrets of the ‘Title’ BB!


Let get right into it…

Boot your machine, login to your workspace/project, and have yourself a simple ‘Case’ entity with all the default BBs applied. I create one extra property case_name; Make sure the ‘Title’ BB is applied as well (with the default settings for now):

title_bb_001

I also add the ‘Title’ value in the ‘List’ BB and show its value on the ‘Create’ form; The same I did for the ‘Identity.Id’ property…Just to double-check things!

Do your publication and create some instances for the ‘Case’ entity. The result:

title_bb_02

My first conclusion; The ‘Title’ BB uses this format {Identity.EntityType}-{Identity.Id} by default and the value is used in some header elements which is explained on the building block itself!

Now, change the ‘Title’ BB to a user provided value and leave the rest as is. I do a clean build output first, and do a publication after it. Why cleaning first? Well, such change has (most of the time) a database impact and will confuse the runtime UI…Keep on reading for that DB-impact!

In runtime, we can now fill in the ‘Title’ manually (where it was first non-editable) on our ‘Case’ entities, and we also see these updates back in the header elements…Great! Let’s push it to the limits now and add the maximum number of 256 characters for a ‘Title’ value…WHAT? Yes, that’s the limit!

Save the entity instance and watch what happens:

title_bb_03

My next question would be what the impact would be when we move the ‘Title’ BB back to a generated value by the system and apply a title specification like this:

title_bb_04

So, that’s the 256 chars plus something extra to play with! 😁 Well, publish it (with clean output) and try it out. I removed also all the old instances and create a new one. Now, after opening my new created instance I get an error:

title_bb_05

Well, a correct error (as concluded before); It’s easy solved by cleaning my entity name, but it raises new questions!?!? Keep on reading…


The DB-impact conclusion

Now, I’m getting really curious where this ‘Title’ BB value is saved in the database? So, I open a DB connection to my PostgreSQL instance using HeidiSQL. I first look up my organization ID:

1
2
3
4
5
6
7
8
9
10
11
12
SELECT s_organizationid,
s_payload
FROM s_elementdefinitions
WHERE s_payload LIKE '%{"organizationName%';

-- Or nicer (with PostgreSQL)

SELECT s_organizationid,
s_payload::JSON->'organizationName' AS org_name
FROM s_elementdefinitions
WHERE s_payload::JSON->'organizationName' IS NOT NULL
AND s_payload::JSON->'databaseProviderName' IS NULL;

With this ID I can find my ‘Case’ table with name o2aw_tipsaw_genericcase and execute on this statement:

1
2
3
4
5
SELECT column_name,
data_type,
character_maximum_length
FROM information_schema.columns
WHERE TABLE_NAME = 'o2aw_tipsaw_genericcase';

title_bb_06

Do you see what I see!?!? Yes, my friend…No ‘Title’ column to be found! WHAT, PANIC, 😲…Now what? Well, my conclusion is that the ‘Title’ BB value is generated on the fly in runtime (when not set to have a human interaction)!

What if you DO have a human interaction? Good thinking…You’re paying attention!…Well, in that case it’s indeed saved in the database; In that same table under column s_title as VARCHAR[256]!!

How on earth can we make sure to NOT exceed the max of 256 chars…Watch and learn!


CYA with the ternary operator expression

To make sure the ‘Title’ BB value (generated by the system) is not exceeding the 256 number of characters, we need to check its value and act on it. This “smells” like business logic related to a ‘Rule’ BB, but we can also use a smart expression like this (it contains line breaks for readability!):

1
2
3
4
5
{
item.Properties.case_name.length() <= 10
? item.Properties.case_name
: item.Properties.case_name.substring(0,10) + '...'
}

What does it do? Well, this expression is called a ternary operator; In simple talk, an if-then-else statement written in one line of code. Our example checks if the case_name has a certain length (0-10); If so, set the title with the name; If not, cut the name at 10 characters and add 3 dots (or ellipsis) indicating an intentional omission of text.

Niceeee, but our case_name is only 64 characters in length by default!? Yes, my friend, but we can concatenate properties as well! So, our example (in context of our post) would look like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
{
(
'01234567890123456789012345678901234567890123456789' +
'01234567890123456789012345678901234567890123456789' +
'01234567890123456789012345678901234567890123456789' +
'01234567890123456789012345678901234567890123456789' +
'01234567890123456789012345678901234567890123456789' +
'012345' +
item.Properties.case_name
).length() <= 253
? (
'01234567890123456789012345678901234567890123456789' +
'01234567890123456789012345678901234567890123456789' +
'01234567890123456789012345678901234567890123456789' +
'01234567890123456789012345678901234567890123456789' +
'01234567890123456789012345678901234567890123456789' +
'012345' +
item.Properties.case_name
)
: (
'01234567890123456789012345678901234567890123456789' +
'01234567890123456789012345678901234567890123456789' +
'01234567890123456789012345678901234567890123456789' +
'01234567890123456789012345678901234567890123456789' +
'01234567890123456789012345678901234567890123456789' +
'012345' +
item.Properties.case_name
).substring(0,253) + '...'
}

It looks unclear (and you need to paste it all after each other), but it works for sure to play with; Note the extra parenthesis!

title_bb_07


That’s a “DONE” where we exposed the hidden secrets of the ‘Title’ BB and solved a common problem with the underrated ternary operator. Great stuff, your end-users will love you for this knowledge…we can all sleep well now! Have a good weekend and I CU next week on another great mystery exposure on AppWorks Tips.

Don’t forget to subscribe to get updates on the activities happening on this site. Have you noticed the quiz where you find out if you are also “The AppWorks guy”?