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
Maf_007Maf_007 

Visual force page block

Hi All,

 

I have custom VF page block where there are two input fields take input and create a contact associated with this account. It also should redirect to another page where customer click to get back to the same page. Now how do I associate this new contact with the account? Also my redirection does not work?? Any help would be appreciated. My code is below:

 

Page Block:

<apex:page standardController="Account" extensions="ContactEntry"  sidebar="false" showHeader="false" >
    <apex:form >
    <apex:pageBlock title="Sales Contacts Quick Entry" >
            <apex:pageBlockButtons >
                <apex:commandButton value="Save" alt="save" action="{!save}" rerender="error" id="savebutton" />
            </apex:pageBlockButtons>
            <apex:pageBlockTable value="{!conts}" var="a" id="table">
                <apex:column headerValue="First Name">
                    <apex:inputField value="{!a.FirstName}"/>
                </apex:column>               
                <apex:column headerValue="Last Name">
                    <apex:inputField value="{!a.LastName}"/>
                </apex:column>                
                <apex:column headerValue="Phone">
                    <apex:inputField value="{!a.Phone}"/>
                </apex:column>
                <apex:column headerValue="Email">
                    <apex:inputField value="{!a.Email}"/>
                </apex:column>
            </apex:pageBlockTable>
    <apex:pageblockButtons location="bottom">
        <div style="text-align:right;margin-right:30px;font-weight:bold;">
            <apex:commandLink value="Add Row" action="{!addRow}" rerender="table,error" immediate="true" />
&nbsp;|&nbsp;&nbsp;
            <apex:commandLink value="Remove Row" action="{!removeRow}" rerender="table,error" immediate="true" />                
        </div>
    </apex:pageblockButtons>  
    </apex:pageBlock>
    </apex:form>
</apex:page>

 

 

And Apex Class:

 

public class ContactEntry{

    public List<Contact> Conts {get; set;}

    public ContactEntry(ApexPages.StandardController myController) {
        Conts = new List<Contact>();
        //Contact c = new Contact();
        Conts.add(New Contact());}
        
    public void addrow() {
        Conts.add(new Contact());}
            
    public void removerow(){
        Integer i = conts.size();
        Conts.remove(i-1);}
            
    public PageReference save() {
        insert Conts;
        PageReference secondPage = Page.test2;
        secondPage.setRedirect(true);
        return secondPage; }
        
    public PageReference OK(){
        PageReference pageRef = new PageReference('/apex/test1');
        return pageref;
    }
}

 

Test 2 is the confirmation page and is below:

 

<apex:page standardController="Account" extensions="ContactEntry"  sidebar="false" showHeader="false" >
    <apex:form >
    <apex:pageBlock title="Sales Contact Save Confirmation" >
            <apex:pageBlockButtons >
                <apex:commandButton value="OK" alt="OK" action="{!OK}" id="confirmbutton" />
            </apex:pageBlockButtons>
            <apex:pageBlock >
            "Your Contacts have been added"
            </apex:pageBlock>
    <apex:pageblockButtons location="bottom">
        <div style="text-align:right;margin-right:30px;font-weight:bold;">             
        </div>
    </apex:pageblockButtons>  
    </apex:pageBlock>
    </apex:form>
</apex:page>

 

 

I get an URL does not exist error???

 

 

Best Answer chosen by Admin (Salesforce Developers) 
Kamatchi Devi SargunanathanKamatchi Devi Sargunanathan

Hi,

 

Yeah, I have your code. You need to include the following code in pagereference function save(),

 

 public Pagereference save(){
         insert Conts;
         PageReference secondPage = new PageReference ('/apex/test2'); //This line you are missing
         secondPage = Page.test2;  // use the name of the VF page to where you need to be redirected
         secondPage.setRedirect(true);
         secondPage.getParameters().put('Param1',string.valueof(para1)); //This statement is used when passing any parameter               
         return secondPage;
     }

 

Hope this will help you...!

 

Don't forget to give Kudos and mark this answer as a Solution if this works out.

All Answers

Kamatchi Devi SargunanathanKamatchi Devi Sargunanathan

Hi Maf,

 

I have checked your page and controller, you have to do some changes in the below highlighted lines in the controller, Please check again.

 

Controller:

 

public with Sharing class ContactEntry{
    public List<Contact> Conts {get; set;}

    public Account acc{get;set;} //This is because you are using the account as a standard controller

    public ContactEntry(ApexPages.StandardController myController) {

        acc = (Account)myController.getRecord(); //This is because you are using the account as a standard controller
        Conts = new List<Contact>();
        //Contact c = new Contact();
        Conts.add(New Contact());}
        
    public void addrow() {
        Conts.add(new Contact());}
            
    public void removerow(){
        Integer i = conts.size();
        Conts.remove(i-1);}
            
    public PageReference save() {

        insert acc; //You need to create the account first
        insert Conts;
        PageReference secondPage = Page.test2;
        secondPage.setRedirect(true);
        return secondPage; }
        
    public PageReference OK(){
        PageReference pageRef = new PageReference('/apex/test1'); //Check if this page exists with some confirmation message
        return pageref;
    }
}

 

