Retrieve current record id in salesforce

To get the current record ID in Salesforce Visualforce, you can use the Id parameter in the URL or leverage standard controllers. Here are two common methods to accomplish this:

Using the Id Parameter in the URL:

In Visualforce, you can access the current record's ID using the Id parameter in the URL. This method is commonly used when creating custom pages or links.

<apex:page standardController="Account">

    <h1> Account Record ID: {!$CurrentPage.parameters.Id}</h1>

</apex:page>

In this example, {!$CurrentPage.parameters.Id} retrieves the ID from the URL.

Using Standard Controllers:

If you're using a standard controller in your Visualforce page, you can directly reference the record ID using the Id property of the standard controller.

<apex:page standardController="Account">
    <h1>Record ID: {! Account.Id}</h1>
</apex:page>

In this example, {! Account.Id} retrieves the ID of the current record from the standard controller.

Controller

ApexPages.CurrentPage().getparameters().get('id')

public AccountIdController(ApexPages.StandardController controller) {

        acc = [select id ,name, AccountNumber, Type, Industry from Account where id =:     ApexPages.CurrentPage().getparameters().get('id') ];

    }

}

Above methods will provide you with the ID of the current record being viewed or edited in Salesforce Visualforce pages. Choose the method that best fits your use case and application architecture.

Comments

Popular posts from this blog

Introduction to Salesforce Agent Script: Build Predictable AI Agents

Anti-Patterns in Salesforce Agentforce: What to Avoid When Building AI Agents

MCP vs. Traditional APIs: Choosing the Right Integration Approach