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

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? – Phil Hawthorn Jan 12 '16 at 17:48
  • ('/'+t1.Id); In this Id means which Id u mean? – Salesfor_dev Jan 12 '16 at 17:49
  • @puravthakkar here Id means newly created one.. it will redirect to newly created record detail page. – Ratan Paul Jan 12 '16 at 17:50
2

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();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.