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
Sree_NaniSree_Nani 

How to get current created Id & pass it to vfpage

Hi developers,

 

                     I am asking small doubt which i have a problem. I want to pass an Id to URL. 

Your question is Which Id you need to pass?

 

I want to create a new record from vf page of any object as a standard controller & I want to get that newly created record Id. That Id i want to pass to URL. 

 

 

How can I do this ?

Best Answer chosen by Admin (Salesforce Developers) 
Navatar_DbSupNavatar_DbSup

Hi,

 

Try the below code snippet.

 

<apex:page standardController="Contact" extensions="cls_standrdcontDemo" >

  <apex:form >

  Contact Lastname

  <apex:inputField value="{!con.LastName}"/>

  <apex:commandButton value="Save" action="{!save1}" />

  </apex:form>

</apex:page>

 

==========Apex Controller==========

 

public class cls_standrdcontDemo {

public Contact con{get;set;}

 

    public cls_standrdcontDemo(ApexPages.StandardController controller)

    {

        con= (Contact)controller.getRecord();

    }

    public PageReference save1()

    {

        insert con;

        PageReference pageRef = page.cmdlinkDemo;

        pageRef.getParameters().put('id ',con.id);

        pageRef.setRedirect(true);

        return pageRef; 

    }

   

}

 

Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved. 

 

 

All Answers

Navatar_DbSupNavatar_DbSup

Hi,

 

Try the below code snippet.

 

<apex:page standardController="Contact" extensions="cls_standrdcontDemo" >

  <apex:form >

  Contact Lastname

  <apex:inputField value="{!con.LastName}"/>

  <apex:commandButton value="Save" action="{!save1}" />

  </apex:form>

</apex:page>

 

==========Apex Controller==========

 

public class cls_standrdcontDemo {

public Contact con{get;set;}

 

    public cls_standrdcontDemo(ApexPages.StandardController controller)

    {

        con= (Contact)controller.getRecord();

    }

    public PageReference save1()

    {

        insert con;

        PageReference pageRef = page.cmdlinkDemo;

        pageRef.getParameters().put('id ',con.id);

        pageRef.setRedirect(true);

        return pageRef; 

    }

   

}

 

Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved. 

 

 

This was selected as the best answer
Sree_NaniSree_Nani

Hi Navatar_DbSup,

 

                   Thanks & great  Your code is absolutely correct. I need your explanation of few statements.

 

In constructor you have written one statement of type casting, what it does please?

 

my point of understanding is your getting record id which is created recently. but, without query you are fetching from the page. Is it correct?

 

You have not used map collection, but you have used put()?

 

After insert statement, you have used two statements page creation from controller & comparison of id's with this.

 

You have used setRedirect(true)?

I want to redirect it to detail page of a record, what i need to put in the place of true.

 

Navatar_DbSupNavatar_DbSup

Hi,

 

I have make use of typecast because "getRecord" is a method of StandardController class and it return sobject. For more detail follow the below link:

http://www.salesforce.com/us/developer/docs/pages/Content/apex_pages_standardcontroller.htm

 

Also if you want to redirect to detail page you can try the below code snippet

 

public class cls_standrdcontDemo {

public Contact con{get;set;}

 

    public cls_standrdcontDemo(ApexPages.StandardController controller)

    {

        con= (Contact)controller.getRecord();

    }

    public PageReference save1()

    {

        insert con;

        PageReference pageRef = new PageReference('/'+con.id);

     //   pageRef.getParameters().put('id ',con.id);

        pageRef.setRedirect(true);

        return pageRef; 

    }

   

 

}

Sree_NaniSree_Nani

Hi Ankit, 

 

                 I got an issue with the code, can please solve this one. As param passing to URL 

https://c.ap1.visual.force.com/apex/cmdlinkdemo?id+=0039000000C52YZAAZ

 

I don't need " + " sign after id

 

 

Sree_NaniSree_Nani

Hi Ankit,

 

                  You are completely taking me into another direction with second snippet code. I need "Id" in the URL without "+" sign after Id. your updated URL is helpful of information. Thanks for it