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
prati@salesforceprati@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>
Best Answer chosen by prati@salesforce
Bryan JamesBryan James
Hi Prati,
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

Bryan JamesBryan James
Hi Prati,
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.
This was selected as the best answer
prati@salesforceprati@salesforce
Thank you for your quick response . I tried including the Company field and the parameter as you mentioned. But it still fails. Also Do I have to include parameter before entering values or after entering?
Bryan JamesBryan James
before you redirect to the page. So add it at the end of your URL and then hit enter. Once the page loads make sure that the parameter is at the end of it. if it is then go ahead and enter your values and check again
 
prati@salesforceprati@salesforce
Thank you it worked.. but I didn’t understand the difference adding the parameters before or after inserting. I am new to visual force development. Could you please send me some useful links regarding passing parameters?
Bryan JamesBryan James
Definatly. Pretty much the basics are that you have a ? after the url of the page you are accessing. the ? tells the browser that anything following it will be a parameter and not a directory or subpage. Then you just enter the name of the parameter as the left hand assignment in this case 'qp' followed by an = and then the value for instance 'yyyy'. You can keep adding more parameters by seperating them with an &. so if you had 2 parameters it would read like https://skinnybeecoding.na24.visual.force.com/apex/pratiVisualForcePage?qp=yyyy&salesforce=AWESOME.

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. 
 
prati@salesforceprati@salesforce
Thanks a bunch :)) I marked it.. You were very helpful