Help with Query Illegal Assignment from a List to a List
Clash Royale CLAN TAG#URR8PPP
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;
up vote
2
down vote
favorite
I have a custom object Relationship_Owner__C with a lookup relationship to Contacts. I need to query all the contacts that belong to the relationship owner, and all the tasks assigned to the contact in order to get a count of unique subject lines.
When I tried to break it into two queries I got a SOQL 101 error. But now I seem to be stuck on making it work at all. Any help would be great.
trigger CountUniqueEmails on relationship_owner__c (before insert, before update) {
LIST<Task> taskList =[SELECT ID,
(SELECT Id,
Subject
FROM Tasks)
FROM Contact
WHERE New_Relationship_owner1__c IN :Trigger.New];
system.debug('Influencers found =:' + taskList.size());
//If the list size is greater than 0 get the tasks assigned to the influencers
If(taskList.size() !=0){
String subject = 'none';
Integer uniqueCount = 0;
New Code
trigger CountUniqueEmails on relationship_owner__c (after insert, after update)
Map contactMap = new Map([SELECT Id, New_Relationship_owner1__c
FROM Contact
WHERE New_Relationship_owner1__c IN :Trigger.newMap.keySet()]);
List taskList = [SELECT Id, WhoId,Subject
FROM Task
WHERE WhoId IN :contactMap.keySet()];
Map> subjectLineMap = new Map>();
for(Task t: taskList)
Id relationshipOwnerId = contactMap.get(t.WhoId).New_Relationship_owner1__c;
if(!subjectLIneMap.containsKey(relationshipOwnerId))
subjectLineMap.put(relationshipOwnerId, new Set<String>());
subjectLineMap.get(relationshipOwnerId).add(t.Subject);
system.debug('unique subject count=' + subjectLineMap.size());
trigger soql learning
add a comment |Â
up vote
2
down vote
favorite
I have a custom object Relationship_Owner__C with a lookup relationship to Contacts. I need to query all the contacts that belong to the relationship owner, and all the tasks assigned to the contact in order to get a count of unique subject lines.
When I tried to break it into two queries I got a SOQL 101 error. But now I seem to be stuck on making it work at all. Any help would be great.
trigger CountUniqueEmails on relationship_owner__c (before insert, before update) {
LIST<Task> taskList =[SELECT ID,
(SELECT Id,
Subject
FROM Tasks)
FROM Contact
WHERE New_Relationship_owner1__c IN :Trigger.New];
system.debug('Influencers found =:' + taskList.size());
//If the list size is greater than 0 get the tasks assigned to the influencers
If(taskList.size() !=0){
String subject = 'none';
Integer uniqueCount = 0;
New Code
trigger CountUniqueEmails on relationship_owner__c (after insert, after update)
Map contactMap = new Map([SELECT Id, New_Relationship_owner1__c
FROM Contact
WHERE New_Relationship_owner1__c IN :Trigger.newMap.keySet()]);
List taskList = [SELECT Id, WhoId,Subject
FROM Task
WHERE WhoId IN :contactMap.keySet()];
Map> subjectLineMap = new Map>();
for(Task t: taskList)
Id relationshipOwnerId = contactMap.get(t.WhoId).New_Relationship_owner1__c;
if(!subjectLIneMap.containsKey(relationshipOwnerId))
subjectLineMap.put(relationshipOwnerId, new Set<String>());
subjectLineMap.get(relationshipOwnerId).add(t.Subject);
system.debug('unique subject count=' + subjectLineMap.size());
trigger soql learning
add a comment |Â
up vote
2
down vote
favorite
up vote
2
down vote
favorite
I have a custom object Relationship_Owner__C with a lookup relationship to Contacts. I need to query all the contacts that belong to the relationship owner, and all the tasks assigned to the contact in order to get a count of unique subject lines.
When I tried to break it into two queries I got a SOQL 101 error. But now I seem to be stuck on making it work at all. Any help would be great.
trigger CountUniqueEmails on relationship_owner__c (before insert, before update) {
LIST<Task> taskList =[SELECT ID,
(SELECT Id,
Subject
FROM Tasks)
FROM Contact
WHERE New_Relationship_owner1__c IN :Trigger.New];
system.debug('Influencers found =:' + taskList.size());
//If the list size is greater than 0 get the tasks assigned to the influencers
If(taskList.size() !=0){
String subject = 'none';
Integer uniqueCount = 0;
New Code
trigger CountUniqueEmails on relationship_owner__c (after insert, after update)
Map contactMap = new Map([SELECT Id, New_Relationship_owner1__c
FROM Contact
WHERE New_Relationship_owner1__c IN :Trigger.newMap.keySet()]);
List taskList = [SELECT Id, WhoId,Subject
FROM Task
WHERE WhoId IN :contactMap.keySet()];
Map> subjectLineMap = new Map>();
for(Task t: taskList)
Id relationshipOwnerId = contactMap.get(t.WhoId).New_Relationship_owner1__c;
if(!subjectLIneMap.containsKey(relationshipOwnerId))
subjectLineMap.put(relationshipOwnerId, new Set<String>());
subjectLineMap.get(relationshipOwnerId).add(t.Subject);
system.debug('unique subject count=' + subjectLineMap.size());
trigger soql learning
I have a custom object Relationship_Owner__C with a lookup relationship to Contacts. I need to query all the contacts that belong to the relationship owner, and all the tasks assigned to the contact in order to get a count of unique subject lines.
When I tried to break it into two queries I got a SOQL 101 error. But now I seem to be stuck on making it work at all. Any help would be great.
trigger CountUniqueEmails on relationship_owner__c (before insert, before update) {
LIST<Task> taskList =[SELECT ID,
(SELECT Id,
Subject
FROM Tasks)
FROM Contact
WHERE New_Relationship_owner1__c IN :Trigger.New];
system.debug('Influencers found =:' + taskList.size());
//If the list size is greater than 0 get the tasks assigned to the influencers
If(taskList.size() !=0){
String subject = 'none';
Integer uniqueCount = 0;
New Code
trigger CountUniqueEmails on relationship_owner__c (after insert, after update)
Map contactMap = new Map([SELECT Id, New_Relationship_owner1__c
FROM Contact
WHERE New_Relationship_owner1__c IN :Trigger.newMap.keySet()]);
List taskList = [SELECT Id, WhoId,Subject
FROM Task
WHERE WhoId IN :contactMap.keySet()];
Map> subjectLineMap = new Map>();
for(Task t: taskList)
Id relationshipOwnerId = contactMap.get(t.WhoId).New_Relationship_owner1__c;
if(!subjectLIneMap.containsKey(relationshipOwnerId))
subjectLineMap.put(relationshipOwnerId, new Set<String>());
subjectLineMap.get(relationshipOwnerId).add(t.Subject);
system.debug('unique subject count=' + subjectLineMap.size());
trigger soql learning
trigger soql learning
edited Aug 16 at 21:10
asked Aug 16 at 19:34
Brooks Johnson
717
717
add a comment |Â
add a comment |Â
2 Answers
2
active
oldest
votes
up vote
6
down vote
accepted
Direct Answer
Your query's result type will be a List<sObject>
, where sObject
is the primary object in the query. In this case,
SELECT ID, (SELECT Id, Subject FROM Tasks) FROM Contact...
the primary object is Contact
. For that reason, the return value will be List<Contact>
.
Note also that you cannot use Trigger.new
as a bind variable for an Id query. You can use Trigger.newMap.keySet()
, because it's the right data type:
SELECT ID, (SELECT Id, Subject FROM Tasks) FROM Contact WHERE New_Relationship_owner1__c IN :Trigger.newMap.keySet()
Another Approach
If what you want, though, is the unique subject lines of Tasks attached to Contacts which have specific relationship owners, but you do not need them grouped by the individual Contact, you can break the query into two to go straight to the Task level. It would look something like this:
Map<Id, Contact> contactMap = new Map<Id, Contact>([SELECT Id FROM Contact WHERE New_Relationship_owner1__c IN :Trigger.newMap.keySet()]);
List<Task> taskList = [SELECT Id, WhoId, Subject FROM Task WHERE WhoId IN :contactMap.keySet()];
Map<Id, Set> subjectLineMap = new Map<Id, Set>();
Then you can iterate over those Tasks:
for (Task t: taskList)
Id relationshipOwnerId = contactMap.get(t.WhoId).New_Relationship_owner1__c;
if (!subjectLineMap.containsKey(relationshipOwnerId))
subjectLineMap.put(relationshipOwnerId, new Set<String>());
subjectLineMap.get(relationshipOwnerId).add(t.Subject);
Then your subjectLineMap
ends up with one Set of the unique Subject lines, across all contacts, for each Relationship Owner. You can inspect the size of the set for an owner's Id to see how many unique subject lines there were (note that it's case sensitive).
add a comment |Â
up vote
2
down vote
you are querying FROM Contact so the list must be of type list'(Contact)'..
if you want to check to see if any tasks are related to a contact you need to iterate over the list of contacts and see if tasks exists.
for( Contact con : [SELECT ID,
(SELECT Id,
Subject
FROM Tasks)
FROM Contact
WHERE New_Relationship_owner1__c IN :Trigger.New])
if ( con.tasks.isEmpty() )
// no tasks assigned to contact
else
// tasks exists for this contact
add a comment |Â
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
6
down vote
accepted
Direct Answer
Your query's result type will be a List<sObject>
, where sObject
is the primary object in the query. In this case,
SELECT ID, (SELECT Id, Subject FROM Tasks) FROM Contact...
the primary object is Contact
. For that reason, the return value will be List<Contact>
.
Note also that you cannot use Trigger.new
as a bind variable for an Id query. You can use Trigger.newMap.keySet()
, because it's the right data type:
SELECT ID, (SELECT Id, Subject FROM Tasks) FROM Contact WHERE New_Relationship_owner1__c IN :Trigger.newMap.keySet()
Another Approach
If what you want, though, is the unique subject lines of Tasks attached to Contacts which have specific relationship owners, but you do not need them grouped by the individual Contact, you can break the query into two to go straight to the Task level. It would look something like this:
Map<Id, Contact> contactMap = new Map<Id, Contact>([SELECT Id FROM Contact WHERE New_Relationship_owner1__c IN :Trigger.newMap.keySet()]);
List<Task> taskList = [SELECT Id, WhoId, Subject FROM Task WHERE WhoId IN :contactMap.keySet()];
Map<Id, Set> subjectLineMap = new Map<Id, Set>();
Then you can iterate over those Tasks:
for (Task t: taskList)
Id relationshipOwnerId = contactMap.get(t.WhoId).New_Relationship_owner1__c;
if (!subjectLineMap.containsKey(relationshipOwnerId))
subjectLineMap.put(relationshipOwnerId, new Set<String>());
subjectLineMap.get(relationshipOwnerId).add(t.Subject);
Then your subjectLineMap
ends up with one Set of the unique Subject lines, across all contacts, for each Relationship Owner. You can inspect the size of the set for an owner's Id to see how many unique subject lines there were (note that it's case sensitive).
add a comment |Â
up vote
6
down vote
accepted
Direct Answer
Your query's result type will be a List<sObject>
, where sObject
is the primary object in the query. In this case,
SELECT ID, (SELECT Id, Subject FROM Tasks) FROM Contact...
the primary object is Contact
. For that reason, the return value will be List<Contact>
.
Note also that you cannot use Trigger.new
as a bind variable for an Id query. You can use Trigger.newMap.keySet()
, because it's the right data type:
SELECT ID, (SELECT Id, Subject FROM Tasks) FROM Contact WHERE New_Relationship_owner1__c IN :Trigger.newMap.keySet()
Another Approach
If what you want, though, is the unique subject lines of Tasks attached to Contacts which have specific relationship owners, but you do not need them grouped by the individual Contact, you can break the query into two to go straight to the Task level. It would look something like this:
Map<Id, Contact> contactMap = new Map<Id, Contact>([SELECT Id FROM Contact WHERE New_Relationship_owner1__c IN :Trigger.newMap.keySet()]);
List<Task> taskList = [SELECT Id, WhoId, Subject FROM Task WHERE WhoId IN :contactMap.keySet()];
Map<Id, Set> subjectLineMap = new Map<Id, Set>();
Then you can iterate over those Tasks:
for (Task t: taskList)
Id relationshipOwnerId = contactMap.get(t.WhoId).New_Relationship_owner1__c;
if (!subjectLineMap.containsKey(relationshipOwnerId))
subjectLineMap.put(relationshipOwnerId, new Set<String>());
subjectLineMap.get(relationshipOwnerId).add(t.Subject);
Then your subjectLineMap
ends up with one Set of the unique Subject lines, across all contacts, for each Relationship Owner. You can inspect the size of the set for an owner's Id to see how many unique subject lines there were (note that it's case sensitive).
add a comment |Â
up vote
6
down vote
accepted
up vote
6
down vote
accepted
Direct Answer
Your query's result type will be a List<sObject>
, where sObject
is the primary object in the query. In this case,
SELECT ID, (SELECT Id, Subject FROM Tasks) FROM Contact...
the primary object is Contact
. For that reason, the return value will be List<Contact>
.
Note also that you cannot use Trigger.new
as a bind variable for an Id query. You can use Trigger.newMap.keySet()
, because it's the right data type:
SELECT ID, (SELECT Id, Subject FROM Tasks) FROM Contact WHERE New_Relationship_owner1__c IN :Trigger.newMap.keySet()
Another Approach
If what you want, though, is the unique subject lines of Tasks attached to Contacts which have specific relationship owners, but you do not need them grouped by the individual Contact, you can break the query into two to go straight to the Task level. It would look something like this:
Map<Id, Contact> contactMap = new Map<Id, Contact>([SELECT Id FROM Contact WHERE New_Relationship_owner1__c IN :Trigger.newMap.keySet()]);
List<Task> taskList = [SELECT Id, WhoId, Subject FROM Task WHERE WhoId IN :contactMap.keySet()];
Map<Id, Set> subjectLineMap = new Map<Id, Set>();
Then you can iterate over those Tasks:
for (Task t: taskList)
Id relationshipOwnerId = contactMap.get(t.WhoId).New_Relationship_owner1__c;
if (!subjectLineMap.containsKey(relationshipOwnerId))
subjectLineMap.put(relationshipOwnerId, new Set<String>());
subjectLineMap.get(relationshipOwnerId).add(t.Subject);
Then your subjectLineMap
ends up with one Set of the unique Subject lines, across all contacts, for each Relationship Owner. You can inspect the size of the set for an owner's Id to see how many unique subject lines there were (note that it's case sensitive).
Direct Answer
Your query's result type will be a List<sObject>
, where sObject
is the primary object in the query. In this case,
SELECT ID, (SELECT Id, Subject FROM Tasks) FROM Contact...
the primary object is Contact
. For that reason, the return value will be List<Contact>
.
Note also that you cannot use Trigger.new
as a bind variable for an Id query. You can use Trigger.newMap.keySet()
, because it's the right data type:
SELECT ID, (SELECT Id, Subject FROM Tasks) FROM Contact WHERE New_Relationship_owner1__c IN :Trigger.newMap.keySet()
Another Approach
If what you want, though, is the unique subject lines of Tasks attached to Contacts which have specific relationship owners, but you do not need them grouped by the individual Contact, you can break the query into two to go straight to the Task level. It would look something like this:
Map<Id, Contact> contactMap = new Map<Id, Contact>([SELECT Id FROM Contact WHERE New_Relationship_owner1__c IN :Trigger.newMap.keySet()]);
List<Task> taskList = [SELECT Id, WhoId, Subject FROM Task WHERE WhoId IN :contactMap.keySet()];
Map<Id, Set> subjectLineMap = new Map<Id, Set>();
Then you can iterate over those Tasks:
for (Task t: taskList)
Id relationshipOwnerId = contactMap.get(t.WhoId).New_Relationship_owner1__c;
if (!subjectLineMap.containsKey(relationshipOwnerId))
subjectLineMap.put(relationshipOwnerId, new Set<String>());
subjectLineMap.get(relationshipOwnerId).add(t.Subject);
Then your subjectLineMap
ends up with one Set of the unique Subject lines, across all contacts, for each Relationship Owner. You can inspect the size of the set for an owner's Id to see how many unique subject lines there were (note that it's case sensitive).
edited Aug 16 at 21:08
answered Aug 16 at 20:07
David Reed
20.3k31640
20.3k31640
add a comment |Â
add a comment |Â
up vote
2
down vote
you are querying FROM Contact so the list must be of type list'(Contact)'..
if you want to check to see if any tasks are related to a contact you need to iterate over the list of contacts and see if tasks exists.
for( Contact con : [SELECT ID,
(SELECT Id,
Subject
FROM Tasks)
FROM Contact
WHERE New_Relationship_owner1__c IN :Trigger.New])
if ( con.tasks.isEmpty() )
// no tasks assigned to contact
else
// tasks exists for this contact
add a comment |Â
up vote
2
down vote
you are querying FROM Contact so the list must be of type list'(Contact)'..
if you want to check to see if any tasks are related to a contact you need to iterate over the list of contacts and see if tasks exists.
for( Contact con : [SELECT ID,
(SELECT Id,
Subject
FROM Tasks)
FROM Contact
WHERE New_Relationship_owner1__c IN :Trigger.New])
if ( con.tasks.isEmpty() )
// no tasks assigned to contact
else
// tasks exists for this contact
add a comment |Â
up vote
2
down vote
up vote
2
down vote
you are querying FROM Contact so the list must be of type list'(Contact)'..
if you want to check to see if any tasks are related to a contact you need to iterate over the list of contacts and see if tasks exists.
for( Contact con : [SELECT ID,
(SELECT Id,
Subject
FROM Tasks)
FROM Contact
WHERE New_Relationship_owner1__c IN :Trigger.New])
if ( con.tasks.isEmpty() )
// no tasks assigned to contact
else
// tasks exists for this contact
you are querying FROM Contact so the list must be of type list'(Contact)'..
if you want to check to see if any tasks are related to a contact you need to iterate over the list of contacts and see if tasks exists.
for( Contact con : [SELECT ID,
(SELECT Id,
Subject
FROM Tasks)
FROM Contact
WHERE New_Relationship_owner1__c IN :Trigger.New])
if ( con.tasks.isEmpty() )
// no tasks assigned to contact
else
// tasks exists for this contact
answered Aug 16 at 19:40
Benjamin Pirih
1,696719
1,696719
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsalesforce.stackexchange.com%2fquestions%2f229143%2fhelp-with-query-illegal-assignment-from-a-list-to-a-list%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password