2

I want a VF page that would aid creating multiple child records for selected IDs.

As an example, I would start from Account, select 5 or 7 Contacts, click a button which would take me to a custom VF page on a custom object where I can add data a la Excel style. Contact ID would be a parent ID.

Ideally it would use standard controller.

Do you think it's possible or have you seen any examples?

I am going to guess it could be a good time saver for many teams!

Thanks ~dzh

EDIT: Code I've got so far.

VF:

<apex:page standardController="Electricity_Meter__c" recordSetVar="ElecMeters" extensions="massQuoting" tabStyle="Electricity_Meter__c" sidebar="false">
    <apex:form >   
            <apex:pageBlock title="Populate all fields with the same value" mode="edit" id="masspgblck">          
            <apex:pageBlockTable value="{!Electricity_Meter__c}" var="mult" >
                   <apex:column headerValue="RecordType" value="{!mult.RecordType.Name}"/>
                    <apex:column headervalue="Linked Basket" footervalue="Linked Basket">
                    <apex:inputField value="{!mult.Linked_Basket2__c}">
                    <apex:actionSupport event="onchange" rerender="muselected"/>
                    </apex:inputField>                   
                    </apex:column>
</apex:pageBlockTable>
        </apex:pageBlock>                
        <apex:pageblock title="Selected Meters" id="muselected">
        <apex:pageMessages />               
        <apex:pageBlockButtons >       
            <apex:commandButton value="Save" action="{!save}" />
            <apex:commandButton value="Cancel" action="{!cancel}"/>           
        </apex:pageBlockButtons>
<apex:column headervalue="Linked Basket" footervalue="Linked Basket">
                    <apex:inputField value="{!elecM.Linked_Basket2__c}"/>                
                    </apex:column>   
                    <apex:column headerValue="Day Rate" footerValue="Day Rate">
                        <apex:inputField value="{!newQuotes.Day_rate__c}"/>
                    </apex:column>
</apex:pageBlockTable>
        </apex:pageBlock>   
    </apex:form>
</apex:page>

Apex extension:

public class massQuoting{
  ApexPages.StandardSetController mycon;
  public List<Quote__C> newQuotes {get; set;}
  public List<Electricity_Meter__c> Selectedmeters {get; set;}
  ID accId{get;set;}
  public massQuoting(ApexPages.StandardSetController controller) {
    accId = ApexPages.currentPage().getParameters().get('id');
    mycon = controller;
    Selectedmeters = mycon.getSelected();
  }
    public void save(){
    newQuotes = new list<Quote__c>();
    for (Electricity_Meter__c m : Selectedmeters){
        newQuotes.add(new Quote__c(       
        Day_rate__c=decimal.valueOf('0.01'), /* needs to be entered from VF */
        Standing_Charge__c=decimal.valueOf('120'), /* needs to be entered from VF */
        Supplier__c='Avanti Gas', /* needs to be entered from VF */
        All_Products__c=m.id       
        ));
    }
    try {
        insert newQuotes;
    } catch (DmlException ex){
        ApexPages.addMessages(ex);
    }
    mycon.save();  
    }
}
4
  • This is a bit too open ended for SFSE, but I can say that a standard set controller is only going to get you as far as finding the list of contact records selected. Everything else will need to be handled via components or a custom extension controller. Maybe have a crack at building something out then ask if you get stuck with a specific part of the solution.
    – Matt Lacey
    Feb 5, 2014 at 13:09
  • I believe you are right about this - it is quite broad, however what I was seeking was direction to approach rather than finished code. It's a shame SFSE is discouraging that. I think it could have been a good discussion about limitations of VF, and several pretty good answers within an hour proves something. Nevertheless, I think I have found something than I can use as a foundation: bobbuzzard.blogspot.co.uk/2011/07/…
    – dzh
    Feb 5, 2014 at 14:50
  • Yeah I get where you're coming from, it was flagged by 5 different users as being too broad too though. It comes down to using the right tool for the job. The StackExchange model was designed specifically to avoid discussion and as such it does not facilitate it well at all. The Developer Boards on the other hand are great for open ended and on-going discussion, though they're less suited (IMHO) to getting a fast and accurate answer.
    – Matt Lacey
    Feb 5, 2014 at 22:35
  • Thanks for understanding. I do dislike the way SE sites can appear hostile at first but it's the precise nature that makes them so powerful :)
    – Matt Lacey
    Feb 6, 2014 at 23:45

