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
suji srinivasansuji srinivasan 

Hi, I want to write soql to get account without opportunity ,tasks for last two years with rating = prospect related contacts

select id, Name, (Select Id, Name from contacts), (Select Id, Name From Opportunities where Id='Your Opportunity Id') from Account

Thanks in advance
Best Answer chosen by suji srinivasan
mukesh guptamukesh gupta
Hi Suji,

Please follow below code:-
 
Set<Id> accIds = new Set<Id>();
List<Account> accList = [select id, Name, (Select Id, Name from contacts), (Select Id, Name From Opportunities where CreatedDate < LAST_N_YEARS:2) from Account Where rating = 'prospect'];

for(Account acc : accList){
   accIds.add(acc);
}

List<Task> tskList  = [SELECT id,whatId, subject FROM Task WHERE whatId IN: accIds];

if you need any assistanse, Please let me know!!

Kindly mark my solution as the best answer if it helps you.

Thanks
Mukesh

All Answers

Sudhanshu Pandey 15Sudhanshu Pandey 15
can you please describe more about it
suji srinivasansuji srinivasan
soql for batch apex required.
conditions
1.account rating = prospect
2.account without opportunities last 2 years
3.account  without tasks last 2 years get this related account details and contact details.
mukesh guptamukesh gupta
Hi Suji,

Please follow below code:-
 
Set<Id> accIds = new Set<Id>();
List<Account> accList = [select id, Name, (Select Id, Name from contacts), (Select Id, Name From Opportunities where CreatedDate < LAST_N_YEARS:2) from Account Where rating = 'prospect'];

for(Account acc : accList){
   accIds.add(acc);
}

List<Task> tskList  = [SELECT id,whatId, subject FROM Task WHERE whatId IN: accIds];

if you need any assistanse, Please let me know!!

Kindly mark my solution as the best answer if it helps you.

Thanks
Mukesh
This was selected as the best answer