0

Objective: To display the URL of VF page as a link on the record detail page and when clicked on that, the VF page should open in a separate tab by taking the record values, such as name dynamically.

What I have achieved till now:

VF Page:

<apex:page standardController="Project__c" extensions="myExtension">
    <apex:form >
        <html>
           Name of the record: {!currentRecord.Name}
        </html>
    </apex:form>
</apex:page>

Controller:

public class myExtension {

        public project__c currentRecord{get; set;}
        
        public myExtension(ApexPages.StandardController controller) {
            currentRecord = [SELECT Id, Name FROM project__c  WHERE Id = :ApexPages.currentPage().getParameters().get('id')];
        }
    
    }

And I have created a formula field that displays the URL of this VF page on the record detail page. It is as follows:

HYPERLINK("/apex/Project?recid="&CASESAFEID(Id) , "View Chart", "_blank")

But, when I click on the URL, I am getting the following error:

List has no rows for assignment to SObject An unexpected error has occurred. Your development organization has been notified.

Can anyone please let me know how to achieve this scenario? Thank you advance

2 Answers 2

1

You used the wrong parameter name (recId where it should be id), but you actually shouldn't use page parameters at all here. The StandardController class has a getId() method, which is always how I recommend you get the Id if you are still using Visualforce.

SEELECT ... FROM Project__c WHERE Id = :controller.getId()

Note also that a Standard Controller should get the record and its fields for you, no need for a custom extension just to query the record itself. Instead of {!currentRecord.Name}, just use {!Name}, and you can get rid of your extension altogether.

2
  • Even if i remove extension and pass {!Project__c.Name} as value, I am seeing blank output. The output is as follows: Name of the record: Instead of name, i am seeing blank there. please suggest
    – SFDCUser
    May 24, 2021 at 14:02
  • Change recid to id in your url. You should use the correct parameter names.
    – Adrian Larson
    May 24, 2021 at 14:05
1

When using extensions, you must use the literal parameter id:

HYPERLINK("/apex/Project?id="&Id , "View Chart", "_blank")

Also, instead of a URL like this, you can use $Page instead. This would be future compatible with any changes that Salesforce may make to their URL scheme.

URLFOR($Page.Project, Id, null, true)

In addition, you do not need a query, just get the record from the controller:

    public myExtension(ApexPages.StandardController controller) {
        currentRecord = (Project__c)controller.getRecord();
    }

You must log in to answer this question.

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