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
Lakshmi SLakshmi S 

How to redirect the visualforce page to record detail page after clicking on Cancel Button ?

Hi Team,
How to redirect the visualforce page to record detail page after clicking on Cancel Button ?

Controller Class
-------------------------
public with sharing class ControllerClass {
    
    public static String oppid;
    
    private ApexPages.StandardController sc{get;set;}
    public Opportunity opp = new Opportunity();
    public ControllerClass(ApexPages.StandardController sc){
        this.sc = sc;
        oppid = ApexPages.currentPage().getParameters().get('oppid');
        //system.debug('------'+oppid);
        opp =[select id,name,Account.name from Opportunity where id =:oppid limit 1];
 
    }
    }
    
    Public PageReference Cancel(){
        PageReference pr = new PageReference('/'+Schema.SObjectType.Opportunity.getKeyPrefix()+'/o');
        return pr;
        }

}
How to redirect this page to opportunity record detail page.
Please let me know any one.

Regrds
Lakshmi
 
Best Answer chosen by Lakshmi S
Yogeshwar TailorYogeshwar Tailor
Hi Lakshmi,

Please try the below code.
public with sharing class ControllerClass {
    
    public static String oppid;
    
    private ApexPages.StandardController sc{get;set;}
    public Opportunity opp;
    
    public ControllerClass(ApexPages.StandardController sc){
        opp = new Opportunity();
        this.sc = sc;
        oppid = ApexPages.currentPage().getParameters().get('oppid');
        opp =[select id,name,Account.name from Opportunity where id =:oppid limit 1];
    }
    
    
    Public PageReference Cancel(){
        system.debug('------'+opp.id);
        PageReference pr = new PageReference('/'+opp.id);
        pr.setRedirect(true);
        return pr;
    }

}

Thanks,
Yogesh

 

All Answers

Yogeshwar TailorYogeshwar Tailor
Hi Lakshmi,

Please try the below code.
public with sharing class ControllerClass {
    
    public static String oppid;
    
    private ApexPages.StandardController sc{get;set;}
    public Opportunity opp;
    
    public ControllerClass(ApexPages.StandardController sc){
        opp = new Opportunity();
        this.sc = sc;
        oppid = ApexPages.currentPage().getParameters().get('oppid');
        opp =[select id,name,Account.name from Opportunity where id =:oppid limit 1];
    }
    
    
    Public PageReference Cancel(){
        system.debug('------'+opp.id);
        PageReference pr = new PageReference('/'+opp.id);
        pr.setRedirect(true);
        return pr;
    }

}

Thanks,
Yogesh

 
This was selected as the best answer
Lakshmi SLakshmi S
Hi Yogesh,

Thanks for your quick response.

Regards
Lakshmi.