I have a business object, which has properties like streetNumber (string), etc.
I create a new version of this object (no default values, no behavior that sets the initial value to empty string).
On a page, I have a Field element that references that value.
Without setting a default on the field, the value of that field is undefined when a new object instance is created, and the page therefore throws an error like:
vendor.49251c68e24304bb.js:134884 ERROR TypeError: Cannot read properties of undefined (reading 'streetNumber')
at PlaceComponent_apex_flex_column_1_Template (main.c457dd126b455d85.js:129464:94)
at executeTemplate (vendor.49251c68e24304bb.js:139491:5)
at refreshView (vendor.49251c68e24304bb.js:140956:7)
at detectChangesInView$1 (vendor.49251c68e24304bb.js:141174:5)
at detectChangesInViewIfAttached (vendor.49251c68e24304bb.js:141137:3)
at detectChangesInEmbeddedViews (vendor.49251c68e24304bb.js:141095:7)
So, what’s the best practice here? Ideas I have are:
- Set default values on the Business Object fields (Default value on the property itself. And use a behavior to set any relationship values you want to default.
- Set default values and relationship defaults via a behavior on the business object
- Do some magical typescript syntax on the page/component to deal with the property value being undefined (perhaps to set it to an empty string, or default the value in some other way)
tried setting the default values in the properties to ‘’ (error’d out, debug showed values “‘’” (double quotes around single quotes)…and same if I set it to double quotes “”, also gets the same error. Maybe I have something else going on?
seems like I needed to rebuild from scratch, as a prior business object relationship was still showing in the debug of the object incorrectly.
But the question still stands as to the best practice, thanks.
Could you please ping me a link to the component so that I can take a look?
sure, go here and you can see the place object (with empty strings attached to fields like streetNumber) and you’ll see the error like I mentioned:
vendor.49251c68e24304bb.js:134884 ERROR TypeError: Cannot read properties of undefined (reading 'streetNumber')
at PlaceComponent_apex_flex_column_1_Template (main.cd89e318a8155aca.js:129477:94)
at executeTemplate (vendor.49251c68e24304bb.js:139491:5)
After looking at your example, there are some references in the HTML that don’t exist. Here is how I found them. Notice in the error that the “main…” entry name endsd in “_Template”:
That is a sign that the template of your user interface is causing the problem. I clicked on the error and it gave me more information:
With that in hand, I went back to the user interface and noticed that there is not a property placeLocation (perhaps place used to be that). I did a search for placeLocation and found the references:
I changed those to place and everything is fine now :).
my bad, that placeLocation was deleted but I didn’t update the elements.
Hopefully my oopsie this will help others