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
Sylvie SerpletSylvie Serplet 

Pass Opportunity recordId on a Lighting Component to a VF page that open with ui: button - press

Hi,
I have a Lightning Component on the Opportunity detail page that display button when press it opens a VF page.
The VF page is a form to complete to create a record on a custom object Fact_Finder__c which has a master-detail relationship with Opportunity.
I need to pre-populate the Opportunity Name on the VF page from the Opportunity recordId on the Lightning Component and when the Submit button is selected I need to go back to the Opportunity detail page.
I have looked intensively on forums and documentation but could not figure it how to do it.

My current working codes are as following. I have simplified it and did not put much style. The original code display many buttons on the Lightning component and my VF page (form) has a lot of input fields.
My component which capture the Opportunity recordId:
<aura:component implements="flexipage:availableForAllPageTypes,force:hasRecordId" access="global">    
   <aura:attribute name="FFform" type="String" default="Fact Finder Form" access="global" />	
   <aura:attribute name="recordId" type="Id" />  
    
<div class="FFbuttons"> 
    <div aria-labelledby="FFformB">            
         <fieldset class="slds-box slds-theme--default slds-container--fluid">       
          <legend id="FFform" class="slds-text-heading--small">Fact Finder Forms</legend> 
             
     Opportunity id {!v.recordId}
             
             <form class="slds-form--inline">    
            <div class="slds-form-element">
          <ui:button label="Residential Landlord" 
                          press="{!c.openResidentialform}" />
 </div>    
</form>         
  </fieldset>        
  </div>
 </div>
</aura:component>
Client side Controller:
({
	openResidentialform: function(component, event, helper) {
	var urlEvent = $A.get("e.force:navigateToURL");   
    urlEvent.setParams({
      "url": "/apex/ResidentialForm"
    });
    urlEvent.fire();	
	},
VF page:
apex:page standardcontroller="Fact_Finder__c" extensions="ResidentialFormCtrExt" showHeader="false" sidebar="false" >
 
<style type="text/css">
 .buttonsubmit {
  width: 200px;
  font-size: 150% !important;
  background:#E9EEF2 !important;
  }
.buttoncancel {
  width: 200px;
  font-size: 120% !important;
  background:#E9EEF2 !important;
  } 
</style> 
 <apex:form style="width:800px; text-align:center; margin:auto;" enctype="multipart/form-data" > 
  <apex:pageMessages />
   <apex:pageBlock title="Residential / Landlords Fact Finder" >        
     <apex:pageBlockButtons location="bottom" > 
             <apex:commandButton action="{!saveFF}" value="Submit"  styleClass="buttonsubmit" /> 
             <apex:commandButton action="{!cancel}" value="Cancel" styleClass="buttoncancel" />
                </apex:pageBlockButtons> 

 <apex:outputPanel layout="block">
              <apex:pageblocksection columns="1">
                    <apex:inputfield value="{!FF.Opportunity_Name__c}"/>            
                    <apex:outputfield value="{!FF.Client_Name__c}"/>
                    <apex:inputfield value="{!FF.Date__c}"/>
     </apex:pageblocksection>  
</apex:outputPanel>   
 </apex:pageBlock>
  </apex:form>         
</apex:page>
Controller Server Side:
public with sharing class ResidentialFormCtrExt
{   
 @AuraEnabled

 public Fact_Finder__c FF {get; set;}   
Public ResidentialFormCtrExt(ApexPages.StandardController controller)
  {              
       this.FF=(Fact_Finder__c)controller.getRecord();
      
       //FF.Opportunity_Name__c= oppName;  

public PageReference cancel(){
   
    return null;
    //new PageReference('/' + oppName);  

    }

  public PageReference saveFF() {
                  
    insert (this.FF);
    
    return null;
    //new PageReference('/' + oppName);
      }      
  }

The oppName is what I need to capture (based on the Opportunity recordId) to pre-populate my form.
Any idea?
Thank you in advance for your help.
Sylvie

 
Best Answer chosen by Sylvie Serplet
Sylvie SerpletSylvie Serplet
Hi,
For those who face the same problem, this is how I make it works:

Application
<aura:application >
  <ltng:require styles="{!$Resource.SLDS202 +
  '/assets/styles/salesforce-lightning-design-system-ltng.css'}"/>    
<div class="FFbuttons"> 
  <c:FactFinderFormscomp /> 
</div>     
</aura:application>
Component
<aura:component implements="flexipage:availableForAllPageTypes,force:hasRecordId" controller="ResidentialFormCtrExt" access="global">       
   <aura:attribute name="FFform" type="String" default="Fact Finder Form" access="global" />	
   <aura:attribute name="recordId" type="String" />         
<div class="FFbuttons"> 
    <div aria-labelledby="FFformB">            
         <fieldset class="slds-box slds-theme--default slds-container--fluid">       
          <legend id="FFform" class="slds-text-heading--small">Fact Finder Forms</legend>         
              <form class="slds-form--inline">    
            <div class="slds-form-element">
          <ui:button label="Residential Landlord" 
                     class="button"
                     press="{!c.openResidentialform}" />
          </div>     
</form>         
  </fieldset>        
  </div>
 </div>
</aura:component>
Controller
({    
    openResidentialform: function(component, event, helper) {
    var urlEvent = $A.get("e.force:navigateToURL");
    var recordId = component.get("v.recordId");    
        urlEvent.setParams({
        "url": "/apex/ResidentialForm?recordId="+recordId
        });
    urlEvent.fire();
	}
})
APEX
public with sharing class ResidentialFormCtrExt
{   
 @AuraEnabled
 public static Opportunity getOpportunity (Id recordId) {
        return [
            SELECT Id, Name 
            FROM Opportunity
            WHERE Id =:recordId
        ][0];
    } 
 public Fact_Finder__c FF {get; set;}        
 public User selectedUser {get; set;}
       
 public ResidentialFormCtrExt(ApexPages.StandardController controller)
  {              
       String recordId = ApexPages.currentPage().getParameters().get('recordId');         
       this.FF=(Fact_Finder__c)controller.getRecord();   
       FF.Opportunity_Name__c= recordId;  
}
VF Page
<apex:page standardcontroller="Fact_Finder__c" extensions="ResidentialFormCtrExt" showHeader="false" sidebar="false" >
//some style
<apex:form style="width:800px; text-align:center; margin:auto;" enctype="multipart/form-data" > 
  <apex:pageMessages />
   <apex:pageBlock title="Residential / Landlords Fact Finder" >        
 <apex:outputPanel styleClass="black" layout="block">
              <apex:pageblocksection columns="1">
                    <apex:inputfield value="{!FF.Opportunity_Name__c}"/>            
                    <apex:outputfield value="{!FF.Client_Name__c}"/>
                    <apex:inputfield value="{!FF.Date__c}"/>
     </apex:pageblocksection>  
</apex:outputPanel> 
 </apex:pageBlock>
  </apex:form>         
</apex:page>

Sylvie

All Answers

Balasubramaniam 07Balasubramaniam 07
Hi 

Try this,
​var id = '[RecordID]';
        urlEvent.setParams({
            "url": "/apex/PageName?id="+id
        });
        urlEvent.fire();

Thanks,
Bala
Sylvie SerpletSylvie Serplet
Hi Bala,
I have amended the Controller like that:
({
	openResidentialform: function(component, event, helper) {
    var urlEvent = $A.get("e.force:navigateToURL");
    var id = '[RecordID]'; 
      urlEvent.setParams({
      "url": "/apex/ResidentialForm?id="+id
    });
    urlEvent.fire();
	},

And add the following to my apex class:
public with sharing class ResidentialFormCtrExt
{   
 @AuraEnabled

 public Fact_Finder__c FF {get; set;}        
 public User selectedUser {get; set;}
    
 String oppName=System.currentPageReference().getParameters().get('Id');
                   
 public ResidentialFormCtrExt(ApexPages.StandardController controller)
  {              
       this.FF=(Fact_Finder__c)controller.getRecord();   
               
       FF.Opportunity_Name__c= oppName;  
 }      
  }

And I get the following message:
 
Id value is not valid for the Fact_Finder__c standard controller

Any other thought?
Thanks,
Sylvie
Balasubramaniam 07Balasubramaniam 07
Hi Sylvie,

You should replace '[RecordId]' with correct record id value. I just mentioned it for your understanding.

Please change it like this.
​var id = component.get("v.recordId");
        urlEvent.setParams({
            "url": "/apex/PageName?id="+id
        });
        urlEvent.fire();
Thanks,
Bala
Sylvie SerpletSylvie Serplet
Hi Bala,

Now I got this message:
Id value 006280000075PPT is not valid for the Fact_Finder__c standard controller
The problem is probably on the Apex page. Could you please have a look above and tell me if I called the id correctly?

Thanks,
Sylvie
Sylvie SerpletSylvie Serplet
Hi,
For those who face the same problem, this is how I make it works:

Application
<aura:application >
  <ltng:require styles="{!$Resource.SLDS202 +
  '/assets/styles/salesforce-lightning-design-system-ltng.css'}"/>    
<div class="FFbuttons"> 
  <c:FactFinderFormscomp /> 
</div>     
</aura:application>
Component
<aura:component implements="flexipage:availableForAllPageTypes,force:hasRecordId" controller="ResidentialFormCtrExt" access="global">       
   <aura:attribute name="FFform" type="String" default="Fact Finder Form" access="global" />	
   <aura:attribute name="recordId" type="String" />         
<div class="FFbuttons"> 
    <div aria-labelledby="FFformB">            
         <fieldset class="slds-box slds-theme--default slds-container--fluid">       
          <legend id="FFform" class="slds-text-heading--small">Fact Finder Forms</legend>         
              <form class="slds-form--inline">    
            <div class="slds-form-element">
          <ui:button label="Residential Landlord" 
                     class="button"
                     press="{!c.openResidentialform}" />
          </div>     
</form>         
  </fieldset>        
  </div>
 </div>
</aura:component>
Controller
({    
    openResidentialform: function(component, event, helper) {
    var urlEvent = $A.get("e.force:navigateToURL");
    var recordId = component.get("v.recordId");    
        urlEvent.setParams({
        "url": "/apex/ResidentialForm?recordId="+recordId
        });
    urlEvent.fire();
	}
})
APEX
public with sharing class ResidentialFormCtrExt
{   
 @AuraEnabled
 public static Opportunity getOpportunity (Id recordId) {
        return [
            SELECT Id, Name 
            FROM Opportunity
            WHERE Id =:recordId
        ][0];
    } 
 public Fact_Finder__c FF {get; set;}        
 public User selectedUser {get; set;}
       
 public ResidentialFormCtrExt(ApexPages.StandardController controller)
  {              
       String recordId = ApexPages.currentPage().getParameters().get('recordId');         
       this.FF=(Fact_Finder__c)controller.getRecord();   
       FF.Opportunity_Name__c= recordId;  
}
VF Page
<apex:page standardcontroller="Fact_Finder__c" extensions="ResidentialFormCtrExt" showHeader="false" sidebar="false" >
//some style
<apex:form style="width:800px; text-align:center; margin:auto;" enctype="multipart/form-data" > 
  <apex:pageMessages />
   <apex:pageBlock title="Residential / Landlords Fact Finder" >        
 <apex:outputPanel styleClass="black" layout="block">
              <apex:pageblocksection columns="1">
                    <apex:inputfield value="{!FF.Opportunity_Name__c}"/>            
                    <apex:outputfield value="{!FF.Client_Name__c}"/>
                    <apex:inputfield value="{!FF.Date__c}"/>
     </apex:pageblocksection>  
</apex:outputPanel> 
 </apex:pageBlock>
  </apex:form>         
</apex:page>

Sylvie
This was selected as the best answer
Shubham Bansal 45Shubham Bansal 45
Hi Sylvie, I am facing the same issue I have followed your approach but my record id appears to be null.