2 Answers 2

2

You can accomplish this with the "recordSetVar" attribute on your Visualforce page.

From the apex:page documentation:

(the recordSetVar) attribute indicates that the page uses a set-oriented standard controller. The value of the attribute indicates the name of the set of records passed to the page. This record set can be used in expressions to return values for display on the page or to perform actions on the set of records. For example, if your page is using the standard accounts controller, and recordSetVar is set to "accounts", you could create a simple pageBlockTable of account records by doing the following:

<apex:pageBlockTable value="{!accounts}" var="a">
    <apex:column value="{!a.name}"/>
</apex:pageBlockTable>

The recordSetVar gives you a couple options:

  1. Create a completely custom VF page to list out the contacts, and then perform the logic to create the child records on save.
  2. Create a custom visualforce button, add it to a related list on a detail page. Then you could perform the save logic for the selected records on that related list in an action within your Visualforce page.

More info on handling multiple records via the recordSetVar attribute can be found on this developer force blog here: http://developer.force.com/cookbook/recipe/editing-multiple-records-using-a-visualforce-list-controller

Your Visualforce would look something like this. You could create a custom button on the Contact and display it on the Account's contact related list.

<apex:page standardController="Contact" recordSetVar="myContacts" tabStyle="Contact" extensions="MyContactChildCreationExt" action="{!createContactChildren}">
</apex:page>

Your Apex extension controller would look something like this:

/*
 * MyContactChildCreationExt
 * @description Extension class for handling set of records via recordSetVar
 */
public with sharing class MyContactChildCreationExt {

    private List<Contact> contacts {get; set;}

    public MyContactChildCreationExt(ApexPages.StandardSetController controller){
        //Use the standard controller's get records method
        contacts = (List<Contact>)controller.getRecords();
    }

    public PageReference createContactChildren(){
        List<Child__c> contactChildren = new List<Child__c>();
        for(Contact c : contacts){
            //Create child__c record for each contact
        }
        //insert child__c list here
        //return a new page reference to whichever page
        //maybe the detail page of the account record?
    }
}
7
  • I think you've got the idea where I want to get. I actually already have something similar developed in my org by previous admin, however the purpose and functionality is different. From what I see the button passes ids in this fashion "/apex/page?id={parent id}&ids={record to be edited}&ids={record to be edited}". In my case I will have several parent ids and no child ids as I need to create them. However, I still can't understand what recordSetVar plays, as defined variable is never used in VF code...
    – dzh
    Feb 5, 2014 at 14:39
  • @Dzh, updated my answer with an example of how you would get the records, then create children for those records. Feb 5, 2014 at 15:00
  • James, thanks for help. I am getting somewhere. The issue now is - how do I add those child fields on VisualForce? Adding <apex:inputField value="{!contactChildren.Field__c}"/> will render "Could not resolve the entity from <apex:inputField> value binging '{!contactChildren.Field__c}'. <apex:inputField> can only be used with SObjects, or objects that are Visualforce field component resolvable" error.
    – dzh
    Feb 7, 2014 at 15:53
  • Can you update the question with your visualforce page? Is ContactChildren a list of Contacts? If so, the input field uses single sobjects, so you will need to iterate through the contactChildren using either apex:repeat or apex:pageBlock or some other means, and then use apex:inputField for each contact. Feb 7, 2014 at 16:57
  • James, ContactChildren is a list of children, as in your example. Edit: added code snippets.
    – dzh
    Feb 7, 2014 at 17:07
0

Is shouldn't be hard to achieve your goal. You just need create search block for parents and in some kind of Map <Id, List<childObject>> then just display in page this Map. And in save action just insert map.values()

But why do you need standartController?

1
  • Thanks for steering. The reason of using standard controllers is as usual - for the sake of simplicity.
    – dzh
    Feb 5, 2014 at 14:52

You must log in to answer this question.

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