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
ShashwatShashwat 

How to make a dynamic table using VF to retreive data and then save new data from a row???

Hi,   
Can anyone help me out with this tast..??
I've created a table, it is retreving data successfully, but the code gives error when i try to save a new data.
Error--- Mandatory field missing.

The table successfully saves data if i place the input field outside the facet tag. but it places an input field in all rows and columns, which is not a proper way...

page---

<apex:page id="Page" controller="acc_add"> 
<apex:pageBlock title="Hello {!$User.FirstName}!"/>           
        <apex:form>
                <apex:sectionHeader title="Accounts through Table"/>
                <apex:pageblock>
                <apex:pageBlockSection title="Accounts">                
                <apex:dataTable value="{!Accounts}" var="account" width="100%" cellPadding="2" cellspacing="1" border="1">

                <apex:column>
                <apex:facet name="header">Name</apex:facet> 
                <apex:outputText value="{!account.name}"/>              
                <apex:facet name="footer">
                <apex:inputText value="{!Name}"></apex:inputText> 
        </apex:facet>
                </apex:column>

                <apex:column>
                <apex:facet name="header">BillingCity</apex:facet> 
                <apex:outputText value="{!account.BillingCity }"/>              
                <apex:facet name="footer">
                <apex:inputText value="{!BillingCity}" ></apex:inputText> 
                </apex:facet>
                </apex:column>                
                
                </apex:dataTable>                
                </apex:pageBlockSection>
                <apex:pageBlockButtons>
                <apex:commandButton action="{!save}" value="Save Data to Account"/>
                </apex:pageBlockButtons>
                </apex:pageBlock>
        </apex:form>        
</apex:page>


controller--------


public class acc_add
{
public string name;
public string BillingCity;

List<Account> Accounts;
public List<Account> getAccounts()
{
if(Accounts == null) Accounts = [select Name, BillingCity from account];
return Accounts;
}

public String getName()
{return this.name;}

public void setName(String name)
{this.name = name;}

public String getBillingCity()
{return this.BillingCity;}

public void setBillingCity(String BillingCity)
{this.BillingCity = BillingCity;}

public pagereference save()
{
Account acc = new Account(Name=this.Name, BillingCity=this.BillingCity);
insert acc;
pagereference p;
p = Page.acc_table;
p.setRedirect(true);
return p;
}

}

___________________
Thanks in anticipation of a reply !!!

Thanks and regards
Shashwat Verma




Message Edited by Shashwat on 04-14-2008 04:05 AM

Message Edited by Shashwat on 04-14-2008 04:06 AM
Best Answer chosen by Admin (Salesforce Developers) 
Ron HessRon Hess
Here is a working multi-line editor, one custom field which you can remove to see how this works in your org.


first the page
Code:
<apex:page controller="multiEditCon" sidebar="false" >
<apex:form>
<apex:pageBlock title="Multi Line Edit">
<apex:pageBlockButtons>

 <apex:commandButton value="Save"  action="{!save}"  rerender="rows" status="outStatus"/>
 <apex:commandButton value="Add"   action="{!add}"   rerender="rows" status="outStatus"/>
 <apex:commandButton value="Reset" action="{!reset}" rerender="rows" status="outStatus" 
  immediate="true" />

</apex:pageBlockButtons>

<apex:pageBlockList value="{!accounts}" var="a" id="rows" > 
  
  <apex:column><apex:inputField value="{!a.name}" required="true"/></apex:column>
  <apex:column><apex:inputField value="{!a.parentid}" required="false"/></apex:column>
  <apex:column><apex:inputField value="{!a.type}" required="false"/></apex:column>
  <apex:column><apex:inputField value="{!a.billingcity}" required="false"/></apex:column>
  <apex:column><apex:inputField value="{!a.SLAExpirationDate__c}" required="false"/></apex:column>

</apex:pageBlockList>


</apex:pageblock>

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

 

then a simple controller
Code:
public class multiEditCon {
 
        List<Account> accountList;  // list of accounts to appear in the multi-line
        
        public PageReference reset() {
            accountList = [select name, Type, billingCity, parentid, SLAExpirationDate__c 
                                                from account order by createddate asc limit 6 ];
            return null;
        }  
        public List<Account> getAccounts() {
             if(accountList == null) reset(); 
             return accountList;
        }
        public void setAccounts(List<Account> accounts) {
              accountList = accounts;
        }
        public PageReference save() {
                upsert accountList;
                return null;
        }
        public PageReference add() {
                accountList.add(New Account());
                return null;
        }
}

 

hope this helps.
   

All Answers

Ron HessRon Hess
could you paste the src code in using the SRC button, that way the sources can be easily re-constructed in an org and we can see what is wrong.
this is full of simley's and won't paste into eclipse properly.

thanks
Ron HessRon Hess
Here is a working multi-line editor, one custom field which you can remove to see how this works in your org.


first the page
Code:
<apex:page controller="multiEditCon" sidebar="false" >
<apex:form>
<apex:pageBlock title="Multi Line Edit">
<apex:pageBlockButtons>

 <apex:commandButton value="Save"  action="{!save}"  rerender="rows" status="outStatus"/>
 <apex:commandButton value="Add"   action="{!add}"   rerender="rows" status="outStatus"/>
 <apex:commandButton value="Reset" action="{!reset}" rerender="rows" status="outStatus" 
  immediate="true" />

</apex:pageBlockButtons>

<apex:pageBlockList value="{!accounts}" var="a" id="rows" > 
  
  <apex:column><apex:inputField value="{!a.name}" required="true"/></apex:column>
  <apex:column><apex:inputField value="{!a.parentid}" required="false"/></apex:column>
  <apex:column><apex:inputField value="{!a.type}" required="false"/></apex:column>
  <apex:column><apex:inputField value="{!a.billingcity}" required="false"/></apex:column>
  <apex:column><apex:inputField value="{!a.SLAExpirationDate__c}" required="false"/></apex:column>

</apex:pageBlockList>


</apex:pageblock>

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

 

then a simple controller
Code:
public class multiEditCon {
 
        List<Account> accountList;  // list of accounts to appear in the multi-line
        
        public PageReference reset() {
            accountList = [select name, Type, billingCity, parentid, SLAExpirationDate__c 
                                                from account order by createddate asc limit 6 ];
            return null;
        }  
        public List<Account> getAccounts() {
             if(accountList == null) reset(); 
             return accountList;
        }
        public void setAccounts(List<Account> accounts) {
              accountList = accounts;
        }
        public PageReference save() {
                upsert accountList;
                return null;
        }
        public PageReference add() {
                accountList.add(New Account());
                return null;
        }
}

 

hope this helps.
   
This was selected as the best answer
programmer4hireprogrammer4hire
Is there a way to use this method in order to create new accounts or opportunities, instead of editing them?