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
gregusagregusa 

Visualforce Page to insert records

Sorry if this is way too basic but I've been pouring over all of the books I got at dreamforce and the online docs and cannot figure this out.

I'm creating a Visualforce page for a custom object called "Override".  At minimum I'd like the user to be able to insert an Override record on this page (in one row).  However, all I can see in the docs is how to create a page that displays existing records and perform mass updates to them:

Code:
<apex:page standardController="Override__c" recordSetVar="overrides">
<apex:sectionHeader title="Overrides"></apex:sectionHeader>
    <apex:form >
        <apex:pageBlock id="pageRowInsert">
        <apex:pageMessages />
            <apex:pageBlockButtons >
                <apex:commandButton action="{!Save}" value="Save"/>
            </apex:pageBlockButtons>
            <apex:pageBlockTable value="{!overrides}" var="ovr">
               <apex:column ><apex:inputField value="{!ovr.Advisor_Rep__c}"  /></apex:column>
                <apex:column ><apex:inputField value="{!ovr.Institution__c}"  /></apex:column>
                <apex:column headerValue="Opportunity" value="{!ovr.Opportunity__c}"></apex:column>
                <apex:column headerValue="Override Amount" value="{!ovr.Override_Amount__c}"></apex:column>               
            </apex:pageBlockTable>
        </apex:pageBlock>
   </apex:form>
</apex:page>

How do I tell the system that I want empty  inputFields so that I can Insert a record?

And if it's not too much to ask, what I'd really want is for the user to be able to have 10 or 20 empty rows so they could setup 10 to 20 records at one time then press "SAVE" once and have all of them inserted.

Best Answer chosen by Admin (Salesforce Developers) 
TehNrdTehNrd
Here is something I threw together using Accounts.

Code:
Page:

<apex:page controller="multiAccountInsert">
<apex:form >
<apex:pageBlock >

<apex:pageBlockButtons >
<apex:commandButton value="Save" action="{!save}" rerender="error"/>
</apex:pageBlockButtons>

<apex:pageBlockTable value="{!accts}" var="a" id="table">
<apex:facet name="footer">
<apex:commandLink value="Add Row" action="{!addRow}" rerender="table,error"/>
</apex:facet>
<apex:column headerValue="Name">
<apex:inputField value="{!a.Name}"/>
</apex:column>
<apex:column headerValue="Billing City">
<apex:inputField value="{!a.BillingCity}"/>
</apex:column>
</apex:pageBlockTable>

</apex:pageBlock>
</apex:form>
</apex:page>

Controller:

public class multiAccountInsert{

public List<Account> accts {get; set;}

public multiAccountInsert(){
accts = new List<Account>();
accts.add(new Account());
}

public void addrow(){
accts.add(new Account());
}

public PageReference save(){
insert accts;
PageReference home = new PageReference('/home/home.jsp');
home.setRedirect(true);
return home;
}
}

One thing you will notice is that you can't add an additional row until you enter in all the required fields. You can remove this behavior by tweaking this line and adding required="false":

Code:
<apex:inputField value="{!a.Name}" required="false"/>

If you do this you will then need to add some validation to the save method to confirm that the required fields are filled in before saving.

-Jason



All Answers

TehNrdTehNrd
I'm still catching up on the Standard List Controller documentation but I'm thinking that what you are trying to do isn't quite possible with the standard controller. Do you have Enterprise or Unlimited edition? If so this can easily be done with a custom controller or extension.


Message Edited by TehNrd on 12-16-2008 02:38 PM
gregusagregusa
We have Enterprise.  Can someone give me at least a little code sample to get me started?
TehNrdTehNrd
Here is something I threw together using Accounts.

Code:
Page:

<apex:page controller="multiAccountInsert">
<apex:form >
<apex:pageBlock >

<apex:pageBlockButtons >
<apex:commandButton value="Save" action="{!save}" rerender="error"/>
</apex:pageBlockButtons>

<apex:pageBlockTable value="{!accts}" var="a" id="table">
<apex:facet name="footer">
<apex:commandLink value="Add Row" action="{!addRow}" rerender="table,error"/>
</apex:facet>
<apex:column headerValue="Name">
<apex:inputField value="{!a.Name}"/>
</apex:column>
<apex:column headerValue="Billing City">
<apex:inputField value="{!a.BillingCity}"/>
</apex:column>
</apex:pageBlockTable>

</apex:pageBlock>
</apex:form>
</apex:page>

Controller:

public class multiAccountInsert{

public List<Account> accts {get; set;}

public multiAccountInsert(){
accts = new List<Account>();
accts.add(new Account());
}

public void addrow(){
accts.add(new Account());
}

public PageReference save(){
insert accts;
PageReference home = new PageReference('/home/home.jsp');
home.setRedirect(true);
return home;
}
}

