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
DavyDavy 

Pass Id from VF Page to Apex on page load

Hello,

I created a VF Page to create new leads, the new lead must be related to an existing Campaign. On the VF Page I want to show the Campaign description. I created a VF Page:
 
<apex:page showheader="false" sidebar="false" standardStylesheets="false" controller="CreateLead" tabStyle="Lead">
  <apex:form>
				  <apex:outputText value="{!Campaign.Description}" escape="false" />
						<apex:pageMessages id="msgs"/>
						<apex:inputfield value="{!Lead.LastName}" html-placeholder="Achternaam" styleClass="store w-full border bg-gray-100 hover:bg-gray-200 focus:bg-gray-200 outline-none px-3 py-2"/>
						<apex:outputtext value=""></apex:outputtext>
						...
						<apex:inputfield value="{!Lead.Email}" html-placeholder="Email adres" styleClass="store w-full border bg-gray-100 hover:bg-gray-200 focus:bg-gray-200 outline-none px-3 py-2"/>
						<apex:outputtext value=""></apex:outputtext>
      <apex:commandButton rerender="msgs" action="{! save }" value="Save"/>
  </apex:form>
</apex:page>
And an Apex page to get the Campaign description and to save the Lead and set a relation with the Campaign:
 
public class CreateLead {


    public Lead getLead() {
        return lead;
    }
    
				public Lead lead {get;set;}

				public CreateLead (){
								lead = new Lead();
				}
    
    public PageReference save() {
	    insert lead;

        CampaignMember member = new CampaignMember(
            LeadId = lead.Id,
            CampaignId = '7011r00000'
        );
        insert member; 

        return null;
    }

    private Campaign campaign;

   	public Campaign getCampaign() {
        
        if (campaign == null){                     
	        campaign = [select Name, Description from Campaign where
            id = '7011r00000'];       
        }
        return campaign;
    }


}



This works fine, however I hardcoded the Campaign Id in the Apex class. I want to be able to reuse the Apex class so I want to make the Campaign Id variable. 

How can I pass a Campaign Id from the VF Page to the Apex class when the page is loaded. I found solutions with an additional form on the VF Page that can be used to set the Campaign Id. I would like to define it in the VF Page and make it available in the Apex class when a user loads the page.

Thank you for your help!
Best Answer chosen by Davy
Devi ChandrikaDevi Chandrika (Salesforce Developers) 
Hi Davy,
try this code.Kindly modify according to your requirements
<apex:page showheader="false" sidebar="false" standardStylesheets="false" controller="CreateLead" tabStyle="Lead">
  <apex:form>
				  <apex:outputText value="{!Campaign.Description}" escape="false" />
						<apex:pageMessages id="msgs"/>
						<apex:inputfield value="{!Lead.LastName}" html-placeholder="Achternaam" styleClass="store w-full border bg-gray-100 hover:bg-gray-200 focus:bg-gray-200 outline-none px-3 py-2"/>
						<apex:outputtext value=""></apex:outputtext>
						...
						<apex:inputfield value="{!Lead.Email}" html-placeholder="Email adres" styleClass="store w-full border bg-gray-100 hover:bg-gray-200 focus:bg-gray-200 outline-none px-3 py-2"/>
						<apex:outputtext value=""></apex:outputtext>
      <apex:inputfield value="{!Lead.company}" html-placeholder="Company name" styleClass="store w-full border bg-gray-100 hover:bg-gray-200 focus:bg-gray-200 outline-none px-3 py-2"/>
						<apex:outputtext value=""></apex:outputtext>
      <apex:commandButton rerender="msgs" action="{! save }" value="Save"/>
  </apex:form>
</apex:page>

Controller
public class CreateLead {
    public id campaignId;
    
    public Lead getLead() {
        return lead;
    }
    
    public Lead lead {get;set;}
    
    public CreateLead (){
        campaignId = ApexPages.currentPage().getParameters().get('id');
        lead = new Lead();
    }
    
    public PageReference save() {
        insert lead;
        
        CampaignMember member = new CampaignMember(
            LeadId = lead.Id,
            CampaignId = campaign.id
        );
        insert member; 
        
        return null;
    }
    
    private Campaign campaign;
    
    public Campaign getCampaign() {
        
        if (campaign == null){                     
            campaign = [select Name, Description from Campaign where
                        id = :campaignId];       
        }
        return campaign;
    }
    
    
}

After previewing your vf page
Pass id of the campaign you want to relate like this
https://yourInstanceName/apex/pagename?id​​​​​​​=givecampaignidhere

​​​​​​​Hope this helps you
Let me know if this helps you. Kindly mark it as solved so that it may help others in future.

Thanks and Regards

All Answers

jordy molierejordy moliere
Hello Davy,

if I understand well, you want to retrieve tje Id from the campaign. Why do you not use thr standard controller of the campaign and then retrieve the id on the controller 
public controller(ApexPages.StandardController controller)
  {
   
   Id = stdController.getRecord().id;
   System.debug('***id***'+id);
  }
