How can I initialise an apex Set, Set or Set from SOQL in Apex Code?
Clash Royale CLAN TAG#URR8PPP
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;
up vote
2
down vote
favorite
Is this possible? I would like to do the following directly:
Set<String> titles = new Set<String>([Select Title From SObject]);
apex soql
add a comment |Â
up vote
2
down vote
favorite
Is this possible? I would like to do the following directly:
Set<String> titles = new Set<String>([Select Title From SObject]);
apex soql
3
this functionality doesn't exist unfortunately. You will have to iterate over the list of SObjects and manually add the title fields to the set.
– Programatic
Sep 5 at 14:15
add a comment |Â
up vote
2
down vote
favorite
up vote
2
down vote
favorite
Is this possible? I would like to do the following directly:
Set<String> titles = new Set<String>([Select Title From SObject]);
apex soql
Is this possible? I would like to do the following directly:
Set<String> titles = new Set<String>([Select Title From SObject]);
apex soql
asked Sep 5 at 14:08
KennyBartMan
1807
1807
3
this functionality doesn't exist unfortunately. You will have to iterate over the list of SObjects and manually add the title fields to the set.
– Programatic
Sep 5 at 14:15
add a comment |Â
3
this functionality doesn't exist unfortunately. You will have to iterate over the list of SObjects and manually add the title fields to the set.
– Programatic
Sep 5 at 14:15
3
3
this functionality doesn't exist unfortunately. You will have to iterate over the list of SObjects and manually add the title fields to the set.
– Programatic
Sep 5 at 14:15
this functionality doesn't exist unfortunately. You will have to iterate over the list of SObjects and manually add the title fields to the set.
– Programatic
Sep 5 at 14:15
add a comment |Â
2 Answers
2
active
oldest
votes
up vote
7
down vote
accepted
No it is not possible except for Set<Id>
. With other types, you need a for
loop:
Set<String> names = new Set<String>();
for (MyObject__c record : [SELECT Name FROM MyObject__c])
names.add(record.Name);
The special case is that with a record's own Id, you can use this Map constructor:
Set<Id> ids = new Map<Id, SObject>([/*query*/]).keySet();
You can abuse this shortcut and field alias functionality in aggregate queries to get a collection of parent Ids also.
Set<Id> parentIds = new Map<Id, SObject>([
SELECT Parent__c Id FROM MyObject__c GROUP BY Parent__c
// ^^ you can now call .get('Id') on a result record
]).keySet();
Missing comma between Parent__c and Id in the SOQL query.
– Tad
Sep 5 at 17:15
1
No. That is the whole point of including that query. What it was actually missing is theGROUP BY
clause.
– Adrian Larson♦
Sep 5 at 17:17
I don't understand. You're saying you intentionally left out the comma between Parent__c and Id in your example. This query as is won't work without a comma between the fields. SELECT Parent__c Id FROM MyObject__c GROUP BY Parent__c Adding a comma will make it work. SELECT Parent__c, Id FROM MyObject__c GROUP BY Parent__c If I'm not following then it might be that your explanation needs a bit more clarification as to why you left it out.
– Tad
Sep 5 at 17:20
2
I didn't leave anything out. I am not showing how to query two separate fields. I am using alias functionally.
– Adrian Larson♦
Sep 5 at 17:21
Ahhhh... I see now. My fault. Interesting solution.
– Tad
Sep 5 at 17:22
 |Â