Also you didn't have inputs for account object in the first VF page to insert the account.

 

VF page

<apex:page standardController="Account" extensions="ContactEntry"  sidebar="false" showHeader="false" >
    <apex:form >
    <apex:pageBlock title="Sales Contacts Quick Entry" >
            <apex:pageBlockButtons >
                <apex:commandButton value="Save" alt="save" action="{!save}" rerender="error" id="savebutton" />
            </apex:pageBlockButtons>
            <apex:pageBlockTable value="{!conts}" var="a" id="table">
                <apex:column headerValue="First Name">
                    <apex:inputField value="{!a.FirstName}"/>
                </apex:column>      

              <apex:column headerValue="First Name">
                    <apex:InputField value="{!a.AccountId}"/>//To associate the account to create contact
                </apex:column>       
                <apex:column headerValue="Last Name">
                    <apex:inputField value="{!a.LastName}"/>
                </apex:column>                
                <apex:column headerValue="Phone">
                    <apex:inputField value="{!a.Phone}"/>
                </apex:column>
                <apex:column headerValue="Email">
                    <apex:inputField value="{!a.Email}"/>
                </apex:column>
            </apex:pageBlockTable>
    <apex:pageblockButtons location="bottom">
        <div style="text-align:right;margin-right:30px;font-weight:bold;">
            <apex:commandLink value="Add Row" action="{!addRow}" rerender="table,error" immediate="true" />
&nbsp;|&nbsp;&nbsp;
            <apex:commandLink value="Remove Row" action="{!removeRow}" rerender="table,error" immediate="true" />                
        </div>
    </apex:pageblockButtons>  
    </apex:pageBlock>
    </apex:form>
</apex:page>

 

 

Try again by using the above mentioned changes. you will not get the error.

 

Don't forget to give Kudos and mark this answer as a Solution if this works out.

 

 

Maf_007Maf_007

Hi Kamat,

 

Thanks for your reply. I am adding these contacts from account page so I do not want to create a new accout. However, your following bit of code showed me light to bind the contact with account:

 

acc = (Account)myController.getRecord();

 

But now the problem is I want to redirect this page block to test2 page and it is there, code is following:

 

<apex:page standardController="Account" extensions="TestVFController"  sidebar="false" showHeader="false" >
    <apex:form >
    <apex:pageBlock title="Sales Contact Save Confirmation" >
            <apex:pageBlockButtons >
                <apex:commandButton value="OK" alt="OK" action="{!OK}" id="confirmbutton" />
            </apex:pageBlockButtons>
            <apex:pageBlock >
            "Your Contacts have been added"
            </apex:pageBlock>
    <apex:pageblockButtons location="bottom">
        <div style="text-align:right;margin-right:30px;font-weight:bold;">             
        </div>
    </apex:pageblockButtons>  
    </apex:pageBlock>
    </apex:form>
</apex:page>

 

So my following bit of code is not working:

 

PageReference secondPage = Page.test2;

 

it shows the source code for some reason. Do you have any idea???

Kamatchi Devi SargunanathanKamatchi Devi Sargunanathan

Hi,

 

Yeah, I have your code. You need to include the following code in pagereference function save(),

 

 public Pagereference save(){
         insert Conts;
         PageReference secondPage = new PageReference ('/apex/test2'); //This line you are missing
         secondPage = Page.test2;  // use the name of the VF page to where you need to be redirected
         secondPage.setRedirect(true);
         secondPage.getParameters().put('Param1',string.valueof(para1)); //This statement is used when passing any parameter               
         return secondPage;
     }

 

Hope this will help you...!

 

Don't forget to give Kudos and mark this answer as a Solution if this works out.

This was selected as the best answer
Maf_007Maf_007

Hi,

 

My redirection is working fine but I have got a problem on refreshing the parent page. I have gone through lot of materials online and did the following but It's not working. My class is following:

 

public class TestVFController{
    Private Account ACID;
    public List<Contact> Conts {get; set;}
    public Boolean refreshPage {get; set;}
    ApexPages.StandardController stdCtrl;

public TestVFController(ApexPages.StandardController myController) {
    	stdCtrl = myController;
    	refreshPage = false;
        This.ACID = (Account)stdCtrl.getRecord();
        Conts = new List<Contact>();
        Contact c = new Contact();
        c.accountid = ACID.id;
        Conts.add(c);
}
        
    public void addrow() {
        Conts.add(new Contact());}
            
    public void removerow(){
        Integer i = conts.size();
        Conts.remove(i-1);}
            
    public PageReference savereturn() {
        insert Conts;
        //PageReference result = stdctrl.view();
        PageReference result = new PageReference('/apex/test2');
        //PageReference result=ApexPages.currentpage();
        result.setRedirect(true);
        return result;
    }
        
    public PageReference OK(){
        refreshPage = true;
        PageReference pageRef = new PageReference('/apex/test1');
        //pageRef.setRedirect(true);
        return pageref;
    }
}

 

