I have a scenario where I'd like to do something like this:

account a = new account (name= 'abc');
contact c1= new contact(firstname = 'first1', lastname = 'last1' );
contact c2= new contact(firstname = 'first2', lastname = 'last2' );
//notice, no insert.

Is there a way for me to relate c1 and c2 to a , and then access them in the context of an a?

The reason I'm trying to do that is that I have a VF page built around the parent object, and i want to display the child records info in a column.

So in VF, I have something like this potentially?

<apex:column >
      <apex:repeat value="{!item.contacts__r}" var="cId">
              <a href="/{!cId}">view</a>
  • Surely you'd at least need to perform DML on creating the Account in order to get the Id so you can associate your contacts with it?
    – Dan Jones
    Commented Aug 5, 2015 at 12:43
  • really trying to avoid that. web services are involved and we get back 100's of parents in the response Commented Aug 5, 2015 at 12:45

2 Answers 2


If you're creating all of these records in memory, then the only way I can think of doing it would be to use dynamic Visualforce binding and use a map of a list of contacts.

You'd have to set some unique value in a field on the account (I don't really know how you're creating these records in memory so this is just an example):

Account a = new Account (name = 'abc', stamp__c = system.now().time());

Map<double, List<Contact>> accountsToContacts = new Map<double, List<Contact>>();

accountsToContacts.put(a.stamp__c, new List<Contact>
    new Contact(firstname = 'first1', lastname = 'last1'),
    new contact(firstname = 'first2', lastname = 'last2')

Then in the page, assuming you're using a public variable for the account:

<apex:repeat value="{!accountsToContacts[a.stamp__c]}" var="c">
  <apex:inputField value="{!c.Email}"/>
  <!-- stuff -->

Since the unique values only need to be in the context of the page then a simple counter should suffice, starting from 0, alternatively the webservice you're receiving accounts from may provide an external ID that you can use.


You could consider using a wrapper class for this. You can probably make this work with maps, but you might find that a wrapper class is more flexible and easier to work with.

Visualforce Page

<apex:page controller="Controller">
    <apex:pageBlock >
        <apex:repeat value="{!TestData}" var="wrapper">
            <apex:outputText value="{!wrapper.a.Name}" />
            <apex:dataTable value="{!wrapper.c}" var="contact">
                <apex:column >
                    <a href="/{!contact.Id}">view</a>


public class Controller{

    public list<WrapperClass> TestData {get; set;}

    public Controller(){
        TestData = new list<WrapperClass>{
            new WrapperClass(
                new account (name= 'abc'), 
                new list<Contact>{
                    new contact(firstname = 'first1', lastname = 'last1' ),
                    new contact(firstname = 'first2', lastname = 'last2' )

    public class WrapperClass{
        public Account a {get; set;}
        public list<Contact> c {get; set;}
        WrapperClass(Account a, list<Contact> c){this.a = a; this.c = c;}


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .