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
David OvellaDavid Ovella 

How to get the id from a standard page, and then transfer to a visualforce page

Hello, can someone help me please.
I created a button that goes from a standard page to a visualforce page.
But when I click on it, goes with the id of the field.


User-added image
Best Answer chosen by David Ovella
Jan VandeveldeJan Vandevelde
public class PruebaController {
    Pre_Factura__c prefactura;
    OT__c ot;
    Linea_de_detalle__c lineadedetalle;
   String cuentaId = ApexPages.currentPage().getParameters().get('cuentaId');


    public Pre_Factura__c getPreFactura(){
        if(prefactura == null) prefactura = new Pre_Factura__c();
        prefactura.cuenta__c = cuentaId;
        return prefactura;
    }
    
    public OT__c getOT(){
        if(ot == null) ot = new OT__c();
        return ot;
    }
    
    public Linea_de_detalle__c getLineadedetalle(){
        if(lineadedetalle == null) lineadedetalle = new Linea_de_detalle__c();
        return lineadedetalle;
    }

    public PageReference PPagina1(){
        return Page.Pagina1;
    }
    
    public PageReference PPagina2(){
        return Page.Pagina2;
    }

    public PageReference PPagina3(){
        return Page.Pagina3;
    }         
    
    
    public PageReference cancel() {
        return Page.Pagina1;
    }
  
    public PageReference save() {
        
        insert prefactura;
        
        ot.Pre_Factura__c = prefactura.Id;
        insert ot;
        
        lineadedetalle.OT__c = ot.Id;
        insert lineadedetalle;   
        

        PageReference prefacturaPage = new ApexPages.StandardController(prefactura).view();
        prefacturaPage.setRedirect(true);
        return prefacturaPage;
    }   
}

You are starting of the Cuenta object record and there you have a button. Behind your button choose URL as content source and put in
/apex/Pagina1?cuentaId={!Cuenta.Id}

Cuenta.Id should be the record Id mergefield, when clicking the button you are opening the VF page Pagina1 and you pass the cuenta.id to the controller.
The controller creates a variable type String called cuentaId and sets it to the value received in the URL after ?cuentaid

then when creating the new object pre_factura_c, it immediately sets your lookupfield named cuenta__c to the id recieved and returns the page with the id prepopulated

All Answers

Jan VandeveldeJan Vandevelde
Don't know how your page is called, but I guess you just want to pass in the AccountId
/apex/YourVFPage?acctId={!Account.Id}

and in your Visualforce controller start with taking in the id from the URL like so


String acctId = ApexPages.currentPage().getParameters().get('acctId');  

and the pass it to the field like

Cuenta__c = acctId;
 
Zuinglio Lopes Ribeiro JúniorZuinglio Lopes Ribeiro Júnior
Hello,

If your button call this Visualforce directly and this visualforce has as the StandardController the same object, you can try this way on your extension controller:
public ExtensionController(ApexPages.StandardController stdController) {

     customSObject__c   obj = (  customSObject__c  ) stdController.getRecord();
     
     yourVFObject.customSObjectLookup__c = obj.Id;

}

Regards.

Don't forget to mark your thread as 'SOLVED' with the answer that best helps you.
David OvellaDavid Ovella
thank you for answer, I'm new at this.

the 2 obj have a lookuprela
cuenta__c --> pre_factura__c

my visual force page "Pagina1"


<apex:page controller="PruebaController" tabStyle="Pre_Factura__c">
<script> function confirmCancel() {
var isCancel = confirm("Are you sure you wish to cancel?");
if (isCancel) return true; return false; }
</script>
<apex:sectionHeader title="Crear Pre-Factura" subtitle="Pre-Factura"/>
<apex:form >
<apex:pageBlock mode="edit">
      <apex:pageBlockButtons location="bottom">
              <apex:commandButton action="{!PPagina2}" value="Siguiente"/>
              <apex:commandButton action="{!cancel}" value="Cancelar" onclick="return confirmCancel()" immediate="true"/>
     </apex:pageBlockButtons>
<apex:pageBlockSection title="Información Basica" columns="1">
            <apex:inputField value="{!prefactura.Sucursal__c}"/>
            <apex:inputField value="{!prefactura.Fecha__c}"/>
              <apex:inputField value="{!prefactura.Forma_de_Pago__c}"/>
</apex:pageBlockSection>
 <apex:pageBlockSection title="Cuenta" columns="1">
            <apex:inputField value="{!prefactura.Cuenta__c}"/>
</apex:pageBlockSection> </apex:pageBlock>
</apex:form>
</apex:page>

and my controller
public class PruebaController {
    Pre_Factura__c prefactura;
    OT__c ot;
    Linea_de_detalle__c lineadedetalle;


    public Pre_Factura__c getPreFactura(){
        if(prefactura == null) prefactura = new Pre_Factura__c();
        return prefactura;
    }
    
    public OT__c getOT(){
        if(ot == null) ot = new OT__c();
        return ot;
    }
    
    public Linea_de_detalle__c getLineadedetalle(){
        if(lineadedetalle == null) lineadedetalle = new Linea_de_detalle__c();
        return lineadedetalle;
    }

