You need to sign in to do that
Don't have an account?
Tomasz Woźniak
Adding controller extension intereferes with saving method.
Hello. I'm new to salesforce. I created simple apex page, using standard controller and custom extension. I have contact list for a specific account shown as input fields to edit that straight away. The problem is when I added the extension, standard save function won't work, and I couldn't really find the solution. (without an extension saving works fine, but then I can see EVERY contact, not only contact connected to specific account). I would really appreciate some help. This is the code:
and here is my extension:
I tried adding this code:
<apex:page standardController="Contact" recordSetVar="contacts" extensions="myExt"><apex:form > <apex:pageBlock title="Contacts"> <apex:pageMessages /> <apex:pageBlockButtons > <apex:commandButton action="{! save}" value="Save"/> <apex:commandButton value="Cancel" action="{!cancel}"/> </apex:pageBlockButtons> <apex:pageBlockTable value="{!contacts}" var="contact"> <apex:column ><apex:facet name="header">First name</apex:facet><apex:inputField value="{!contact.FirstName}" required="true"/></apex:column> <apex:column ><apex:facet name="header">Last name</apex:facet><apex:inputField value="{!contact.LastName}" required="true"/></apex:column> <apex:column ><apex:facet name="header">E-mail</apex:facet><apex:inputField value="{!contact.Email}"/></apex:column> <apex:column ><apex:facet name="header">Phone</apex:facet><apex:inputField value="{!contact.Phone}"/></apex:column> <apex:column ><apex:facet name="header">Description</apex:facet><apex:inputField value="{!contact.Description}"/></apex:column> </apex:pageBlockTable> </apex:pageBlock></apex:form> </apex:page>
and here is my extension:
public class myExt { public ApexPages.StandardSetController stdCntrlr {get; set;} public myExt(ApexPages.StandardSetController controller) { stdCntrlr = controller; contactAddList = new List<Contact>(); } private String sortOrder='LastName'; private String acId=ApexPages.currentPage().getParameters().get('Id'); public List<Contact> getContacts(){ List<Contact> results = Database.query( 'SELECT Id, FirstName, LastName, Phone, Email, Description ' + 'FROM Contact WHERE AccountId=\'' + acId + '\' ' + 'ORDER BY ' + sortOrder + ' ASC ' + 'LIMIT 10' ); return results; } }
I tried adding this code:
public PageReference save() { stdCntrlr.save(); return Apexpages.currentPage(); }to invoke standard save method, but it only refreshes the page.
You don't need your own save method since your code is an extension of the standard controller. But you have mis-spellt the call in your Visualforcew code: action="{! save}" There is a space there that shouldn't be.
/Mats
I tried your code and modified the extension class.Try this code and let me know.
public class myExt {
public ApexPages.StandardSetController stdCntrlr{get; set;}
public List<Contact> contactAddList{get;set;}
public myExt(ApexPages.StandardSetController controller) {
stdCntrlr = controller;
contactAddList = new List<Contact>();
}
public String sortOrder='LastName';
public String acId=ApexPages.currentPage().getParameters().get('Id');
public List<Contact> getContacts(){
List<Contact> results = [Select id,FirstName,LastName,Phone,Email,Description From Contact Where AccountId =: acId];
return results;
}
}
1)Variable does not exist: contactAddList
2)System.QueryException: Salutation, LastName, Id, Phone FROM Contact WHERE id = '000000000000000' ^ ERROR at Row:1:Column:78 invalid ID field: null
So for that reason I initialized :
public List<Contact> contactAddList{get;set;}
Secondly,I thought that list is empty and ID is not holding any value.So I have modified the Query to this.
[Select id,FirstName,LastName,Phone,Email,Description From Contact Where AccountId =: acId];
So for the above reasons I modified the code.
Thanks.
contactAddList = new List<Contact>();
shouldn;t exist, I deleted that, it was from the previous version of the code.
As for the ID, you need to open the visual force page with ending &id=accountID
for this to work, then it shows only contacts related to the specific accounts, but this is not the issue. Thnaks anyway
I know it is tooooo long to respond to this Lol...
But I just tweaked your code 'tiny' bit and it works just fine.
1. I pulled the declaration of the List<Account> results out of the inner method to outside and made it public.
2. I tweaked your PageReference save method with some corrections as below.
Rest is all your code and the save button works just fine now.
-Amar
I am also getting the same error, i created a wizard to insert opportunity using standard controller and extension and save button just refreshes the code. My code;
<apex:page standardController="Opportunity" extensions="Wizard">
<apex:form >
<apex:PageBlock >
<apex:pageBlockSection title="Basic Info" rendered="{!page1}" >
<apex:inputField value="{!Opportunity.Name}"/>
<apex:inputField value="{!Opportunity.AccountId}"/>
<apex:inputField value="{!Opportunity.CloseDate}"/>
<apex:inputField value="{!Opportunity.Amount}"/>
<apex:inputField value="{!Opportunity.LeadSource}"/>
<apex:inputField value="{!Opportunity.Description}"/>
</apex:pageBlockSection>
<apex:pageBlockSection title="Opportunity Detail" rendered="{!page2}">
<apex:inputField value="{!Opportunity.NextStep}"/>
<apex:inputField value="{!Opportunity.ForecastCategoryName}"/>
<apex:inputField value="{!Opportunity.IsPrivate}"/>
<apex:inputField value="{!Opportunity.Probability}"/>
<apex:inputField value="{!Opportunity.Type}"/>
</apex:pageBlockSection>
<apex:pageBlockSection title="Additional Information" rendered="{!page3}">
<apex:inputField value="{!Opportunity.TotalOpportunityQuantity}"/>
<apex:inputField value="{!Opportunity.CampaignId}"/>
<apex:inputField value="{!Opportunity.Pricebook2Id}"/>
<apex:inputField value="{!Opportunity.ExpectedRevenue}"/>
<apex:inputField value="{!Opportunity.MainCompetitors__c}"/>
</apex:pageBlockSection>
<apex:pageBlockButtons >
<apex:commandButton value="Previous" action="{!Previous}" rendered="{!hasPrevious}"/>
<apex:commandButton value="Next" action="{!Next}" rendered="{!hasNext}"/>
<apex:commandButton value="Save" action="{!save}" rendered="{!hasSave}"/>
</apex:pageBlockButtons>
</apex:PageBlock>
</apex:form>
</apex:page>
Extension:
public with sharing class Wizard {
public Boolean page1 {get; set;}
public Boolean page2 {get; set;}
public Boolean hasPrevious {get; set;}
public Boolean hasNext {get; set;}
public Boolean hasSave {get; set;}
public Boolean page3 {get; set;}
public Wizard(ApexPages.StandardController controller) {
page1 = true;
page2 = false;
page3 = false;
hasNext =true;
}
public PageReference Next()
{
if(page1 == true)
{
page1 = false;
page2 = true;
page3 = false;
hasPrevious=true;
hasNext = true;
hasSave = false;
}
else
{
page1 = false;
page2 = false;
page3 = true;
hasPrevious=true;
hasSave = true;
hasNext = false;
}
return null;
}
public PageReference Previous()
{
if(page2 == true)
{
page1 = true;
page2 = false;
page3 = false;
hasNext = true;
hasSave = false;
hasPrevious = false;
}
else
{
page1 = false;
page2 = true;
page3 = false;
hasPrevious = true;
hasNext = true;
hasSave = false;
}
return null;
}
}
Any help would be appreciated, also let me know if this code be customized and made more efficient.