show 1 more comment
up vote
3
down vote
You can do it fairly easily for the Set of Id use case by passing the SOQL to the constructor of an Id to Sobject Map and then grabbing the keyset from this;-
Set<Id> Ids = new Map<Id, Sobject>([SELECT Id FROM SObject]).keySet();
Other than that though there isn't a direct way to do it for non-Id fields. Your best bet would probably to create a reusable worker method to handle this.
Depending on what you need this for aggregate SOQL functions may help as they will produce a list of all unique values for the aggregated field.
Set<String> titles = new Set<String>();
for(AggregateResult ar : [SELECT Title FROM SObject GROUP BY Title])
titles.add((String)ar.get('Title'));
New contributor
Alex 255 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
add a comment |Â
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
7
down vote
accepted
No it is not possible except for Set<Id>
. With other types, you need a for
loop:
Set<String> names = new Set<String>();
for (MyObject__c record : [SELECT Name FROM MyObject__c])
names.add(record.Name);
The special case is that with a record's own Id, you can use this Map constructor:
Set<Id> ids = new Map<Id, SObject>([/*query*/]).keySet();
You can abuse this shortcut and field alias functionality in aggregate queries to get a collection of parent Ids also.
Set<Id> parentIds = new Map<Id, SObject>([
SELECT Parent__c Id FROM MyObject__c GROUP BY Parent__c
// ^^ you can now call .get('Id') on a result record
]).keySet();
Missing comma between Parent__c and Id in the SOQL query.
– Tad
Sep 5 at 17:15
1
No. That is the whole point of including that query. What it was actually missing is theGROUP BY
clause.
– Adrian Larson♦
Sep 5 at 17:17
I don't understand. You're saying you intentionally left out the comma between Parent__c and Id in your example. This query as is won't work without a comma between the fields. SELECT Parent__c Id FROM MyObject__c GROUP BY Parent__c Adding a comma will make it work. SELECT Parent__c, Id FROM MyObject__c GROUP BY Parent__c If I'm not following then it might be that your explanation needs a bit more clarification as to why you left it out.
– Tad
Sep 5 at 17:20
2
I didn't leave anything out. I am not showing how to query two separate fields. I am using alias functionally.
– Adrian Larson♦
Sep 5 at 17:21
Ahhhh... I see now. My fault. Interesting solution.
– Tad
Sep 5 at 17:22
 |Â
show 1 more comment
up vote
7
down vote
accepted
No it is not possible except for Set<Id>
. With other types, you need a for
loop:
Set<String> names = new Set<String>();
for (MyObject__c record : [SELECT Name FROM MyObject__c])
names.add(record.Name);
The special case is that with a record's own Id, you can use this Map constructor:
Set<Id> ids = new Map<Id, SObject>([/*query*/]).keySet();
You can abuse this shortcut and field alias functionality in aggregate queries to get a collection of parent Ids also.
Set<Id> parentIds = new Map<Id, SObject>([
SELECT Parent__c Id FROM MyObject__c GROUP BY Parent__c
// ^^ you can now call .get('Id') on a result record
]).keySet();
Missing comma between Parent__c and Id in the SOQL query.
– Tad
Sep 5 at 17:15
1
No. That is the whole point of including that query. What it was actually missing is theGROUP BY
clause.
– Adrian Larson♦
Sep 5 at 17:17
I don't understand. You're saying you intentionally left out the comma between Parent__c and Id in your example. This query as is won't work without a comma between the fields. SELECT Parent__c Id FROM MyObject__c GROUP BY Parent__c Adding a comma will make it work. SELECT Parent__c, Id FROM MyObject__c GROUP BY Parent__c If I'm not following then it might be that your explanation needs a bit more clarification as to why you left it out.
– Tad
Sep 5 at 17:20
2
I didn't leave anything out. I am not showing how to query two separate fields. I am using alias functionally.
– Adrian Larson♦
Sep 5 at 17:21
Ahhhh... I see now. My fault. Interesting solution.
– Tad
Sep 5 at 17:22
 |Â
