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
SFDC_2706SFDC_2706 

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

Best Answer chosen by Admin (Salesforce Developers) 
Saikishore Reddy AengareddySaikishore Reddy Aengareddy

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

Saikishore Reddy AengareddySaikishore Reddy Aengareddy

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;
}

}

 

 

This was selected as the best answer
SFDC_2706SFDC_2706

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...

Saikishore Reddy AengareddySaikishore Reddy Aengareddy

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.

SFDC_2706SFDC_2706
this is my query...plzz help me out...
 
¢Create a custom object “Revenue”. Create a field named “Total Revenue”.
¢Create another custom object Revenue Line Items. Create field named “Add Revenue” and lookup to Revenue.
¢Create a trigger on Revenue Line Items to update “Total Revenue” field on Revenue with the total(sum) of “Add Revenue” on all child records to the parent revenue record.
¢Handle insert, update and delete. Also handle change of parent revenue on child record.
 
BRIEF SUMMARY :
suppose “Total Revenue” = 30 and “Add Revenue” = 20...if i add 10 in “Add Revenue” than 
  “Total Revenue” should become 40 automatically and if i subtract 10 from “Add Revenue” than
  “Total Revenue” should become 20....
 
Any help would be appreciated....!!!
SFDC_2706SFDC_2706
this is my query...plzzz someone help me out...
 
-Create a picklist on Account object named Color Codes. Add values Red, Green and Blue to it. Create same picklist on Contact also.
 

-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...

Hemant ThakkarHemant Thakkar
You can take this code for the custom rollup , this code will rollup all your contact child field to the Parent Account . The same you can do with your all Object (Standard or Custom).


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;   
}