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 have to query a prospectaccount without opportunity and task for last two years

I queried Tasks
SELECT accountid FROM Task WHERE What.Type = 'Account 'AND CreatedDate = LAST_N_MONTHS:24
I queried Account and Opportunity
SELECT Id, Name FROM account WHERE Rating = 'Prospect 'AND Id NOT IN (SELECT accountid FROM Opportunity WHERE CreatedDate = LAST_N_MONTHS:24) 
how to connect this withmy Account and opportunity.

Thanksin Advance
Best Answer chosen by suji srinivasan
TUSHAR_MATHURTUSHAR_MATHUR
Hi @suji,

You can store the all accountid's in a set collection and then run query on account excluding the ids which are in the set.
Set<Account> accIds = new Set<Account>();
for(Task thisTsk : [SELECT accountid FROM Task WHERE What.Type = 'Account 'AND CreatedDate = LAST_N_MONTHS:24]){
accIds.add(thisTsk.accountId);
}
for(Opportunity thisOpp : [(SELECT accountid FROM Opportunity WHERE CreatedDate = LAST_N_MONTHS:24]){
accIds.add(thisOpp.accountId);
}
List<Account> accList = [SELECT Id, Name FROM account WHERE Rating = 'Prospect 'AND Id NOT IN: accIds];

This list will have your values.

Thanks and Regards,
Tushar Mathur

 

All Answers

TUSHAR_MATHURTUSHAR_MATHUR
Hi @suji,

You can store the all accountid's in a set collection and then run query on account excluding the ids which are in the set.
Set<Account> accIds = new Set<Account>();
for(Task thisTsk : [SELECT accountid FROM Task WHERE What.Type = 'Account 'AND CreatedDate = LAST_N_MONTHS:24]){

}
TUSHAR_MATHURTUSHAR_MATHUR
Hi @suji,

You can store the all accountid's in a set collection and then run query on account excluding the ids which are in the set.
Set<Account> accIds = new Set<Account>();
for(Task thisTsk : [SELECT accountid FROM Task WHERE What.Type = 'Account 'AND CreatedDate = LAST_N_MONTHS:24]){
accIds.add(thisTsk.accountId);
}
for(Opportunity thisOpp : [(SELECT accountid FROM Opportunity WHERE CreatedDate = LAST_N_MONTHS:24]){
accIds.add(thisOpp.accountId);
}
List<Account> accList = [SELECT Id, Name FROM account WHERE Rating = 'Prospect 'AND Id NOT IN: accIds];

This list will have your values.

Thanks and Regards,
Tushar Mathur

 
This was selected as the best answer