And Javascript in my code is below:

 

<apex:page standardController="Account" extensions="TestVFController"  sidebar="false" showHeader="false" >
    <apex:form >
    <apex:pageBlock title="Sales Contact Save Confirmation" >
            <apex:pageBlockButtons >
                <apex:commandButton value="OK" alt="OK" action="{!OK}" id="confirmbutton" />
            </apex:pageBlockButtons>
            <apex:pageBlock >
            "Your Contacts have been added"
            </apex:pageBlock>
    <apex:pageblockButtons location="bottom">
        <div style="text-align:right;margin-right:30px;font-weight:bold;">             
        </div>
    </apex:pageblockButtons>  
    </apex:pageBlock>
    </apex:form>
    <apex:outputPanel id="refresh" rendered="true"> 
    <apex:outputPanel id ="refresh1" rendered="{!refreshPage}">
   		<script>
     		 window.top.location='{!Account.id}';
   		</script>
	</apex:outputPanel>
    </apex:outputPanel>
</apex:page>

 

The following bit should do the refresh:

 

<apex:outputPanel id="refresh" rendered="true"> 
    <apex:outputPanel id ="refresh1" rendered="{!refreshPage}">
   		<script>
     		 window.top.location='{!Account.id}';
   		</script>
	</apex:outputPanel>
    </apex:outputPanel>
Kamatchi Devi SargunanathanKamatchi Devi Sargunanathan

Hi,

 

I analysed your code and think so you need to do some small changes in the controller & VF pages as mentioned below,

 

In your controller do the following,

 

public class TestVFController{
    public  Account ACID{get;set;}   //This id should be refered while refreshing a page to be with Getter & Setter
    public List<Contact> Conts {get; set;}
    public Boolean refreshPage {get; set;}
    ApexPages.StandardController stdCtrl;

public TestVFController(ApexPages.StandardController myController) {
        stdCtrl = myController;
        refreshPage = false;
        This.ACID = (Account)stdCtrl.getRecord(); //This is the id we need to use while refreshing the page.
        Conts = new List<Contact>();
        Contact c = new Contact();
        c.accountid = ACID.id;
        Conts.add(c);
}
        
    public void addrow() {
        Conts.add(new Contact());}
            
    public void removerow(){
        Integer i = conts.size();
        Conts.remove(i-1);}
            
    public PageReference savereturn() {
        insert Conts;
        //PageReference result = stdctrl.view();
        PageReference result = new PageReference('/apex/test2');
        //PageReference result=ApexPages.currentpage();
        result.setRedirect(true);
        return result;
    }
        
    public PageReference OK(){
        refreshPage = true;
        PageReference pageRef = new PageReference('/apex/test1');
        //pageRef.setRedirect(true);
        return pageref;
    }
}

 

And in your VF page need to change the following,

 

<apex:outputPanel id="refresh" rendered="true">
    <apex:outputPanel id ="refresh1" rendered="{!refreshPage}">
           <script>
              window.top.location='{!ACID.id}'; //Here you need to use the ACID, that is refered in the controller by standard controller instance.
           </script>
    </apex:outputPanel>
</apex:outputPanel>

 

Hope this will help to solve your issue...!

 

Don't forget to give Kudos if this works out for you.

 

Maf_007Maf_007

Hi Kamat,

 

I am already using account controller for this page which is same as ACID id. Please see the following page:

 

 

<apex:page standardController="Account" extensions="TestVFController" sidebar="false" showHeader="false" >
<apex:form >
<apex:pageBlock title="Sales Contact Save Confirmation" >
<apex:pageBlockButtons >
<apex:commandButton value="OK" alt="OK" action="{!OK}" id="confirmbutton" />
</apex:pageBlockButtons>
<apex:pageBlock >
"Your Contacts have been added"
</apex:pageBlock>
<apex:pageblockButtons location="bottom">
<div style="text-align:right;margin-right:30px;font-weight:bold;">
</div>
</apex:pageblockButtons>
</apex:pageBlock>
</apex:form>
<apex:outputPanel id="refresh" rendered="true">
<apex:outputPanel id="refresh1" rendered="{!refreshPage}">
<script>
window.top.location='/{!Account.id}'; // this is the same id i am holding in ACID in my apex class infact if I write ACID.id here it will give an error
</script>
</apex:outputPanel>
</apex:outputPanel>
</apex:page>

Kamatchi Devi SargunanathanKamatchi Devi Sargunanathan

Hi,

 

You are using standard controller right? and also getting the record details in ACID in the controller, So this wont give you error. Try using {ACID.id} instead of {Account.id}

 

<script>
window.top.location='/{!ACID.id}'; // Try this again
</script>

 

Have you tried using it?

 

 

 

Maf_007Maf_007

Hi Kamat,

 

I have tried using this as you suggested but this gives me following error:

 

Unknown Property 'AccountStandardController.ACID'

 

Any idea??

Maf_007Maf_007

I have change the property to get; set and it solves the error but still does not refresh the page....:((((