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
PrazPraz 

redirect to object layout from visualforce page

Hi I am trying to redirect from a page to object layout.

 

This is what I am trying but is not working any help will be much appreciated

 

 

public PageReference value(){
    Account accnt = new Account();

    PageReference acctPage = new ApexPages.StandardController(accnt).view();
      acctPage.setRedirect(true);

      return acctPage;

}

 

 

Best Answer chosen by Admin (Salesforce Developers) 
sinsiterrulezsinsiterrulez

The prefix will be same if you are planning to move it from Full sandbox to production.

Or else use the following code to dynamically get the ID prefix:

 

Schema.DescribeSObjectResult r = <Object_Api>.sObjectType.getDescribe();
string IDPrefix = r.getKeyPrefix();

 

in object_Api add the api name of object

 

All Answers

sinsiterrulezsinsiterrulez

Hi,

In your code the accnt is a new account...You need to associate it to some existing account record to goto View page as you can only view existing records!!!!

PrazPraz

I want user to create a new account upon directed..so how can I do that?

sinsiterrulezsinsiterrulez

You can use the following code:

 

public PageReference value(){
    Account accnt = new Account();

    PageReference acctPage = new Pagereference('001/e');
      acctPage.setRedirect(true);
      return acctPage;
}

 

 

PrazPraz

Hi it woked with this..

 

 

public PageReference value(){
    Account accnt = new Account();

    PageReference acctPage = new Pagereference('/001/e');
      acctPage.setRedirect(true);
      return acctPage;
}

 

but in other function I want to transfer it to a custom object  for creating a row of record...when I replaced Account with Custom Object..it redirected me to Account only

 

 

 

sinsiterrulezsinsiterrulez

In the code for pagereference add ID prefix istead of 001 (*which infact is ID prefix for account)..

 

public PageReference value(){
    Account accnt = new Account();

    PageReference acctPage = new Pagereference('/<ID_Prefix>/e');
      acctPage.setRedirect(true);
      return acctPage;
}

 You can get the ID prefix for your custom object from apex explorer or you can also use the first 3 digits of ID of any record of your custom object e.g. All account records have ID starting with 001

 

Hope this solve's your problem

 

PrazPraz

If we move from one SFDC environment to other will it remain the same? the ID prefix...?

sinsiterrulezsinsiterrulez

The prefix will be same if you are planning to move it from Full sandbox to production.

Or else use the following code to dynamically get the ID prefix:

 

Schema.DescribeSObjectResult r = <Object_Api>.sObjectType.getDescribe();
string IDPrefix = r.getKeyPrefix();

 

in object_Api add the api name of object

 

This was selected as the best answer
PrazPraz

Thanks a lot :)