You need to sign in to do that
Don't have an account?
prati@salesforce
passing parameters to vf page
Hi...everbody.I was referring to the visualforce developer guide and I came across this example in vf testing, But I am gettig an error as failed. I am not bale to save the record may be because I am not able to pass the parameters to URL which is the only condition to save the values to Lead. Here are my vf page and controller codes..
Controller______________________
public class thecontroller {
private String firstName;
private String lastName;
private String email;
private String qp;
public thecontroller() {
this.qp = ApexPages.currentPage().getParameters().get('qp');
}
public String getFirstName() {
return this.firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return this.lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getEmail() {
return this.email;
}
public void setEmail(String email) {
this.email = email;
}
public PageReference save() {
PageReference p = null;
if (this.qp == null || !'yyyy'.equals(this.qp)) {
p = Page.failure;
p.getParameters().put('error', 'noParam');
} else {
try {
Lead newLead = new Lead(LastName=this.lastName,
FirstName=this.firstName,
Email=this.email);
insert newLead;
} catch (Exception e) {
p = Page.failure;
p.getParameters().put('error', 'noInsert');
}
}
if (p == null) {
p = Page.success;
}
p.setRedirect(true);
return p;
}
}
and my visualforce code is____________________________
<apex:page controller="thecontroller" tabStyle="Contact">
<apex:pageBlock >
<apex:form >
<p>First Name: <apex:inputText value="{!FirstName}"/></p>
<p>Last Name: <apex:inputText value="{!LastName}"/></p>
<p>Email: <apex:inputText value="{!Email}"/></p>
<apex:commandButton value="SAVE" action="{!save}"/>
</apex:form>
</apex:pageBlock>
</apex:page>
Controller______________________
public class thecontroller {
private String firstName;
private String lastName;
private String email;
private String qp;
public thecontroller() {
this.qp = ApexPages.currentPage().getParameters().get('qp');
}
public String getFirstName() {
return this.firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return this.lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getEmail() {
return this.email;
}
public void setEmail(String email) {
this.email = email;
}
public PageReference save() {
PageReference p = null;
if (this.qp == null || !'yyyy'.equals(this.qp)) {
p = Page.failure;
p.getParameters().put('error', 'noParam');
} else {
try {
Lead newLead = new Lead(LastName=this.lastName,
FirstName=this.firstName,
Email=this.email);
insert newLead;
} catch (Exception e) {
p = Page.failure;
p.getParameters().put('error', 'noInsert');
}
}
if (p == null) {
p = Page.success;
}
p.setRedirect(true);
return p;
}
}
and my visualforce code is____________________________
<apex:page controller="thecontroller" tabStyle="Contact">
<apex:pageBlock >
<apex:form >
<p>First Name: <apex:inputText value="{!FirstName}"/></p>
<p>Last Name: <apex:inputText value="{!LastName}"/></p>
<p>Email: <apex:inputText value="{!Email}"/></p>
<apex:commandButton value="SAVE" action="{!save}"/>
</apex:form>
</apex:pageBlock>
</apex:page>
I recreated your code and I noticed that there may be only 2 small things missing from it, in order to get it to work.
1. Make sure in your URL you have a parameter = 'yyyy' since you require it to match that string as one of your checks.
/apex/pratiVisualForcePage?qp=yyyy
2. When creating your new Lead record make sure to include the Company field as well. Unless you made this field nor required in your org.
Hope this all helps.
All Answers
I recreated your code and I noticed that there may be only 2 small things missing from it, in order to get it to work.
1. Make sure in your URL you have a parameter = 'yyyy' since you require it to match that string as one of your checks.
/apex/pratiVisualForcePage?qp=yyyy
2. When creating your new Lead record make sure to include the Company field as well. Unless you made this field nor required in your org.
Hope this all helps.
Additional links:
https://www.interactiveties.com/blog/archive/pass-vars-url.php
http://www.forcetree.com/2009/06/passing-parameters-to-visualforce-page.html
Please remember to mark this question as solved if I was able to help.