This question seems a bit vague, but assuming you're using a standard controller for the page you're going to need to write a controller extension, and I think others might find themselves in similar scenarios so I'm going to attempt to provide a useful answer.
Essentially you save the new Object A record manually in the extension controller and then create the Object B records (as many as required) and set their lookup fields using the new ID found on the A record.
The rough skeleton for the controller would like the one below, and instead of calling {!Save}
in your page you need an action button to use the customised saved method. This doesn't include any error checking so you'll want to add some try {} catch {}
blocks in for safety!
Controller Extension
public with sharing ObjBExtension
{
private ApexPages.StandardController sc;
public ObjBExtension(ApexPages.StandardController sc)
{
/* Store a reference to the standard controller so we can use
it's save method later on */
this.sc = sc;
}
public PageReference SaveAndCreateBRecords()
{
ApexPages.PageReference pr = sc.save();
// Cast the SObject returned to the real type
ObjectA__c objA = (ObjectA__c)sc.getRecord();
List<ObjectB__c> bRecords = new List<ObjectB__c>();
// Assuming you want 7 B records, one for each day of the week
for(Integer i = 0; i < 7; i++)
{
bRecords.add(new ObjectB__c(Name = 'Day ' + i), ObjALookup__c = objA.Id);
}
insert bRecords;
/* Returning the page reference from the save method will take
the user to the detail page for the new A record */
return pr;
}
}
Page Snippet
<apex:page controller="{!ObjectA__c}" extensions="ObjBExtension">
<apex:form>
<apex:inputField value="{!ObjectA__c.Name}"/>
<apex:commandButton action="{!SaveAndCreateBRecords}" value="Save"/>
<!-- snip! -->