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
kishna 15kishna 15 

How to redirect record detail page in lightning using apex

How to redirect account record detail page using below code
public PageReference save(string id) {
        System.debug('Entered');
        PageReference nextPage = new PageReference('/'+id+'/view');
        nextPage.setRedirect(true);
        return nextPage;
}

Thanks.........
Khan AnasKhan Anas (Salesforce Developers) 
Hi Kishna,

Greetings to you!

I tried to research your problem and found some solution which might help you. 

If you want to use your page in lightning only then you can use:
//Some Logic
INSERT acc;

PageReference pageRef = new PageReference('/' + acc.id);
aura.redirect(pageRef);
return pageRef;

However, if you want to use your page in both classic and lightning then you can use getUiThemeDisplayed ()
 
String uiThemeDisplayed = UserInfo.getUiThemeDisplayed();

These are the values:
Theme1—Obsolete Salesforce theme
Theme2—Salesforce Classic 2005 user interface theme
Theme3—Salesforce Classic 2010 user interface theme
Theme4d—Modern “Lightning Experience” Salesforce theme
Theme4t—Salesforce mobile app theme
Theme4u—Lightning Console theme
PortalDefault—Salesforce Customer Portal theme
Webstore—Salesforce AppExchange theme

So, you have to use if condition like this:
 
//Some Logic
INSERT acc;

String uiThemeDisplayed = UserInfo.getUiThemeDisplayed();
PageReference pageRef = new PageReference('/' + acc.Id);

if(uiThemeDisplayed=='Theme4d'){  // Lightning Theme Value          
       aura.redirect(pageRef);
}

if(uiThemeDisplayed=='Theme2' || uiThemeDisplayed=='Theme3'){  // Classic Theme Value
       pageRef.setRedirect(true);
}
return pageRef;

I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.

Thanks and Regards,
Khan Anas
SandhyaSandhya (Salesforce Developers) 
Hi,

You can try below url
'lightning/r/My_Custom_SObject__c/' + customRecord.Id + '/view'


Best Regards,
Sandhya
kishna 15kishna 15
Hi Khan 
Thanks for quick response I am getting below error 
An internal server error has occurred
An error has occurred while processing your request. The salesforce.com support team has been notified of the problem. If you believe you have additional information that may be of help in reproducing or correcting the error, please contact Salesforce Support. Please indicate the URL of the page you were requesting, any error id shown on this page as well as any other related information. We apologize for the inconvenience. 

Thank you again for your patience and assistance. And thanks for using salesforce.com! 
kishna 15kishna 15
Hi Sandya,
Thanks for quick response
I have tried below condition it is not working for me
'lightning/r/My_Custom_SObject__c/' + customRecord.Id + '/view'
Khan AnasKhan Anas (Salesforce Developers) 
Hi Kishna,

Actually, this is a known issue. Visualforce Page reference id not working on Lightning.

Please refer to the below link for more information.
https://success.salesforce.com/issues_view?id=a1p3A0000001BrUQAU&title=visualforce-page-reference-id-not-working-on-lightning

And there is no workaround available. But it is in a review and we may get a solution for this soon.

I hope it helps you.

Kindly mark this as solved if the information was helpful.

Thanks and Regards,
Khan Anas
kishna 15kishna 15
Hi Khan

I am able to collect current record id from URL but same URL id not able pass in lightning

public class contactQuery {
    public list<Contact> contacts{get;set;}
    public contactQuery(ApexPages.StandardController controller){
        id accid=ApexPages.currentPage().getParameters().get('id');
        contacts=[select id,firstname,lastname,phone from contact where accountid=:accid];
    save(accid);
    }
    public PageReference save(string id) {
        System.debug('Entered');
        PageReference pg = new PageReference('/'+id+'/view');
        pg.setRedirect(true);
        return pg;
}
}