You need to sign in to do that
Don't have an account?

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:
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.
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.
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":
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
Message Edited by TehNrd on 12-16-2008 02:38 PM
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":
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
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
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.
I believe site users only have read access to the Account object.
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
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?
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?
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.
Did you get an answer to this. If so, could you kindly share the code?