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
Srinivas223Srinivas223 

Opportunities with approval history related list status approved

Hello All,
Is there anyway that I can get the list of opportunity records with the approval history related list status approved.

writing a inner query like below is not working. It is pulling all opportunities. Can I filter opportunities based on the apporval history fields?
select id, name, account.name, CloseDate, 
             (Select Id, StepStatus From ProcessSteps where StepStatus = 'Approved'),
             from Opportunity where ownerid =: OpptyUser.id and stageName = 'closed won'
I appreciate your help.
Thank you.
Best Answer chosen by Srinivas223
Om PrakashOm Prakash
Hi Srinivas,
This code wil work for your  use case. Let me know if any query.
List<ProcessInstance> lstProcessInstance = [SELECT TargetObjectId, CompletedDate FROM ProcessInstance where TargetObject.Type ='Opportunity' and Status='Approved' Limit 10000];
List<String> lstOppId = new List<String>();
for(ProcessInstance objProcess : lstProcessInstance){
	lstOppId.add(objProcess.TargetObjectId);
}
List<Opportunity> lstOpp = [Select id, name, account.name, CloseDate from Opportunity where id in : lstOppId  and stageName = 'closed won' limit 5000];  // and ownerid =: OpptyUser.id
System.debug('-List of Opportunity -'+lstOpp);


 

All Answers

Om PrakashOm Prakash
Hi Srinivas,
This code wil work for your  use case. Let me know if any query.
List<ProcessInstance> lstProcessInstance = [SELECT TargetObjectId, CompletedDate FROM ProcessInstance where TargetObject.Type ='Opportunity' and Status='Approved' Limit 10000];
List<String> lstOppId = new List<String>();
for(ProcessInstance objProcess : lstProcessInstance){
	lstOppId.add(objProcess.TargetObjectId);
}
List<Opportunity> lstOpp = [Select id, name, account.name, CloseDate from Opportunity where id in : lstOppId  and stageName = 'closed won' limit 5000];  // and ownerid =: OpptyUser.id
System.debug('-List of Opportunity -'+lstOpp);


 
This was selected as the best answer
Srinivas223Srinivas223
Thank you Prakash,
Will try this one.