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
Sujit KarandeSujit Karande 

Fetch related list records associated with Opportunity

I have related list called "Approvers" in opportunity object.

And I created few dummy records in approvers.

I am trying to fetch all those related list record associated with opportunity in apex trigger. As I wannted to assign values to the picklist field in realted list depend on Oppotunity pickist field value

For eg : Select all approvers where opportunity id="some-id";

Any suggestion would be really appreciated.
Rahul KumarRahul Kumar (Salesforce Developers) 
Hi Sujit,

May I suggest you please refer the below link for reference. hope it helps.

Please mark it as best answer if the information is informative.so that question is removed from an unanswered question and appear as a proper solution.

Thanks
Rahul Kumar
Shiva RajendranShiva Rajendran
Hi Sujit,
I assume you have a lookup to opportunity from approver object

Trigger OppTrigger  on Opportunity(after Update)
{
   List<Approver__c> approvers=[select id,opportunityId from approver__c where id in:Trigger.new limit 50000];
Map<Id,List<Approver__c>>  allApproverForOpportunity=new Map<Id,List<Approver__c>>();
For(Approver__c opp : approvers)
{
     if(allApproverForOpportunity.isEmpty()==false)
{
     List<Approver__c> listOfApprover;
         if(allApproverForOpportunity.containsKey(opp.opportunityId))
          listOfApprover=allApproverForOpportunity.get(opp.opportunityId);
    else
listOfApprover=new List<Approver__c>(opp);
allApproverForOpportunity.put(opp.opportunityId,listOfApprover);

}
else
{
List<Approver__c> listOfApprover=new List<Approver__c>(opp);
allApproverForOpportunity.put(opp.opportunityId,listOfApprover);
}
}

}

// the map allApproverForOpportunity will contain all the opportunity id and its related list of approver
Let me know if you need further help

Thanks and Regards,
Shiva RV
Manoj Maraka 18Manoj Maraka 18
Hi Sujit,
Use a sub query as shown below. Replace Approvers__r with relavant child relationship name.
[Select Id, (Select Id from Approvers__r) from Id IN : Trigger.New]

Mark it as best answer if this is what you are looking for.