One thing you will notice is that you can't add an additional row until you enter in all the required fields. You can remove this behavior by tweaking this line and adding required="false":

Code:
<apex:inputField value="{!a.Name}" required="false"/>

If you do this you will then need to add some validation to the save method to confirm that the required fields are filled in before saving.

-Jason



This was selected as the best answer
gregusagregusa
Just what I needed.  Much appreciated!
sf42_Tobisf42_Tobi

Hi Jason,

 

your code works pretty fine. Thanks for this post.

 

We're trying to enhance the functionality, because our customer doesn't want to fill out the same fields with identical data on any added row.

Do you think there is a way to pass some columns (e.g. date, related item) as variable inside the visualforce page, so we could design a header with two inputfields which will be inserted for each row and only the remaining fieldsfilled out row by row.

 

 

<apex:page controller="multiInsert" tabstyle="our_object__c">
<apex:form >
<apex:pageBlock >

<apex:pageBlockButtons >
<apex:commandButton value="Save" action="{!save}" rerender="error"/>
</apex:pageBlockButtons>

<apex:pageBlockTable value="{!ausl}" var="a" id="table_head">
<apex:column headerValue="FIELD_1">
<apex:inputField value="VARIABLE_1" id="IP01"/>
</apex:column>
<apex:column headerValue="FIELD_2">
<apex:inputField value="VARIABLE_2" id="IP02"/>
</apex:column>
</apex:pageBlockTable>

<apex:pageBlockTable value="{!ausl}" var="a" id="table" rows="3">
<apex:facet name="footer">
<apex:commandLink value="add row" action="{!addRow}" rerender="table,error"/>
</apex:facet>

<apex:column headerValue="FIELD_1a">
<apex:inputField value="{!a.field1__c" id="IP01" rendered="false"/>
</apex:column>
<apex:column headerValue="FIELD_2a">
<apex:inputField value="{!a.field2__c" id="IP02" rendered="false"/>
</apex:column>

<apex:column headerValue="FIELD_3">
<apex:inputField value="{!a.field3__c}"/>
</apex:column>
<apex:column headerValue="FIELD_4">
<apex:inputField value="{!a.field4__c}"/>
</apex:column>
<apex:column headerValue="FIELD_5">
<apex:inputField value="{!a.field5__c}" required="false"/>
</apex:column>
<apex:column headerValue="FIELD_6">
<apex:inputField value="{!a.field6__c}" required="false"/>
</apex:column>
<apex:column headerValue="FIELD_7">
<apex:inputField value="{!a.field7__c}" required="false"/>
</apex:column>
</apex:pageBlockTable>
</apex:pageBlock>
</apex:form>
</apex:page>

 

Possible solutions could also be a Javascript, with an input field to get the variables. But I was not able to pass the variable in the inputfield.

 

 

<script type="text/javascript">
function alertInputValue()
{
var1 = document.getElementById('IP01').value;
alert('Die Variable lautet: 'var1 + 'that's it');
}
</script>

<form>
<input type="text" name="IP01" id="IP01" />
<button type="button" onclick="alertInputValue()">Check variable...</button>
</form>

 

 Thanks for your help.

Tobi

JMPlayer913JMPlayer913
UHHHHHHMMMMMM...This is pretty awesome.
Pratik-o2iPratik-o2i

I tried as given in Solution but it just display 'add row' & 'save' button. And when i tried to view on site it gives me error 'AUTHORIZATION REQUIRED' .

 

Any one suggest me the right way.

TehNrdTehNrd

I believe site users only have read access to the Account object.

Ceri S.ax755Ceri S.ax755

Hi Tobi,

 

Did you get an answer to this, I'm trying to auto complete one field with the name of the related object.

 

Ceri

vihiovihio

When I use my code custom controller or not, on the public site when I input data I get authorization required... I have a free account with forc.com... could that be the reason why?

ThadPThadP

Crazy question, but is it possible to create a controller that will insert both a new account AND a new custom object record in the same controller?  

O kayyaO kayya

Hi Jason,

 

It is great code and helped me a lot. Thank you for posting. I have a question.

 

I tried to create  a test class for this code. I managed the first part, for the pageReference save() I could not manage to figure out. Do you have sample test code you can share with us?

 

Thanks a lot.

nyanginyangi

Did you get an answer to this. If so, could you kindly share the code?

Sahasra LakshmiSahasra Lakshmi
Hi, Any Body give me idea how can we prevent Duplicates while inserting multiple records Here.I want to do it validation before inserting the records to database