3

enter image description here new to salesforce

i have a visual force page to enter leads which works when used from leads screen. we want to use the same vf page from the contact screen new lead button. when the button is clicked the "contact info" will be pre-populated from the selected contact.

how do i do this? controller extensions? url hacking?

thanks

3

In this question, how to direct a user to the record type selector from a VF page, my answer demonstrates how to make use URL parameters to send information to a controller. In your case, when you get to your VF page, you'll want the Contact info, so it'd be easiest just to send the Contact's id in the URL parameter.

To do this, first create the extension to pick up the URL paramter

public class contactToLeadExtension{
    private final id contactId;
    private final id leadId;

    public contactToLeadExtension(ApexPages.StandardController con){
        this.leadId=con.getId();
        this.contactId=ApexPages.currentPage().getParameters().get('contactId');
    }//END init(ApexPages.StandardController con)

    public void setContactInfo(){
        // in case we're missing either id
        if(contactId==null || leadId==null)
            return;
        Contact theContact=[SELECT id,FirstName,Last,  ~~fields you need~~
                            FROM Contact
                            WHERE id=:contactId];
        Lead theLead=[SELECT id,FirstName,LastName, --fields you want to "preset"--
                      FROM Lead
                      WHERE id=:leadId];
        // preset any Lead field ya like
        theLead.FirstName = theContact.FirstName;
        theLead.LastName = theContact.LastName;
        //  ....
        //  ....

        update theLead;
    }//END setContactInfo()


}//END contactExtension

So, to recap what we have here, in the Constructor, we are setting the leadId from the Standard Controller, and we are setting the contactId from the URL parameter. Then, once we have the ids, we want to update the appropriate Lead fields with the Contact data. Since we want the data "pre-filled", we'll need to update the Lead. And since you can't do a DML call in a Constructor, we'll do it using a page action. To do this, call the function setContactInfo in the action attribute of the <apex:page> tag of your VF page.

<apex:page standardController="Lead" extensions="contactToLeadExtension" action="{!setContactInfo}">

Now that your VF page will pick up the URl parameter contactId, make a custom button for your Contacts, and all you need to do is append the Contact.id to the URL like

/apex/YourVisualForcePageName?contactId={!Contact.id} 
0
  1. Create an apex class for an extension
  2. In the button from the contact screen, user the URL (/apex/PAGENAME) for the VF Page and add a URL Parameter like &conid={!Contact.id}
  3. In the class create in step 1 use apex pages.currentpage().getparameters().get('conid') to get the passed parameter and query for the contact record
  4. Use the Apexpages.StandardController to get the lead and set the value

    public class vf_page_extension{

    public vf_page_controller(Apexpages.standardController con){
    
        Contact[] c = [Select ID, FIELDS From Contact Where ID = :apexPages.currentPage().getparameters().get('conid');
        if(!c.isEmpty()){
             Lead l = (Lead)con.getRecord();
             l.FIELD = c.FIELD;
        }
    }
    

    }

Then you will have to code to ensure that the lead is actually new or other considerations per your business requirements.

May require edits as this was done from a text editor

0

I got the functionality working by using parameters passed from the calling page as suggested by the folks here and using a controller. Thanks.

enter image description here

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.