function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Richard Harrison 20Richard Harrison 20 

I have a parent object and children object. Each child has an address field. How does can I roll this up to the parent and display "multiple" if children have different addresses?

I am trying to figure out how to create the address field in the parent account.  It needs to look at the children, and if all children are the same, then output the address.  If children have different addresses, then output "Multiple".

How should I approach this?
Mario PariseMario Parise
Technically speaking, you can't (or shouldn't) change the parent's Address field to say "Multiple", since that's not an address.

However, what's the context? Are we tallking about a lightning component?

If it's a component, I would loop through all the children to check for addresses and confirm they're all the same:
// Assuming "allMyChildren" is a list along the lines of List<Child__c> and 
// that Child__c has a field called Address__c

Boolean addressesMatch = true;
String firstChildAddress;

for (var i = 0; i < allMyChildren.size(); i++ {
  if (allMyChildren[0].Address__c != allMyChildren[i].Address__c) {
    addressesMatch = false;
  }
}
addressesMatch will be true if and only if they all match.

Then you just check if it's true or not in your component to determine whether or not you want to show your parent object's address or if you want to loop through allMyChildren to show all the different addresses (or just say "Multiple", whatever your case may be).
Mario PariseMario Parise
...oops, change line 7 to:
for (Integer i = 0; i < allMyChildren.size(); i++) {
I spend too much time in Javascript, clearly.