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
mgonzalezbicmgonzalezbic 

Nested for loops

Afternoon everyone,

So I was wondering how to approach the following situation, i've been on the platform for about a year and a half now and still learning the programming side of things.

We have a custom obj called Employment History ("EH") which looks up to Contacts (since contacts can have many "employment" records). The EH associates to specific accounts that the person has worked for. The lookup field is called Contact_LU__c and the child relationship name for the lookup is Employment_Ownership_Histories.

What I am trying to do is the following:

I made a standard controller using Account with an extension (ExtensionEHReport) which will get the data so I can show it in a VFP.

I am trying to have the controller get the current EH for the account where status = active as a list and then iterate through the list. My problem is setting criteria in for loop for the list. So I have something like the following:
 
Account acct;
 public  List<Employment_History__c>       getEH {get; set;}
 public  List<Employment_History__c>       pastEH {get; set;}

public EHReportExtension(Apexpages.standardcontroller std)
{  
acct = (Account)std.getRecord();

getEH = [SELECT Contact_LU__c FROM Employment_History__c WHERE Account__c = :acct.Id AND Employee_Type__c ='Principal' AND Employee_Status__c = 'Active' ];

for(Employment_History__c a : getEH)
{
PastEH = [SELECT Account__c,Employee_Type__c,Job_Title__c,Employee_Status__c,Contact_Lu__c,
Disclosure_Date__c,of_Shares_Owned__c,of_ownership__c
FROM Employment_History__c WHERE  <I need to set the Contact_LU__c to the value in the list above (getEH).... but it doesn't work> ];
}
Now I tried using maps, but since they're unordered; I feel as if that's not the right approach, but to be honest I am uncertain about maps and sets and how to use them properly anyway. 

Any help you can provide would be awesome. Thanks.
 
Best Answer chosen by mgonzalezbic
ShotShot
It should look something like that:
public class EHReportExtension
{
    private Account acct;
    public  List<Employment_History__c>  emplHistoryList;

    public EHReportExtension(Apexpages.standardcontroller std)
    {  
        acct = (Account)std.getRecord();
    }

    public List<Employment_History__c> getEmplHistoryList()
    {
        Set<Id> contactIds = new Set<Id>();
        if (acct != null)
        {
            for(Employment_History__c empl : [SELECT Contact_LU__c FROM Employment_History__c WHERE Account__c = :acct.Id AND Employee_Type__c ='Principal' AND Employee_Status__c = 'Active' ]){
                contactsIds.add(empl.Contact_LU__c);
            }

            emplHistoryList = [SELECT Account__c, Employee_Type__c, Job_Title__c, Employee_Status__c, Contact_Lu__c, Disclosure_Date__c, Of_Shares_Owned__c, Of_ownership__c
                               FROM Employment_History__c WHERE  Contact_LU__c in: contactsIds];
        }
        return emplHistoryList;
    }
}

 

All Answers

Pankaj_GanwaniPankaj_Ganwani
Hi,

Can you please try below code:

Set<Id> setContactId = new Set<Id>();
for(Employment_History__c a : getEH)
{
   setContactId.add(a.Contact_LU__c);
}

Now use below,

PastEH = [SELECT Account__c,Employee_Type__c,Job_Title__c,Employee_Status__c,Contact_Lu__c,

Disclosure_Date__c,of_Shares_Owned__c,of_ownership__c

FROM Employment_History__c WHERE Contact_Lu__c IN : setContactId];
 
ShotShot
It should look something like that:
public class EHReportExtension
{
    private Account acct;
    public  List<Employment_History__c>  emplHistoryList;

    public EHReportExtension(Apexpages.standardcontroller std)
    {  
        acct = (Account)std.getRecord();
    }

    public List<Employment_History__c> getEmplHistoryList()
    {
        Set<Id> contactIds = new Set<Id>();
        if (acct != null)
        {
            for(Employment_History__c empl : [SELECT Contact_LU__c FROM Employment_History__c WHERE Account__c = :acct.Id AND Employee_Type__c ='Principal' AND Employee_Status__c = 'Active' ]){
                contactsIds.add(empl.Contact_LU__c);
            }

            emplHistoryList = [SELECT Account__c, Employee_Type__c, Job_Title__c, Employee_Status__c, Contact_Lu__c, Disclosure_Date__c, Of_Shares_Owned__c, Of_ownership__c
                               FROM Employment_History__c WHERE  Contact_LU__c in: contactsIds];
        }
        return emplHistoryList;
    }
}

 
This was selected as the best answer
mgonzalezbicmgonzalezbic
Thanks for the solutions guys. I am getting what I need now but have run into another issue. If the EH has one person with multiple EH's my VFP is showing like this (which I know is because the list is returning what it should):

JANE DOE         President/CEO
JANE DOE         Managing Partner
JOHN DOE         Treasurer
JOHN DOE          Manager

How do I get my VFP to show like this:

JANE DOE
      President/CEO
      Managing Partner

JOHN DOE
      Treasurer
      Manager
mgonzalezbicmgonzalezbic
Actually I figured this out by using the rendered tag in the VFP. Thank you guys for all your help!!
ShotShot
I wonder, how did you decide to do this? Did you create List of lists and grouped them by name?