+ Start a Discussion
tinman44tinman44 

Nested List of Sobjects?

Okay...so I have been trying for a couple of hours now to create a nested list that contains three sobjects...and cannot get it working. At this point I think I am simply running into a syntax issue. Checked in the docs but they were not very helpful. Any insight would be greatly appreciated.

 

Essentially what I need to create is a nested List that has the following structure (Forgive me for bad syntax):

 

LIST<Accounts List<Contacts List<Tasks associated with those contacts>>>

 

Or in English:

 

Accounts with all contacts and all tasks associated with those contacts.

 

It would be a simple task in Javascript/S-Control...but I cannot seem to make the conceptual leap to APEX on this one

 

Any help/insight/samples would be greatly appreciated.

 

aalbertaalbert

Not sure this will work for your solution, but you can get all 3 of those objects in a single SOQL query, and pass it into a List. Here is an example where I query the Account, related Contacts and Tasks. Within the For loop, you can see how I access the inner Contacts and Task records.

 

 

List<Account> accounts = [select id, name, (select id, firstname, lastname from Contacts), (select id, subject from tasks) from Account LIMIT 10]; for(Account a: accounts){ List<Contact> contacts = a.Contacts; List<Task> tasks = a.Tasks; }

 

 

 

ShivaShiva

I think this example should help you. Just ignore the email part in this.

 

Create a new page and override the account view page.  

 

<apex:page controller="sendEmail">
    <apex:messages />
    <apex:pageBlock title="Send an Email to Your
            {!account1.name} Representatives">
        <p>Fill out the fields below to test how you might send an email to a user.</p>
        <br />{!account1.name}
        <apex:dataTable value="{!account1.Contacts}" var="contact" border="1">
            <apex:column >
                <apex:facet name="header">Name</apex:facet>
                {!contact.Name}
            </apex:column>
            <apex:column >
                <apex:facet name="header">Email</apex:facet>
                {!contact.Email}
            </apex:column>
        </apex:dataTable>
   
        <apex:form >
        <br /><br />
            <apex:outputLabel value="Subject" for="Subject"/>:<br />    
            <apex:inputText value="{!subject}" id="Subject" maxlength="80"/>
            <br /><br />
            <apex:outputLabel value="Body" for="Body"/>:<br />    
            <apex:inputTextarea value="{!body}" id="Body"  rows="10" cols="80"/>          
            <br /><br /><br />
            <apex:commandButton value="Send Email" action="{!send}" />
        </apex:form>
    </apex:pageBlock>
</apex:page>

 

 

public class sendEmail {
    public String subject { get; set; }
    public String body { get; set; }

    private final Account account1;

    // Create a constructor that populates the Account object
    public sendEmail() {
        account1 = [select Name, (SELECT Contact.Name, Contact.Email FROM Account.Contacts)
                from Account where id = :ApexPages.currentPage().getParameters().get('id')];
    }

    public Account getAccount1() {
        return account1;
    }

    public PageReference send() {
        // Define the email
        Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();

        String addresses;
        if (account1.Contacts != null)
        {
            addresses = account1.Contacts[0].Email;
            // There may be more contacts, so loop through the whole list
            for (Integer i = 1; i < account1.Contacts.size(); i++)
            {
                if (account1.Contacts[i].Email != null)
                {
                    addresses += ':' + addresses;
                }
            }
        }

        String[] toAddresses = addresses.split(':', 0);

        // Sets the paramaters of the email
        email.setSubject( subject );
        email.setToAddresses( toAddresses );
        email.setPlainTextBody( body );
   
        // Sends the email
        Messaging.SendEmailResult [] r =
            Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email});  
       
        return null;
    }
}