show 1 more comment
up vote
7
down vote
accepted
up vote
7
down vote
accepted
No it is not possible except for Set<Id>
. With other types, you need a for
loop:
Set<String> names = new Set<String>();
for (MyObject__c record : [SELECT Name FROM MyObject__c])
names.add(record.Name);
The special case is that with a record's own Id, you can use this Map constructor:
Set<Id> ids = new Map<Id, SObject>([/*query*/]).keySet();
You can abuse this shortcut and field alias functionality in aggregate queries to get a collection of parent Ids also.
Set<Id> parentIds = new Map<Id, SObject>([
SELECT Parent__c Id FROM MyObject__c GROUP BY Parent__c
// ^^ you can now call .get('Id') on a result record
]).keySet();
No it is not possible except for Set<Id>
. With other types, you need a for
loop:
Set<String> names = new Set<String>();
for (MyObject__c record : [SELECT Name FROM MyObject__c])
names.add(record.Name);
The special case is that with a record's own Id, you can use this Map constructor:
Set<Id> ids = new Map<Id, SObject>([/*query*/]).keySet();
You can abuse this shortcut and field alias functionality in aggregate queries to get a collection of parent Ids also.
Set<Id> parentIds = new Map<Id, SObject>([
SELECT Parent__c Id FROM MyObject__c GROUP BY Parent__c
// ^^ you can now call .get('Id') on a result record
]).keySet();
edited Sep 5 at 17:22
answered Sep 5 at 14:42


Adrian Larson♦
100k19106224
100k19106224
Missing comma between Parent__c and Id in the SOQL query.
– Tad
Sep 5 at 17:15
1
No. That is the whole point of including that query. What it was actually missing is theGROUP BY
clause.
– Adrian Larson♦
Sep 5 at 17:17
I don't understand. You're saying you intentionally left out the comma between Parent__c and Id in your example. This query as is won't work without a comma between the fields. SELECT Parent__c Id FROM MyObject__c GROUP BY Parent__c Adding a comma will make it work. SELECT Parent__c, Id FROM MyObject__c GROUP BY Parent__c If I'm not following then it might be that your explanation needs a bit more clarification as to why you left it out.
– Tad
Sep 5 at 17:20
2
I didn't leave anything out. I am not showing how to query two separate fields. I am using alias functionally.
– Adrian Larson♦
Sep 5 at 17:21
Ahhhh... I see now. My fault. Interesting solution.
– Tad
Sep 5 at 17:22
 |Â
show 1 more comment
Missing comma between Parent__c and Id in the SOQL query.
– Tad
Sep 5 at 17:15
1
No. That is the whole point of including that query. What it was actually missing is theGROUP BY
clause.
– Adrian Larson♦
Sep 5 at 17:17
I don't understand. You're saying you intentionally left out the comma between Parent__c and Id in your example. This query as is won't work without a comma between the fields. SELECT Parent__c Id FROM MyObject__c GROUP BY Parent__c Adding a comma will make it work. SELECT Parent__c, Id FROM MyObject__c GROUP BY Parent__c If I'm not following then it might be that your explanation needs a bit more clarification as to why you left it out.
– Tad
Sep 5 at 17:20
2
I didn't leave anything out. I am not showing how to query two separate fields. I am using alias functionally.
– Adrian Larson♦
Sep 5 at 17:21
Ahhhh... I see now. My fault. Interesting solution.
– Tad
Sep 5 at 17:22
Missing comma between Parent__c and Id in the SOQL query.
– Tad
Sep 5 at 17:15
Missing comma between Parent__c and Id in the SOQL query.
– Tad
Sep 5 at 17:15
1
1
No. That is the whole point of including that query. What it was actually missing is the
GROUP BY
clause.– Adrian Larson♦
Sep 5 at 17:17
No. That is the whole point of including that query. What it was actually missing is the
GROUP BY
clause.– Adrian Larson♦
Sep 5 at 17:17
I don't understand. You're saying you intentionally left out the comma between Parent__c and Id in your example. This query as is won't work without a comma between the fields. SELECT Parent__c Id FROM MyObject__c GROUP BY Parent__c Adding a comma will make it work. SELECT Parent__c, Id FROM MyObject__c GROUP BY Parent__c If I'm not following then it might be that your explanation needs a bit more clarification as to why you left it out.
– Tad
Sep 5 at 17:20
I don't understand. You're saying you intentionally left out the comma between Parent__c and Id in your example. This query as is won't work without a comma between the fields. SELECT Parent__c Id FROM MyObject__c GROUP BY Parent__c Adding a comma will make it work. SELECT Parent__c, Id FROM MyObject__c GROUP BY Parent__c If I'm not following then it might be that your explanation needs a bit more clarification as to why you left it out.
– Tad
Sep 5 at 17:20
2
2
I didn't leave anything out. I am not showing how to query two separate fields. I am using alias functionally.
– Adrian Larson♦
Sep 5 at 17:21
I didn't leave anything out. I am not showing how to query two separate fields. I am using alias functionally.
– Adrian Larson♦
Sep 5 at 17:21
Ahhhh... I see now. My fault. Interesting solution.
– Tad
Sep 5 at 17:22
Ahhhh... I see now. My fault. Interesting solution.
– Tad
Sep 5 at 17:22
 |Â
