This is a nice example shows how you can edit account and its related contacts using field sets in vf.
First create fieldset on account ,for example AccountsAndContactsEdit fieldset has account name,annual revenue,billing country and description and create another fieldset on contact AccountsAndContactsEdit has account name ,full name ,email,salutation.Now it is time to extension controller
Extension Controller:
public class AccountAndContactsEditExtension {
private ApexPages.StandardController std;
// the associated contacts
public List<Contact> contacts;
// the chosen contact id - used when deleting a contact
public Id chosenContactId {get; set;}
public String newContactFirstName {get; set;}
public String newContactLastName {get; set;}
public AccountAndContactsEditExtension()
{
}
public AccountAndContactsEditExtension(ApexPages.StandardController stdCtrl)
{
std=stdCtrl;
}
public Account getAccount()
{
return (Account) std.getRecord();
}
public SObject getSobject()
{
return std.getRecord();
}
private boolean updateContacts()
{
boolean result=true;
if (null!=contacts)
{
// TODO: should work out what's changed and then save, easier to update everything for prototype
List<Contact> updConts=new List<Contact>();
try
{
update contacts;
}
catch (Exception e)
{
String msg=e.getMessage();
integer pos;
// if its field validation, this will be added to the messages by default
if (-1==(pos=msg.indexOf('FIELD_CUSTOM_VALIDATION_EXCEPTION, ')))
{
ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, msg));
}
result=false;
}
}
return result;
}
public PageReference saveAndExit()
{
boolean result=true;
result=updateContacts();
if (result)
{
// call standard controller save
return std.save();
}
else
{
return null;
}
}
public PageReference save()
{
Boolean result=true;
PageReference pr=Page.AccountAndContactsEdit;
if (null!=getAccount().id)
{
result=updateContacts();
}
else
{
pr.setRedirect(true);
}
if (result)
{
// call standard controller save, but don't capture the return value which will redirect to view page
std.save();
ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.INFO, 'Changes saved'));
}
pr.getParameters().put('id', getAccount().id);
return pr;
}
public void newContact()
{
if (updateContacts())
{
Contact cont=new Contact(FirstName=newContactFirstName, LastName=newContactLastName, AccountId=getAccount().id);
insert cont;
newContactFirstName=null;
newContactLastName=null;
contacts=null;
}
}
public void deleteContact()
{
if (updateContacts())
{
if (null!=chosenContactId)
{
Contact cont=new Contact(Id=chosenContactId);
delete cont;
contacts=null;
chosenContactId=null;
}
}
}
public List<Contact> getContacts()
{
if ( (null!=getAccount().id) && (contacts == null) )
{
contacts=[SELECT Id, Name, Email, Phone, AccountId, Title,
Salutation, OtherStreet, OtherState, OtherPostalCode,
OtherPhone, OtherCountry, OtherCity, MobilePhone, MailingStreet, MailingState,
MailingPostalCode, MailingCountry, MailingCity, LeadSource, LastName,
HomePhone, FirstName, Fax, Description, Department
FROM Contact
WHERE AccountId = : getAccount().ID
ORDER BY CreatedDate];
}
return contacts;
}
}
Visualforce Page:
<apex:page standardController="Account"
extensions="AccountAndContactsEditExtension"
tabStyle="Account" title="Prototype Account Edit">
<apex:pageMessages />
<apex:form >
<apex:pageBlock mode="mainDetail">
<apex:pageBlockButtons location="top">
<apex:commandButton action="{!cancel}" value="Exit" />
<apex:commandButton action="{!save}" value="Save" />
<apex:commandButton action="{!newContact}" value="New Contact" rendered="{!NOT(ISBLANK(Account.id))}" onclick="showpopup(); return false;"/>
</apex:pageBlockButtons>
<apex:pageBlockSection title="Account Details" collapsible="true" id="mainRecord" columns="2" >
<apex:repeat value="{!$ObjectType.Account.FieldSets.AccountsAndContactsEdit}" var="field">
<apex:inputField value="{!Account[field]}" />
</apex:repeat>
</apex:pageBlockSection>
<apex:outputPanel id="contactList">
<apex:repeat value="{!contacts}" var="contact" >
<apex:pageBlockSection columns="1" title="Contact {!contact.Name}" collapsible="true">
<apex:pageBlockSectionItem >
<apex:pageBlockSection columns="2">
<apex:repeat value="{!$ObjectType.Contact.FieldSets.AccountsAndContactsEdit}" var="field">
<apex:inputField value="{!contact[field]}" />
</apex:repeat>
</apex:pageBlockSection>
</apex:pageBlockSectionItem>
</apex:pageBlockSection>
<div style="text-align:center">
<apex:commandButton value="Delete This Contact" onclick="idToDelete='{!contact.id}'; showpopup('deletecontent'); return false;"/>
</div>
</apex:repeat>
</apex:outputPanel>
</apex:pageBlock>
<apex:outputPanel id="addPanel">
<apex:actionRegion id="popupRegion">
<div id="opaque"/>
<div id="popupcontent" class="popupcontent" style="width: 250px; height: 100px;">
Please enter the new contact details
<apex:outputLabel value="First Name: "/><apex:inputText id="newfirst" value="{!newContactFirstName}"/>
<apex:outputLabel value="Last Name: "/><apex:inputText id="newlast" value="{!newContactLastName}"/>
<apex:commandButton id="cancelBtn" value="Cancel" onclick="hidepopup(); return false;"/>
<apex:commandButton id="confirmBtn" action="{!newContact}" value="Create" rerender="contactList, addPanel" onclick="hidepopup();" status="working"/>
</div>
</apex:actionRegion>
</apex:outputPanel>
<apex:actionRegion id="deleteRegion">
<div id="deletecontent" class="popupcontent" style="width: 250px; height: 100px;">
Are you sure you wish to delete contact?
<apex:commandButton id="cancelDelBtn" value="Cancel" onclick="hidepopup('deletecontent'); return false;"/>
<apex:commandButton id="confirmDelBtn" value="Delete" rerender="contactList" onclick="hidepopup('deletecontent'); alert('Deleting contact ' + idToDelete); deleteContact(idToDelete); return false;" status="working"/>
</div>
</apex:actionRegion>
<apex:actionFunction name="deleteContact" action="{!deleteContact}" rerender="contactList" status="working">
<apex:param name="contactIdent" value="" assignTo="{!chosenContactId}"/>
</apex:actionFunction>
</apex:form>
<div id="workingcontent" class="popupcontent" style="width:150px; height:50px; margin-top:-100px; marginleft:-100px">
<p align="center" style='{font-family:"Arial", Helvetica, sans-serif; font-size:20px;}'><apex:image value="/img/loading.gif"/> Please wait</p>
</div>
<apex:actionStatus id="working" onstart="showpopup('workingcontent');" onstop="hidepopup('workingcontent');" />
<script>
function showpopup(popupname)
{
var name="popupcontent";
if (popupname)
{
name=popupname;
}
var popUp = document.getElementById(name);
popUp.style.display = "block";
document.getElementById('opaque').style.display='block';
}
function hidepopup(popupname)
{
var name="popupcontent";
if (popupname)
{
name=popupname;
}
var popUp = document.getElementById(name);
popUp.style.display = "none";
document.getElementById('opaque').style.display='none';
}
var idToDelete;
</script>
<style>
.popupcontent{
position: fixed;
top: 50%;
left: 50%;
margin-top: -100px;
margin-left: -100px;
display: none;
overflow: auto;
border:1px solid #CCC;
background-color:white;
border:3px solid #333;
z-index:100;
padding:5px;
line-height:20px;
font-size: 14px;
}
#opaque {
position: fixed;
top: 0px;
left: 0px;
width: 100%;
height: 100%;
z-index: 1;
display: none;
background-color: gray;
filter: alpha(opacity=30);
opacity: 0.3;
-moz-opacity:0.3;
-khtml-opacity:0.3
}
* html #opaque {
position: absolute;
}
</style>
</apex:page>

No comments:
Post a Comment