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
AniltAnilt 

Inserting the lead by passing the field values in the url

Hi Everyone,

 

Code is as below:

 

<apex:page controller="lead_urlparameters">
<apex:form >
<apex:inputText value="{!Organization}" rendered="false"/>
<table >
<tr><td><apex:outputLabel value="Your Name"/><br/>
<apex:inputtext value="{!yourname}" label="Your Name"/></td></tr>
<tr><td><apex:outputLabel value="Your Email"/><br/>
<apex:inputtext value="{!youremail}" label="Your Email"/></td></tr>
<tr><td><apex:outputLabel value="Reason for Contact"><br/>
<apex:selectList value="{!Reason4Contact}" label="Reason for Contact">
<apex:selectOptions value="{!Reasons}"/>
</apex:selectList>
</apex:outputLabel><br/></td></tr>
</table>
<apex:commandButton action="{!save}" value="Save"/>
<apex:commandButton action="{!cancel}" value="Cancel"/>
</apex:form>
</apex:page>

 

Apex:

 

global with sharing class lead_urlparameters {

Lead newLead = new Lead();
String[] Reason4Contact = new String[]{};
public String youremail { get; set; }
public String yourname { get; set; }
Public String Organization { get; set; }
Public lead_urlparameters(){
Map<string,string> urlparameters = ApexPages.currentPage().getParameters();

if (!urlparameters.isEmpty()) {
newLead.Email = ApexPages.currentPage().getParameters().get('Email');
newLead.Description = ApexPages.currentPage().getParameters().get('Reason4Contact');

}
}

public PageReference cancel() {
return null;
}

public PageReference save() {
if (ApexPages.currentPage().getParameters().get('Organization')==null || ApexPages.currentPage().getParameters().get('Organization')=='') {
newLead.Company = 'PopcornApps';
}
if (ApexPages.currentPage().getParameters().get('yourname')==null || ApexPages.currentPage().getParameters().get('yourname')=='') {
newLead.LastName = '[not provided]';
} else {
newLead.LastName = ApexPages.currentPage().getParameters().get('yourname');
}
PageReference pageRef = new PageReference('http://www.google.com');
return pageRef;
}
public List<SelectOption> getReasons() {
List<SelectOption> options = new List<SelectOption>();
options.add(new SelectOption('Select a Topic','Select a Topic'));
return options;
}

public String[] getReason4Contact() {
return Reason4Contact;
}
public void setReason4Contact(String[] Reason4Contact) {
this.Reason4Contact = Reason4Contact;
}
}

 

My Requirement is to pass the field values in url like

vfpage url is enclosed within these brackets

 

na1.salesforce.com\apex\vfpagename?name='xxxx'&email='xx@xxx.com which must insert a lead with these values.

 

there are no exceptions, but not able to insert the records with those values.

 

Please help me

 

 

Best Answer chosen by Admin (Salesforce Developers) 
souvik9086souvik9086

Hi Anilt,

I think you have missed the INSERT/UPSERT keyword inside the save method.

 

If this solve your answer , kindly accept it as solution.

 

Thanks

All Answers

souvik9086souvik9086

Hi Anilt,

I think you have missed the INSERT/UPSERT keyword inside the save method.

 

If this solve your answer , kindly accept it as solution.

 

Thanks

This was selected as the best answer
TanayTanay

PageReference redirect = new PageReference('/apex/xyzpage);

redirect.getParameters().put('id',selectedUser); // selecteduser is a variable , set the value by <apex:param>. u can do it othe way
        redirect.setRedirect(false); // If u redirect as True then u can see u in Url

    return redirect;

vishal@forcevishal@force

Yes, he's correct. In your code you will need to specify record insertion.

 

 

public PageReference save() {
if (ApexPages.currentPage().getParameters().get('Organization')==null || ApexPages.currentPage().getParameters().get('Organization')=='') {
newLead.Company = 'PopcornApps';
}
if (ApexPages.currentPage().getParameters().get('yourname')==null || ApexPages.currentPage().getParameters().get('yourname')=='') {
newLead.LastName = '[not provided]';
} else {
newLead.LastName = ApexPages.currentPage().getParameters().get('yourname');
}

insert newLead;
PageReference pageRef = new PageReference('/' + newLead.Id);
return pageRef;
}

 

Try the changes in RED and let me know.