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
Gokul Pillai 1Gokul Pillai 1 

SOQL for fetching contact roles and fields from Contacts through opportunity

I have a requirement wherein I have to create batch class that picks all the contacts which has “Eligible” checkbox true. Batch execution should perform below operation :
•All the opportunities which has these contacts as a Ship To contact roles , update its stage as “Qualified”.
but I am unable to fetch the contact roles from the opportunity
Best Answer chosen by Gokul Pillai 1
AbhishekAbhishek (Salesforce Developers) 
Try this,

Select (Select OpportunityId From OpportunityContactRoles) From Opportunity where id in (Select OpportunityId From OpportunityContactRole where ContactId != '')

I hope you find the above information is helpful. If it does, please mark as Best Answer to help others too.

Thanks.

All Answers

mukesh guptamukesh gupta
Hi Gokul,

you can use below SOQL to get contact roles from the opportunity.
Select (Select OpportunityId From OpportunityContactRoles) From Opportunity

If this solution is usefull for you, Please mark as a Best Answer to help others.


Regards
Mukesh

 
AbhishekAbhishek (Salesforce Developers) 
Try this,

Select (Select OpportunityId From OpportunityContactRoles) From Opportunity where id in (Select OpportunityId From OpportunityContactRole where ContactId != '')

I hope you find the above information is helpful. If it does, please mark as Best Answer to help others too.

Thanks.
This was selected as the best answer
Gokul Pillai 1Gokul Pillai 1
Thanks for the help Abhishek & Mukesh. I actually tried Abhishek's query and made some tweaks with it as per the requirement and it worked fine
Final Query was: 
Select (Select OpportunityId From OpportunityContactRoles) From Opportunity where id in (Select OpportunityId From OpportunityContactRole WHERE Role = \'Ship To\' AND Contact.Eligible__c=TRUE)