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
TrickTrick 

Vusual force problem explanation

 

Hi Friends ,
Can somebody please explain lines in red.I need to understand the functionality.For example,what kind of value will  value="{!account.contacts}" have. Is it retrieving account or contact information etc.
<apex:page standardController="account">
    <apex:pageblock title="Hello {!$user.firstname}">
    You are displaying contact from the {!account.name}account.
    click contact's name to view his or her details.
    </apex:pageblock>
    <apex:pageblock title="contacts">
        <apex:form>
            <apex:datatable value="{!account.contacts}" var="contact" cellpadding="4" border="1">
                <apex:column>
                    <apex:commandlink rerender="detail">
                        {!contact.name}
                        <apex:param name="cid" value="{!contact.id}"/>
                    </apex:commandlink>
              </apex:column>
            </apex:datatable>
         </apex:form>
      </apex:pageblock>
      <apex:outputpanel id="detail">
          <apex:detail subject="{!$currentpage.parameters.cid}" relatedlist="false" title="false"/>
      </apex:outputpanel>       
</apex:page>

 

 

Thanks,

Trick

Best Answer chosen by Admin (Salesforce Developers) 
Cory CowgillCory Cowgill

First, I would suggest reading the Force.com Developer Guide. This is almost identical to the example in Chapter 9, Section "Developing Visualforce Pages" around Page 250 or so. They explain this crystal clear, and I doubt I can explain it better, but I'll add my thoughts anyway.

 

This VF Page is using the standard Account Controller. So when a user accesses this page the VisualForce already has access to the Account Record. So if we go through your lines:

 

<apex:datatable value="{!account.contacts}" var="contact" cellpadding="4" border="1">

      -This line is createing a data table and iterating over all the contacts for your account. Each individual contact is called 'Contact' per the var attribute. It will iterate over every contact in the account.contacts array and generate HTML for each one.

 

 {!contact.name}

                        <apex:param name="cid" value="{!contact.id}"/>

-- These two lines are displaying the current Contacts name and creating a parameter for each Contact ID, which can be used to find a specific contact
 <apex:detail subject="{!$currentpage.parameters.cid}" relatedlist="false" title="false"/>
-- this line is creating an Apex Detail section for the Contact provided in the cid parameter, which is the <apex:param> tag above.

All Answers

Cory CowgillCory Cowgill

First, I would suggest reading the Force.com Developer Guide. This is almost identical to the example in Chapter 9, Section "Developing Visualforce Pages" around Page 250 or so. They explain this crystal clear, and I doubt I can explain it better, but I'll add my thoughts anyway.

 

This VF Page is using the standard Account Controller. So when a user accesses this page the VisualForce already has access to the Account Record. So if we go through your lines:

 

<apex:datatable value="{!account.contacts}" var="contact" cellpadding="4" border="1">

      -This line is createing a data table and iterating over all the contacts for your account. Each individual contact is called 'Contact' per the var attribute. It will iterate over every contact in the account.contacts array and generate HTML for each one.

 

 {!contact.name}

                        <apex:param name="cid" value="{!contact.id}"/>

-- These two lines are displaying the current Contacts name and creating a parameter for each Contact ID, which can be used to find a specific contact
 <apex:detail subject="{!$currentpage.parameters.cid}" relatedlist="false" title="false"/>
-- this line is creating an Apex Detail section for the Contact provided in the cid parameter, which is the <apex:param> tag above.
This was selected as the best answer
TrickTrick

Thanks cory for the explanation .I have been reading and practicing visualforce.So it will take me a while to understand and become good at it.

 

I have another problem can u help me with that.

 

1) How will constructor fire in the below given code.Here, we are only accessing the code through visual force.We are not creating any object so how will constructor get fired.

 

2) The code has used upsert function.If I am not mistaken upsert can be used to either insert or update a record.I don't think that we can insert new record because we have associated existing account record with the code .Hence,every time I access a record I see  a value in it which means I can only update and not insert.

 

I know I am missing something,Can u please help me with that.

 

Do u have some knowledge about the below given line:-? if possible please explain.

 

return (new ApexPages.StandardController(account)).view();

 

 

 

 

public class NewAndExistingController {
public Account account {get; ate set;}
public NewAndExistingController() {
Id id = ApexPages.currentPage().getParameters().get('id');
account = (id == null) ? new Account() :
[SELECT name, phone, industry FROM account WHERE id = :id];
{
public PageReference save() {
try {
upsert(account);
} catch(System.DMLException e) {
ApexPages.addMessages(e);
return null;
{
// After Save, navigate to the default view page:
return (new ApexPages.StandardController(account)).view();
{
{
The following Visualforce markup shows how the custom controller from above can be used in a page:
<apex:page controller="NewAndExistingController" tabstyle="Account">
<apex:form>
<apex:pageBlock mode="edit">
<apex:pageMessages/>
<apex:pageBlockSection>
<apex:inputField value="{!Account.name}"/>
<apex:inputField value="{!Account.phone}"/>
<apex:inputField value="{!Account.industry}"/>
</apex:pageBlockSection>
<apex:pageBlockButtons location="bottom">
<apex:commandButton value="Save" action="{!save}"/>
</apex:pageBlockButtons>
</apex:pageBlock>
</apex:form>
</apex:page>

 

 

Thanks,

 

Trick

Cory CowgillCory Cowgill

1. A Constructor for a Controller or Extension will get fired with the Visualforce Page is requested by the browser prior to rendering the page. Salesforce nows which constructors to invoke because you specify them in the attributes of the visualforce apex:page tag (controller, extension). The contructor will execute first, then the page will render with values from the controller as appropriate.

 

2. It looks like this code will do either an insert or a update based on if they pass a paramter named id in the URL. Soemthing like this will perform Upsert: na7.salesforce.com/apex/NewAndExistingPage?id=001ABCDEFGHKIJK. Something like this na7.salesforce.com/apex/NewAndExistingPage would perform an insert based on how your constructor is handling the input.

 

3. return (new ApexPages.StandardController(account)).view(); statement will naviage the user to the Standard Salesforce View Page for this account. Documentation on this is provided in the Force.com Visualforce Guide.