And then on the visualforce page, you add the standard controller for the campaign and you can add it on campaign page layout.
Devi ChandrikaDevi Chandrika (Salesforce Developers) 
Hi Davy,
try this code.Kindly modify according to your requirements
<apex:page showheader="false" sidebar="false" standardStylesheets="false" controller="CreateLead" tabStyle="Lead">
  <apex:form>
				  <apex:outputText value="{!Campaign.Description}" escape="false" />
						<apex:pageMessages id="msgs"/>
						<apex:inputfield value="{!Lead.LastName}" html-placeholder="Achternaam" styleClass="store w-full border bg-gray-100 hover:bg-gray-200 focus:bg-gray-200 outline-none px-3 py-2"/>
						<apex:outputtext value=""></apex:outputtext>
						...
						<apex:inputfield value="{!Lead.Email}" html-placeholder="Email adres" styleClass="store w-full border bg-gray-100 hover:bg-gray-200 focus:bg-gray-200 outline-none px-3 py-2"/>
						<apex:outputtext value=""></apex:outputtext>
      <apex:inputfield value="{!Lead.company}" html-placeholder="Company name" styleClass="store w-full border bg-gray-100 hover:bg-gray-200 focus:bg-gray-200 outline-none px-3 py-2"/>
						<apex:outputtext value=""></apex:outputtext>
      <apex:commandButton rerender="msgs" action="{! save }" value="Save"/>
  </apex:form>
</apex:page>

Controller
public class CreateLead {
    public id campaignId;
    
    public Lead getLead() {
        return lead;
    }
    
    public Lead lead {get;set;}
    
    public CreateLead (){
        campaignId = ApexPages.currentPage().getParameters().get('id');
        lead = new Lead();
    }
    
    public PageReference save() {
        insert lead;
        
        CampaignMember member = new CampaignMember(
            LeadId = lead.Id,
            CampaignId = campaign.id
        );
        insert member; 
        
        return null;
    }
    
    private Campaign campaign;
    
    public Campaign getCampaign() {
        
        if (campaign == null){                     
            campaign = [select Name, Description from Campaign where
                        id = :campaignId];       
        }
        return campaign;
    }
    
    
}

After previewing your vf page
Pass id of the campaign you want to relate like this
https://yourInstanceName/apex/pagename?id​​​​​​​=givecampaignidhere

​​​​​​​Hope this helps you
Let me know if this helps you. Kindly mark it as solved so that it may help others in future.

Thanks and Regards
This was selected as the best answer
DavyDavy
Thank you both very much for the quick reply!

Passing the Id as a query parameter works perfect, I didn't realize it is so simple :) The only downside it the changing URLs, I would like to give our volunteers never changing links and manage the Campaign Ids on my end by updating the VF pages. I have a workaround by loading the pages in an iframe, but managing this inside the VF Page would be better. 

Also, is there a way to deny requests without a valid Id?

Thanks again for your help!

 
Diego Gonzalez CurrivilDiego Gonzalez Currivil
I am new to apex, Like the question above, I need to get the business number. For now I am inserting the Conflict of Interest record.
Business is related to the object Conflict of Interest.
--
Class Apex.
public class ConflictoInteres {
    
    public Conflicto_de_Intereses__c accObj{GET;SET;}
    
    public ConflictoInteres(ApexPages.StandardController Controller){
        accObj = new Conflicto_de_Intereses__c();
    }
       
    public PageReference createAccRec(){
        insert accObj;
        return new PageReference('/'+ accObj.Id);
    }
}
--
Visual Force.
<apex:page standardController="Conflicto_de_Intereses__c" extensions="ConflictoInteres" lightningStylesheets="True">
    <apex:form >
        
    <apex:pageBlock title="Formulario Conflicto de Intereses">
        
        <apex:pageBlockSection title="Digitar Negocio" columns="1">
                <apex:inputField value="{!accObj.Negocio__c}"/>    <----- THIS IS WHERE I NEED YOU TO UPLOAD THE BUSINESS THAT IS ALREADY REFERENCED.
        </apex:pageBlockSection>
        <apex:pageBlockSection title="Question 1"  columns="1">
            
            <apex:inputField value="{!accObj.Respuesta1No__c}"/>
            
            <apex:inputField Label="Si su respuesta es Si complete los siguientes datos:" value="{!accObj.Respuesta1Si__c}"/>
            
            <apex:inputField Label="A. question 44:" value="{!accObj.Respuesta_1__c}"/>
            <apex:inputField Label="B. question 55:" value="{!accObj.Respuesta_2__c}"/>
            <apex:inputField Label="C. question66:" value="{!accObj.Respuesta_3__c}"/>
        </apex:pageBlockSection>
             
        <apex:pageBlockButtons >
            <apex:commandButton action="{!createAccRec }" value="Guardar Declaración"></apex:commandButton>
        </apex:pageBlockButtons>
        
    </apex:pageBlock>   
    </apex:form>
</apex:page>
--