    public PageReference PPagina1(){
        return Page.Pagina1;
    }
    
    public PageReference PPagina2(){
        return Page.Pagina2;
    }

    public PageReference PPagina3(){
        return Page.Pagina3;
    }         
    
    
    public PageReference cancel() {
        return Page.Pagina1;
    }
  
    public PageReference save() {
        
        insert prefactura;
        
        ot.Pre_Factura__c = prefactura.Id;
        insert ot;
        
        lineadedetalle.OT__c = ot.Id;
        insert lineadedetalle;   
        

        PageReference prefacturaPage = new ApexPages.StandardController(prefactura).view();
        prefacturaPage.setRedirect(true);
        return prefacturaPage;
    }   
}


 
Jan VandeveldeJan Vandevelde
public class PruebaController {
    Pre_Factura__c prefactura;
    OT__c ot;
    Linea_de_detalle__c lineadedetalle;
   String cuentaId = ApexPages.currentPage().getParameters().get('cuentaId');


    public Pre_Factura__c getPreFactura(){
        if(prefactura == null) prefactura = new Pre_Factura__c();
        prefactura.cuenta__c = cuentaId;
        return prefactura;
    }
    
    public OT__c getOT(){
        if(ot == null) ot = new OT__c();
        return ot;
    }
    
    public Linea_de_detalle__c getLineadedetalle(){
        if(lineadedetalle == null) lineadedetalle = new Linea_de_detalle__c();
        return lineadedetalle;
    }

    public PageReference PPagina1(){
        return Page.Pagina1;
    }
    
    public PageReference PPagina2(){
        return Page.Pagina2;
    }

    public PageReference PPagina3(){
        return Page.Pagina3;
    }         
    
    
    public PageReference cancel() {
        return Page.Pagina1;
    }
  
    public PageReference save() {
        
        insert prefactura;
        
        ot.Pre_Factura__c = prefactura.Id;
        insert ot;
        
        lineadedetalle.OT__c = ot.Id;
        insert lineadedetalle;   
        

        PageReference prefacturaPage = new ApexPages.StandardController(prefactura).view();
        prefacturaPage.setRedirect(true);
        return prefacturaPage;
    }   
}

You are starting of the Cuenta object record and there you have a button. Behind your button choose URL as content source and put in
/apex/Pagina1?cuentaId={!Cuenta.Id}

Cuenta.Id should be the record Id mergefield, when clicking the button you are opening the VF page Pagina1 and you pass the cuenta.id to the controller.
The controller creates a variable type String called cuentaId and sets it to the value received in the URL after ?cuentaid

then when creating the new object pre_factura_c, it immediately sets your lookupfield named cuenta__c to the id recieved and returns the page with the id prepopulated
This was selected as the best answer
David OvellaDavid Ovella
thank you so much, Jan Vandevelde
it works perfectly
Thanks
David
Jan VandeveldeJan Vandevelde
Glad to have helped! It wasn't easy with those spanish words in the code (confusing) but I guessed you would put the pieces I intended to explain together ;-) and you did!
Thumbs up!
David OvellaDavid Ovella
can I bother you with another question?
I have the 3 pages, "Pagina1" "Pagina2" "Pagina3"

"Pagina1" is about Pre_Factura
"Pagina2" is about OT
"Pagina3"  is about Linea_de_detalle

​do you know if I can create more than one OT in this procces?

for example:
I'm in the page "Pagina1", and I finished to create the "Pre_Factura", so i clicked on the a button "next" that goes to the next page "Pagina2".
In the "Pagina2" i created the OT, but how can I create more then one?

I create a button add, and it works
I did this:

public class PruebaController {
    Pre_Factura__c prefactura;
    OT__c ot;
    OT__c ot2;
    Linea_de_detalle__c lineadedetalle;

    
    public OT__c getOT(){
        if(ot == null) ot = new OT__c();
        return ot;
    }

    public OT__c getOT2(){
        if(ot == null) ot2 = new OT__c();
        return ot2;
    }


    public PageReference PPagina1(){
        return Page.Pagina1;
    }
    
    public PageReference PPagina2(){
        return Page.Pagina2;
    }

    public PageReference PPagina3(){
        return Page.Pagina3;
    }         
    
    
    public PageReference cancel() {
        return Page.Pagina1;
    }
  
    public PageReference save() {
        
        insert prefactura;
        
        ot.Pre_Factura__c = prefactura.Id;
        insert ot;

        ot2.Pre_Factura__c = prefactura.Id;
        insert ot2;

        
        lineadedetalle.OT__c = ot.Id;
        insert lineadedetalle;   
    }   
}

It works, but only when I create two OT, when I create one OT doesn't work. By any chance, do you know a way that it will work?

 
Jan VandeveldeJan Vandevelde
Yes, your button calls the save method which will always create 2 OT from your code. If I get it right, the quantity of OT you will be creating can differ everytime. One time it will be only one OT, another it could be 2,3,4, 10 OT.

I would suggest you create 2 buttons: 1 is your save button and 1 "Save & New"
for the save button remove the OT2 code, for the save & new button call an action that only creates a new OT and you'll have to work with a list because you don't know how many records will be created when starting the page.