I am building an Add Account page, and have 3 questions about form entry:
- When entering a new account, how do you refresh the input form fields, so that another value can be entered? I have tried refreshing the section, and the fields unsuccessfully.
- The record insert seem to stop after 1 entry, occasionally 2. Is this down to bad code? or is it more about traffic - trying to create multiple records could be risky?
- The class that I am writing duplicates the SOQL query, how would I write this more efficiently?
Thanks
HTML:
<apex:page docType="html-5.0" controller="addAccountModalController">
<c:overlay />
<apex:form id="inputForm" >
<div id="newAccount">
<div class="modalSubheading">Account Details</div>
<table>
<tr>
<td style="padding-right:10px;">
<apex:inputField id="accountName" styleClass="form-control" value="{!addAccount.name}" html-placeholder="Account Name"/>
</td>
<td style="padding-left:10px;">
<apex:inputField id="primaryEmail" type="email" styleClass="form-control" value="{!addAccount.Primary_Email__c}" html-placeholder="Email Address"/>
</td>
</tr>
</table>
<table style="width:100%;margin-bottom:20px;">
<tr>
<td>
<apex:commandButton styleClass="btn btn-success pull-right" onclick="addAccount();" onComplete="addAccount" value="Add Account"/>
<apex:actionFunction name="addAccount" action="{!addAccount}" status="overlayStatus" reRender="outputForm,inputForm"/>
</td>
</tr>
</table>
</div>
</apex:form>
<apex:form id="outputForm">
<div class="modalSubheading">Existing Accounts</div>
<table>
<thead class="tableHead">
<tr>
<td>Name</td>
<td>Primary Email</td>
<td>Action</td>
</tr>
</thead>
<apex:repeat value="{!allAccounts}" var="acc">
<tr>
<td><apex:inputField value="{!acc.name}" styleClass="form-control"/></td>
<td><apex:inputField value="{!acc.Primary_Email__c}" styleClass="form-control"/></td>
<td>
<apex:commandlink action="{!delAccount}" value="Remove" rerender="outputForm" status="overlayStatus" >
<apex:param name="accId" value="{!acc.Id}" assignTo="{!selectedAccId}" />
</apex:commandlink>
</td>
</tr>
</apex:repeat>
</table>
<table>
<tr>
<td>
<apex:commandButton styleClass="btn btn-save pull-right" action="{!save}" status="overlayStatus" rerender="accountsList" value="Save"/>
</td>
</tr>
</table>
</apex:form>
</apex:page>
Class:
public class addAccountModalController {
public Account addAccount {get;set;}
public Id selectedAccId {get;set;}
public list <Account> allAccounts {get;set;}
public addAccountModalController(){
addAccount = new account(
//recordType.id = '01290000001GbSj',
//recordType.Name = 'Entity'
);
allAccounts = [SELECT id,Name,Primary_Email__c,recordType.id,recordType.Name,createdDate FROM Account ORDER by createdDate DESC LIMIT 5];
}
public PageReference addAccount() {
try {
insert(addAccount);
allAccounts = [SELECT id,Name,Primary_Email__c,recordType.id,recordType.Name,createdDate FROM Account ORDER by createdDate DESC LIMIT 5];
} catch(System.Exception e) {
ApexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR,'Error creating Account.'));
}
return null;
}
public PageReference save() {
try {
update allAccounts;
allAccounts = [SELECT id,Name,Primary_Email__c,recordType.id,recordType.Name,createdDate FROM Account ORDER by createdDate DESC LIMIT 5];
} catch(System.Exception e) {
ApexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR,'Error creating record.'));
}
return null;
}
public pageReference delAccount() {
try {
delete new Account(Id = selectedAccId);
allAccounts = [SELECT id,Name,Primary_Email__c,recordType.id,recordType.Name,createdDate FROM Account ORDER by createdDate DESC LIMIT 5];
} catch(System.Exception e) {
ApexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR,'Error creating record.'));
}
return null;
}
}
Image:
Using only VF tags:
<apex:form >
<apex:pageBlock id="inputForm" >
<apex:inputField id="accountName" styleClass="form-control" value="{!addAccount.name}" html-placeholder="Account Name"/>
<apex:inputField id="primaryEmail" type="email" styleClass="form-control" value="{!addAccount.Primary_Email__c}" html-placeholder="Email Address"/>
<apex:commandButton styleClass="btn btn-success pull-right" onclick="addAccount();" onComplete="addAccount" value="Add Account"/>
<apex:actionFunction name="addAccount" action="{!addAccount}" status="overlayStatus" reRender="outputForm,inputForm"/>
</apex:pageBlock>
</apex:form>
<apex:form >
<apex:pageBlock id="outputForm">
<apex:pageBlockTable value="{!allAccounts}" var="acc">
<apex:column value="{!acc.name}" styleClass="form-control"/>
<apex:column value="{!acc.Primary_Email__c}" styleClass="form-control"/>
<apex:column >
<apex:commandlink action="{!delAccount}" value="Remove" rerender="outputForm" status="overlayStatus" >
<apex:param name="accId" value="{!acc.Id}" assignTo="{!selectedAccId}" />
</apex:commandlink>
</apex:column>
</apex:pageBlockTable>
</apex:pageBlock>
</apex:form>