show 1 more comment
up vote
3
down vote
You can do it fairly easily for the Set of Id use case by passing the SOQL to the constructor of an Id to Sobject Map and then grabbing the keyset from this;-
Set<Id> Ids = new Map<Id, Sobject>([SELECT Id FROM SObject]).keySet();
Other than that though there isn't a direct way to do it for non-Id fields. Your best bet would probably to create a reusable worker method to handle this.
Depending on what you need this for aggregate SOQL functions may help as they will produce a list of all unique values for the aggregated field.
Set<String> titles = new Set<String>();
for(AggregateResult ar : [SELECT Title FROM SObject GROUP BY Title])
titles.add((String)ar.get('Title'));
New contributor
Alex 255 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
add a comment |Â
up vote
3
down vote
You can do it fairly easily for the Set of Id use case by passing the SOQL to the constructor of an Id to Sobject Map and then grabbing the keyset from this;-
Set<Id> Ids = new Map<Id, Sobject>([SELECT Id FROM SObject]).keySet();
Other than that though there isn't a direct way to do it for non-Id fields. Your best bet would probably to create a reusable worker method to handle this.
Depending on what you need this for aggregate SOQL functions may help as they will produce a list of all unique values for the aggregated field.
Set<String> titles = new Set<String>();
for(AggregateResult ar : [SELECT Title FROM SObject GROUP BY Title])
titles.add((String)ar.get('Title'));
New contributor
Alex 255 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
add a comment |Â
up vote
3
down vote
up vote
3
down vote
You can do it fairly easily for the Set of Id use case by passing the SOQL to the constructor of an Id to Sobject Map and then grabbing the keyset from this;-
Set<Id> Ids = new Map<Id, Sobject>([SELECT Id FROM SObject]).keySet();
Other than that though there isn't a direct way to do it for non-Id fields. Your best bet would probably to create a reusable worker method to handle this.
Depending on what you need this for aggregate SOQL functions may help as they will produce a list of all unique values for the aggregated field.
Set<String> titles = new Set<String>();
for(AggregateResult ar : [SELECT Title FROM SObject GROUP BY Title])
titles.add((String)ar.get('Title'));
New contributor
Alex 255 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
You can do it fairly easily for the Set of Id use case by passing the SOQL to the constructor of an Id to Sobject Map and then grabbing the keyset from this;-
Set<Id> Ids = new Map<Id, Sobject>([SELECT Id FROM SObject]).keySet();
Other than that though there isn't a direct way to do it for non-Id fields. Your best bet would probably to create a reusable worker method to handle this.
Depending on what you need this for aggregate SOQL functions may help as they will produce a list of all unique values for the aggregated field.
Set<String> titles = new Set<String>();
for(AggregateResult ar : [SELECT Title FROM SObject GROUP BY Title])
titles.add((String)ar.get('Title'));
New contributor
Alex 255 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
Alex 255 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
answered Sep 5 at 14:41


Alex 255
312
312
New contributor
Alex 255 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
Alex 255 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
Alex 255 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
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%2f231330%2fhow-can-i-initialise-an-apex-setstring-setid-or-setdate-from-soql-in-apex%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
3
this functionality doesn't exist unfortunately. You will have to iterate over the list of SObjects and manually add the title fields to the set.
– Programatic
Sep 5 at 14:15