Create an empty div and add this attribute-> lwc:dom="manual"
The link below has a perfect example of how to achieve this:
appendChild to non empty div in lightning web component
Once you're as far as having the list of Ids, you need to massage it so that it reads:
Id IN ('a130Z00000JIdXoQAL','a133q00000RlADNAA3')
To get there, you can write something like:
String assignvar = 'a130Z00000JIdXoQAL, a133q00000RlADNAA3';
String idValues = assignvar.split(', ?'); // Put values into list
// And then join them together with quotes
CASESAFEID is simply too expensive to use in this formula. I wasn't able to get your code to smaller than 10,000 characters, which was optimized as:
You can shrink your formula dramatically by replacing IF(NOT(ISBLANK(X, X, IF(NOT(ISBLANK, Y, etc)) with BLANKVALUE(X, BLANKVALUE(Y, etc)). Once you've done so, that should free up enough characters to wrap the first BLANKVALUE call in CASESAFEID. You can also save some characters by using Parent.Id instead of ParentId for all your terminal field paths.