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
saleforce beesaleforce bee 

Visual force and Page layout

Anyone can help on the below requirements

 

I have a page layout for custom object. In this page layout, had related list to add values to the child object.

As of now single child record would be created at a time by clicking the save button in the page layout .

Now Requirement is to add multiple child records at a single shot of button click in the page layout.  So I thought of going with VF page for related list in the page layout

 

How we can do this?

1) How to pass page layout master object record information to VF child record add page?

2) How to add multiple child records at a single shot of button click?

Laxman RaoLaxman Rao

Create a visual force page, take standard contoller as parent object and take a extension.

In the Visual force page first take the input from user saying number of child records.

Based on the input, you show the child records in pageblock table.

 

Once this is done create a Custom button on Parent saying(create bulk childs) and take the content source as visualforce(the one which you have created above)

 

Lastly add the button to the parent page layout

 

How to pass page layout master object record information to VF child record add page?

the answer is, since we are using standard contoller we can access the values in extension contoller using stdContollerObj.getRecord().

Saurabh DhobleSaurabh Dhoble

You need to create a mass update list using a custom list controller. Here's the details of what I could get working for mass updating a list of contacts  :-

 

Step #1 :- Create a new custom list controller class - this needs to contain a StandardSetController variable. When you update records in this standardsetcontroller, your underlying salesforce contacts will be automatically updated. In the below example, I am creating the standardsetcontroller from a soql query. Note the bolded section- the code looks at the "ID" parameter on the page URL and uses that as the parent account ID. If you are running the VF page standalone for testing, you can explicitly pass in the parameter like this :- https://c.na9.visual.force.com/apex/MassContactUpdatePage?id=001E00dzsadsKC3245

 

public class MassContactUpdate
{
    public ApexPages.StandardSetController masterSet
    {
        get
        {
            if(masterSet == null)
            {
                String soqlquery = 'Select Id, Name, FirstName, LastName, Phone, Email from Contact where Contact.Account.Id = \''+ ApexPages.currentPage().getParameters().get('Id') + '\'';
                System.Debug(soqlquery);
                masterSet = new ApexPages.StandardSetController(Database.query(soqlquery));
            }
                return masterSet;
        }
        set;
    }
    
    public List<Contact> getContactList()
    {
        return (List<Contact>) masterSet.getRecords();
    }
    public void Save()
    {
        masterSet.save();
    }
    
}

 

 

Step # 2 :- Create the VF page. The page **has to** use the controller class we created above. Create a page block table in the class, and specify the "ContactList" property we created earlier in the controller as the source of the table.

The way it works is, you update values in the "ContactList" via the <apex:inputField>, and it is propagated into the "ContactList" property --> the masterSet standard set controller.

 

When you call the "{!Save}" method using the <apex:commandButton>, the masterSet standard set controller is written back to the database, and your contacts are updated.

 

 

<apex:page controller="MassContactUpdate">
<apex:form>
    <apex:pageblock >
        <apex:pageBlockSection >
            <apex:pageBlockTable value="{!ContactList}" var="con">
                <apex:column headervalue="Firstname">
                    <apex:inputField value="{!con.FirstName}"/>
                </apex:column>
                <apex:column headervalue="Lastname">
                    <apex:inputField value="{!con.LastName}"/>
                </apex:column>                
                <apex:column headervalue="Email">
                    <apex:inputField value="{!con.Email}"/>
                </apex:column>                                
                <apex:column value="{!con.Name}"/>
                <apex:column value="{!con.Email}"/>
                <apex:column value="{!con.Phone}"/>
            </apex:pageBlockTable>
        </apex:pageBlockSection>
    </apex:pageblock>
    <apex:commandButton value="Save" action="{!Save}"/>    
</apex:form>    
</apex:page>

 

 

Let me know if this works for you.

saleforce beesaleforce bee

Nicely articulated.Let me try and update you.. Thanks

 

saleforce beesaleforce bee

Thanks Lakshman & Saurabh Dhoble

 

Lakshman idea really would work...

 

Guys if you have any sample code for this....

Laxman RaoLaxman Rao

try this :

 

I am giving the sample for account and contact.

Pass the account Id as the parameter for the page

like https://c.ap1.visual.force.com/apex/delete1?id=0019000000F0YW0

 

Visual force

<apex:page standardController="Account" extensions="delete1">
<apex:form >
<apex:pageBlock >
<apex:pageMessages />
<apex:pageMessage severity="INFO" summary="Below are th required fields"></apex:pageMessage>
<apex:message />

<apex:pageBlockButtons >
<apex:commandButton value="Display Contacts" action="{!displayContacts}"/>
<apex:commandButton value="Save Contacts" action="{!saveContacts}"/>
</apex:pageBlockButtons>
<apex:pageBlockSection title="UserInputs">
<apex:pageBlockSectionItem >
<apex:outputLabel value="Count Of Contacts" />
<apex:inputText value="{!count}" />
</apex:pageBlockSectionItem>
</apex:pageBlockSection>

<apex:pageBlockSection title="Account Details">
<apex:pageBlockTable value="{!contacts}" var="s">
<apex:column headerValue="First Name">
<apex:inputField value="{!s.FirstName}"/>
</apex:column>

<apex:column headerValue="Last Name">
<apex:inputField value="{!s.LastName}"/>
</apex:column>
</apex:pageBlockTable>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:form>
</apex:page>

 

Contoller:

public class delete1 {

public Account acc {get;set;}
public list<Contact> contacts{get;set;}

public integer count{get;set;}

public delete1(ApexPages.StandardController controller) {
acc = (Account) controller.getRecord();
count = 1;
}

public void displayContacts() {
if(count == 0 || count == null) {
Apexpages.Message errorMessage = new Apexpages.Message(ApexPages.Severity.WARNING, 'Please Enter the Count of Contacts.');
Apexpages.addMessage(errorMessage);
} else {
contacts = new list<Contact>();
for(integer i = 1; i <= count; i++) {
Contact c = new Contact(LastName = 'Contact '+ string.ValueOf(i), AccountId = acc.Id);
contacts.add(c);
}
}
}

public PageReference saveContacts() {
insert contacts;
return new PageReference('/' + acc.Id);
}
}