0

So this is my controller which create new event and save event. in this i have hardcoded page redirect link '/a3I/o'. i dnt want this to hardcoded. I want to do something like it will redirect to that tab without harcoded code or this will redirect to new record page which i am creating in this code(t1).

 public class ERFormController
{
    public Event_Request__c t1{get;set;}

    public ERFormController(ApexPages.StandardController controller)
    {
        t1 = new Event_Request__c();
    }

    public PageReference save()
    {
        insert t1; 
        ApexPages.AddMessage(new ApexPages.Message(ApexPages.Severity.CONFIRM,'Record Created Successfully.Thank you!'));
        PageReference pg = new PageReference('/a3I/o');
        pg.setRedirect(true);
        return pg;  
    }

2 Answers 2

4

Pass the newly generated id PageReference('/'+t1.Id);. it will redirect to newly created record detail page.

public class ERFormController
{
    public Event_Request__c t1{get;set;}

    public ERFormController(ApexPages.StandardController controller)
    {
        t1 = new Event_Request__c();
    }

    public PageReference save()
    {
        insert t1; 
        ApexPages.AddMessage(new ApexPages.Message(ApexPages.Severity.CONFIRM,'Record Created Successfully.Thank you!'));
        PageReference pg = new PageReference('/'+t1.Id);
        pg.setRedirect(true);
        return pg;  
    }
3
  • won't this redirect them to the record rather than the tab? Commented Jan 12, 2016 at 17:48
  • ('/'+t1.Id); In this Id means which Id u mean? Commented Jan 12, 2016 at 17:49
  • @puravthakkar here Id means newly created one.. it will redirect to newly created record detail page.
    – Ratan Paul
    Commented Jan 12, 2016 at 17:50
3

If you want to redirect to the tab in a more programmatic way, which is what you request in your OP, then you can at least use the key prefix:

String url = String.format('/{0}/o', new List<String> {
    SObjectType.Event_Request__c.getKeyPrefix()
});
return new PageReference(url);

If you want to redirect to the record, it is a bit cleaner to use StandardController instead of string concatenation.

return new ApexPages.StandardController(newRecord).view();

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .