You need to sign in to do that
Don't have an account?
Help on VF Page
Hello Experts,
I am beginner in SFDC and trying my hand to develop my first Visual Page with Apex Class. Please help me in understanding the code. Scenario: Create a VF page which has a Account lookup and once you select the account by clicking lookup and click submit , it should display the contact list of that particular account. Once the list appears of associated contacts users should have two options with the associated contact i.e.. Edit and Del
so far I am able to create below VF code which is showing me the Account with lookup but I am struggling with the rest of the part.Account :
<apex:page standardController="contact" >
<apex:sectionHeader title="My Page" />
<apex:pageBlock >
<apex:form >
Account : <apex:inputField id="accountLookup" value="{!Contact.AccountId}"/>
<p> <apex:commandButton action="{! save}" value="Submit!"/> </p>
</apex:form>
</apex:pageBlock>
<apex:relatedList list="Contacts" />
</apex:page>
any help is appreciated. Regards
I dont think standard controller would work in this case..reason is you are creating a visualforcepage on contact as standard controller... and you are trying to update the contact with the selected account and the standard save till redirect to the standard detail page. I recommend using custom controller in this case and add the related list of contacts.. and add edir and delete functionality as you wanted... try this sample code below it might help you
vf page:
<apex:page controller="Lookup" >
<apex:form >
<apex:pageBlock>
<apex:pageBlockSection columns="2">
<apex:pageBlockSectionItem>
<apex:outputLabel >Account</apex:outputLabel>
<apex:inputField value="{!t.parentId}"/>
</apex:pageBlockSectionItem>
</apex:pageBlockSection>
<apex:pageblockButtons>
<apex:commandButton action="{!submit}" value="Submit" reRender="contacts"/>
</apex:pageblockButtons>
</apex:pageBlock>
<apex:pageblock id="contacts" title="Contacts">
<apex:pageBlockTable value="{!cons}" var="c">
<apex:column value="{!c.firstName}"/>
<apex:column value="{!c.lastname}"/>
<apex:column value="{!c.mobilephone}"/>
<apex:column value="{!c.mailingstreet}"/>
<apex:column value="{!c.mailingstate}"/>
<apex:column value="{!c.mailingcountry}"/>
</apex:pageBlockTable>
</apex:pageblock>
</apex:form>
</apex:page>
Controller class:
public with sharing class Lookup {
public Account t{get;set;}
public List<contact> cons{get;set;}
public Lookup ()
{
t =new Account();
cons = new List<Contact>();
}
Public pagereference submit(){
system.debug('Selected Account@@@@> '+t);
cons = [select firstname,lastname,mobilephone,mailingstreet,mailingstate,mailingcountry from contact where AccountId= :t.parentId];
return null;
}
}
All Answers
I dont think standard controller would work in this case..reason is you are creating a visualforcepage on contact as standard controller... and you are trying to update the contact with the selected account and the standard save till redirect to the standard detail page. I recommend using custom controller in this case and add the related list of contacts.. and add edir and delete functionality as you wanted... try this sample code below it might help you
vf page:
<apex:page controller="Lookup" >
<apex:form >
<apex:pageBlock>
<apex:pageBlockSection columns="2">
<apex:pageBlockSectionItem>
<apex:outputLabel >Account</apex:outputLabel>
<apex:inputField value="{!t.parentId}"/>
</apex:pageBlockSectionItem>
</apex:pageBlockSection>
<apex:pageblockButtons>
<apex:commandButton action="{!submit}" value="Submit" reRender="contacts"/>
</apex:pageblockButtons>
</apex:pageBlock>
<apex:pageblock id="contacts" title="Contacts">
<apex:pageBlockTable value="{!cons}" var="c">
<apex:column value="{!c.firstName}"/>
<apex:column value="{!c.lastname}"/>
<apex:column value="{!c.mobilephone}"/>
<apex:column value="{!c.mailingstreet}"/>
<apex:column value="{!c.mailingstate}"/>
<apex:column value="{!c.mailingcountry}"/>
</apex:pageBlockTable>
</apex:pageblock>
</apex:form>
</apex:page>
Controller class:
public with sharing class Lookup {
public Account t{get;set;}
public List<contact> cons{get;set;}
public Lookup ()
{
t =new Account();
cons = new List<Contact>();
}
Public pagereference submit(){
system.debug('Selected Account@@@@> '+t);
cons = [select firstname,lastname,mobilephone,mailingstreet,mailingstate,mailingcountry from contact where AccountId= :t.parentId];
return null;
}
}
Thanks saikishore for your help... Appreciated...
One more thing :
When i am clicking the submit button, its showing the related contact list...But I want the edit button with the contact list....so that, after clicking on edit will redirect me to the contact page edit and save...
Thanks...
use this code
VF Page
<apex:page controller="Lookup">
<apex:form>
<apex:pageBlock>
<apex:pageBlockSection columns="2">
<apex:pageBlockSectionItem >
<apex:outputLabel >Account</apex:outputLabel>
<apex:inputField id="acc_lkid" value="{!t.parentId}"/>
</apex:pageBlockSectionItem>
</apex:pageBlockSection>
<apex:pageblockButtons >
<apex:commandButton action="{!submit}" value="Submit" reRender="contacts"/>
</apex:pageblockButtons>
</apex:pageBlock>
<apex:pageblock id="contacts" title="Contacts">
<apex:pageBlockTable value="{!cons}" var="c">
<apex:column>
<apex:commandLink value="Edit" action="{!edit}" style="color:blue">
<apex:param name="getconId" value="{!c.Id}"/>
</apex:commandLink>
</apex:column>
<apex:column value="{!c.firstName}"/>
<apex:column value="{!c.lastname}"/>
<apex:column value="{!c.mobilephone}"/>
<apex:column value="{!c.mailingstreet}"/>
<apex:column value="{!c.mailingstate}"/>
<apex:column value="{!c.mailingcountry}"/>
</apex:pageBlockTable>
</apex:pageblock>
</apex:form>
</apex:page>
Controller Class:
public with sharing class Lookup {
public Account t{get;set;}
public List<contact> cons{get;set;}
public Lookup ()
{
String accId = System.currentPageReference().getParameters().get('AccId');
t =new Account();
cons = new List<Contact>();
if(accId<>null && accId <>''){
cons = [select Id,firstname,lastname,mobilephone,mailingstreet,mailingstate,mailingcountry from contact where AccountId= :accId];
t=new Account(parentId=accId);
}
}
Public pagereference submit(){
system.debug('Selected Account@@@@> '+t);
cons = [select Id,firstname,lastname,mobilephone,mailingstreet,mailingstate,mailingcountry from contact where AccountId= :t.parentId];
return null;
}
Public pagereference edit(){
String conId = System.currentPageReference().getParameters().get('getconId');
pageReference p = new PageReference('/'+conId+'/e?retURL=/apex/accLookupPage?AccId='+t.parentId);
return p;
}
}
You can modify the edit method as per your needs... I am currently redirecting backto visualforce page once you save/cancel on edit page...yon can do redirects as per your needs...
Hope this helps.
-Create a trigger, if Red is selected delete the all related contacts, if Green is selected map the same value on related contacts, if Blue is selected clone all contacts to the parent account (Parent Account is a field on Account object) record.
thanks in advance...
trigger contactRollup on Contact (after update,after insert,after delete) {
List<Contact> ccList = new List<Contact>();
List<id> Accid = new List<id>();
Map<id,List<Contact>> AccMap = new Map<id,List<Contact>>();
Map<id,Decimal> AccValue = new Map<id,Decimal>();
List<Account> AcctoUpdate = new List<Account>();
List<contact> cntList = new List<contact>();
if(trigger.isInsert || trigger.isUpdate){
for(Contact cc : trigger.new){
ccList.add(cc);
Accid.add(cc.AccountID);
}
}
if(trigger.isDelete){
for(Contact cc : trigger.old){
ccList.add(cc);
Accid.add(cc.AccountID);
}
}
for(Account Acc : [Select id,Total_Revenue__c,(Select id,Add_Revenue__c from Contacts)from Account where id in:Accid]){
//AccMap.put(acc.id,Acc.Contacts);
Decimal totRev = 0;
for(Contact cc : Acc.Contacts){
totRev += cc.Add_Revenue__c;
}
Acc.Total_Revenue__c = totRev ;
System.debug('+++Acc.Total_Revenue__c+++++' +Acc.Total_Revenue__c);
AcctoUpdate.add(Acc);
}
update AcctoUpdate;
}