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
aKallNVaKallNV 

Table rows repeating for each Child Record present in Parent to Child Query

Hi all,

This method populates a pageblockTble that I have built which groups child records by their parent records. I have a simpler version of this method that works fine. However, just got the need for some new logic, and now I'm struggling. The request requires me to provide two different iists based on criteria in the Child records.  The problem is that I'm getting repeating values for each child object. For example, if there are 3 child objects then I see the group of 3 child Objects and its parent repeated 3 times in the table. I understand why this is happening. I have comments to code below to explain.

 

thanks!

 

public void makesStrategyWrappers() {
                
            theDSs = new List<DeployedStrategy__c>();
            theVFDSs = new List<DeployedStrategy__c>();//I use this variable in my VF page as a map key for grouping the child objects
            List<cAIS> cAIS1 = new List<cAIS>();  
            theWrappers = new List<cAIS>();
            theWrapperMap = new Map<Id, List<cAIS>>();
            
            String queryString = this.getStrategyQuery();
            theDSs = Database.query(queryString);
                        
            if(theDSs.size() > 0) {
                    for(DeployedStrategy__c ds : theDSs) {
                        
                        if(ds.DeployedActionItem__r.size() > 0) {
                                for(DeployedActionItems__c actI : ds.DeployedActionItem__r) {
                                        if(actI.Status__c == 'Priority' && actI.Stage__c != 'Completed' && !this.showAll) {                                        	
                                           
                                           theVFDSs.add(ds); //here is where I populate the key. The values repeat because for each child that Iterate //through it adds the same key parent value. How do I populate this with just unique ideas. I tried a set but that just seems to throw errors. 
                                            
                                            cAIS1 = theWrapperMap.get(ds.Id);
                                            
                                            if(null == cAIS1) {       
	                                            cAIS1 = new List<cAIS>();
	                                            theWrappers.add(new cAIS(actI)); 
	                                            theWrapperMap.put(ds.Id, cAIS1);                              
                                            }
                                            cAIS1.add(new cAIS(actI));
                                        }
                                        if(this.showAll){
                                            theVFDSs.add(ds);
                                            cAIS1 = theWrapperMap.get(ds.Id);
                                    
                                            if(null == cAIS1) {       
                                                    cAIS1 = new List<cAIS>();
                                                    theWrappers.add(new cAIS(actI)); 
                                                    theWrapperMap.put(ds.Id, cAIS1);                              
                                            }
                                            cAIS1.add(new cAIS(actI));
                                        }
                                }
                        }
                    }
                }            
        }

 

Best Answer chosen by Admin (Salesforce Developers) 
bob_buzzardbob_buzzard

Visualforce will iterate over a Set - I've done this in a few places in the past (and just verified it).

 

If you want to have a list that only contains unique values, you can create a map where the key is the sobject id and the value is the sobject itself.  That way, if you add the same value twice, the it will overwrite the existing value.  You'd also need a way to get at the values of the map to iterate through on the page.

 

E.g.

 

Map<Id, DeployedStrategy__c> theVFDS=new Map<Id, Deployed_Strategy__c>();

...

theVFDSs.put(ds.id, ds);

public List<Deployed_Strategy__c> getKeys()
{
return theVFDs.values();

 

 

All Answers

sherodsherod

What kind of errors does using a Set throw?

 

You can only use Set's with sObjects, but in your case it shoudl work.

 

You'll may need to cast the set to a List at the end though, I'd be surprised if Visualforce interates over a set.  (I've never tried and the documentation seems a little vague)

bob_buzzardbob_buzzard

Visualforce will iterate over a Set - I've done this in a few places in the past (and just verified it).

 

If you want to have a list that only contains unique values, you can create a map where the key is the sobject id and the value is the sobject itself.  That way, if you add the same value twice, the it will overwrite the existing value.  You'd also need a way to get at the values of the map to iterate through on the page.

 

E.g.

 

Map<Id, DeployedStrategy__c> theVFDS=new Map<Id, Deployed_Strategy__c>();

...

theVFDSs.put(ds.id, ds);

public List<Deployed_Strategy__c> getKeys()
{
return theVFDs.values();

 

 

This was selected as the best answer
aKallNVaKallNV

So the first thing I tried was to just iterate through a Set because it's the easiest. So when I change theVFDSs to be a set.

 

I get the following error when I load the page 'Unknown Property SetValue.Strategy__r 

This is what I ran into last night, and when I did, I thought the same as Sherod which is that you can't iterate through sets in VF.

 

So what  I pasted below is where I iterate through the array(list/set) and the first column of the table, which is, i assume, the one causing the error.

 

and just to be clear, this markup works fine if I use a list, but throws the error if use a set.

 

<apex:pageBlockTable value="{!theVFDSs}" var="d"  >
                              <apex:column headerValue="Strategies">
                                  <b>{!d.Strategy__r.Name }</b>
                              </apex:column>

 

aKallNVaKallNV

Thanks Bob. Your Map solution seems to be working. 

 

Using a Set seems like a better solution, though. So I would still be interested to know if you have any ideas about the errror I get when I try to use a Set.

 

Thanks,

Andy

bob_buzzardbob_buzzard

Interesting.  It seems to be a problem when the set contains sobjects - I've just checked back on mine and they've been primitives.  It smacks that the "SetValue" in the page (sounds low-level possibly a java class) can convert to a string for display if its a primitive, but not when a complex object.

 

You should be able to return this as a list to the page though - have a getter for it like so:

 

public List<DeployedStrategy__c> getTheVFDs()
{
   List<DeployedStrategy__c> result=new List<DeployedStrategy__c>();
   result.addAll(theVFDs);

   return result;
}