You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Apex获取当前查看账户的联系人邮箱列表失败求助

Troubleshooting: Can't Get Account ID via ApexPages.CurrentPage.getParameters().get('id')

Hey there! As a fellow Apex beginner, I totally get how frustrating this can be—let's walk through the most common reasons this happens and fix your issue so you can pull those contact emails successfully.

1. You're not using the Standard Controller (or using it incorrectly)

If your Visualforce page is supposed to work with an Account record, you need to bind it to the Account standard controller in your page markup. This tells Salesforce to pass the current Account's ID into the page parameters automatically.

Make sure your page starts with:

<apex:page standardController="Account" extensions="YourControllerName">

Without this, the id parameter won't be present in the page context when you navigate to the page from an Account detail page.

2. You're using the wrong parameter case

Salesforce uses lowercase 'id' for the record ID parameter. Double-check that you're not using uppercase like 'ID' or 'Id'—those won't match the parameter name.

Bad:

Id accountId = ApexPages.CurrentPage.getParameters().get('Id'); // No match!

Good:

Id accountId = ApexPages.CurrentPage.getParameters().get('id'); // Correct case

3. A more reliable way: Use the Standard Controller's getId() method

Instead of pulling the ID from page parameters directly, use the standard controller's built-in method—it's designed for exactly this scenario and avoids parameter-related issues.

Here's an example of how to implement this in your controller:

public class AccountContactEmailExtension {
    private Id currentAccountId;

    // Constructor that accepts the standard controller
    public AccountContactEmailExtension(ApexPages.StandardController stdController) {
        // Get the ID directly from the standard controller
        currentAccountId = stdController.getId();
    }

    // Method to return related contact emails as a List
    public List<String> getRelatedContactEmails() {
        List<String> contactEmails = new List<String>();
        
        // Query contacts linked to the current account
        List<Contact> relatedContacts = [
            SELECT Email 
            FROM Contact 
            WHERE AccountId = :currentAccountId 
            AND Email != NULL
        ];

        // Populate the list with emails
        for(Contact c : relatedContacts) {
            contactEmails.add(c.Email);
        }

        return contactEmails;
    }
}

4. Verify the page is receiving the id parameter

If you're still stuck, add a debug statement to check what parameters are actually being passed to the page. This will help you confirm if the id parameter exists at all:

System.debug('All page parameters: ' + ApexPages.CurrentPage.getParameters());

Run your code, check the debug logs, and see if 'id' shows up in the parameter map. If it doesn't, you're probably navigating to the page without passing the ID (e.g., typing the URL manually instead of clicking from an Account record).

Quick recap to fix your issue:

  • Ensure your Visualforce page uses the Account standard controller
  • Use stdController.getId() instead of pulling from page parameters (more reliable)
  • Double-check the parameter name is lowercase 'id' if you do use the parameter method
  • Confirm the page is being accessed from an Account record (so the ID is passed automatically)

Once you fix these, you should be able to pull the related contact emails without hardcoding a fixed ID!

内容的提问来源于stack exchange,提问作者to_the_nth

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 08:58:47