如何获取Opportunity对象中Approvers相关列表的所有关联记录?
Hey there! Let's walk through how to fetch all Approvers records linked to a specific Opportunity in Salesforce. I'll cover different scenarios depending on where you need to implement this.
First, the foundation is a SOQL statement tailored to your custom objects. Assuming your Approvers custom object has an API name of Approver__c, and the lookup/master-detail field linking to Opportunity is Opportunity__c, here's the query you need:
SELECT Id, Name, [Add other fields you need here] FROM Approver__c WHERE Opportunity__c = '006XXXXXXXXXXXXXXX' -- Replace with your target Opportunity ID
Note: Swap
Approver__candOpportunity__cwith the actual API names of your custom object and relationship field if they differ.
1. In Apex Code (Triggers/Classes)
If you're working server-side in Apex, use a bound variable to avoid SOQL injection and improve performance:
// Define your target Opportunity ID String targetOpportunityId = '006XXXXXXXXXXXXXXX'; // Fetch the linked Approvers List<Approver__c> relatedApprovers = [ SELECT Id, Name, Approver_Email__c, Approval_Date__c // Include only fields you need FROM Approver__c WHERE Opportunity__c = :targetOpportunityId ]; // Example: Iterate through the results for(Approver__c approver : relatedApprovers) { System.debug('Approver: ' + approver.Name + ' | Email: ' + approver.Approver_Email__c); }
2. In Lightning Web Components (LWC)
For front-end use, pair an Apex controller with LWC's wire service. Here's a quick example:
LWC JavaScript
import { LightningElement, wire, api } from 'lwc'; import getApproversForOpportunity from '@salesforce/apex/ApproverController.getApproversForOpportunity'; export default class OpportunityApproverList extends LightningElement { @api recordId; // Auto-populated if the component is placed on an Opportunity record page // Wire the approvers data using the current Opportunity's ID @wire(getApproversForOpportunity, { oppId: '$recordId' }) approvers; }
Corresponding Apex Controller
public with sharing class ApproverController { @AuraEnabled(cacheable=true) public static List<Approver__c> getApproversForOpportunity(String oppId) { // Guard clause for invalid input if(String.isBlank(oppId)) { return new List<Approver__c>(); } return [ SELECT Id, Name, Approver_Email__c FROM Approver__c WHERE Opportunity__c = :oppId ]; } }
You can then render the data in your LWC template using for:each to loop through approvers.data.
3. Quick Testing in Developer Console
If you just want to verify the data, open the Developer Console > Query Editor, paste your SOQL statement, and click "Execute" to see the results instantly.
- Permissions: Make sure your profile has Read access to the
Approver__cobject and all fields you're querying. - Relationship Type: If using a Master-Detail relationship, Approvers will be deleted automatically if the parent Opportunity is deleted. For Lookup relationships, you might want to add
WHERE Opportunity__c != nullto filter out orphaned records. - Field Selection: Always query only the fields you need—this improves performance and avoids hitting governor limits unnecessarily.
内容的提问来源于stack exchange,提问作者Amit

