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
Antonio_hotelbedsAntonio_hotelbeds 

Get a list of ids from an SOQL query

Hi,

 

I have my accounts related on a way that one parent Account is pointed by its daughters with the field ParentId.

 

I need to do a trigger that when the parent account is modified in the field Recovery_actions__C(is a checkfield), this change has to go to the daughters accounts.

 

This is my trigger:

 

trigger UpdateCierrePorRecobro on Account (beforeupdate) {

 

Set<Id> addAccountsActivate = newSet<Id>();

Set<Id> addAccountsDeactivate = newSet<Id>();

 

//Aislo los id's

 

for(integer i=0;i<Trigger.new.size();i++){

//If the value has changed add to the list

if ((Trigger.new[i].Recovery_Actions__c!=Trigger.old[i].Recovery_Actions__c)) { 

if (trigger.new[i].recovery_actions__c){//changed to true

addAccountsActivate.add(Trigger.new[i].id);

}

else{//changed to false

addAccountsDeactivate.add(Trigger.new[i].id);

}

}

}

if(addAccountsActivate.size()>0){

 

//create a list of ids with the daughter ids of the main account

//????????????????????????????????????????

List<id> listaToTrue=newList<id>();

listaParaActivar.add([select id fromAccountwhereParentId in:addAccountsActivate]);

//????????????????????????????????????????????

 

Accountacc;

 

//and update the value

for(integer j=0;j<listaToTrue.size();j++){

acc.id=listaParaActivar[j];

acc.Reacovery_Actions__c=1;

updateacc;

}

}

 

}

 

 

---------------------------------------

 

I have the problem in the part where I query in order to get the ids of the daughter accounts, I dont know how to get that list of ids.

 

Anyone could help here?

 

Thanks,

Antonio

Best Answer chosen by Admin (Salesforce Developers) 
ministe2003ministe2003

Oh I see your problem.  You're querying the list of accounts, meaning you're returning accounts.  Even though you're only querying for the ID field the result set will still be Accounts.

 

Change the list type from ID to account and the query will work.  Then either work with the IDs from the queried accounts or if you just want a list of the IDs, loop over the list of accounts and add their IDs to a new list.  This should work:

 

List<id> listaToTrue=newList<id>();
for(Account a : [select id from Account where ParentId in:addAccountsActivate]){
    listaParaActivar.add(a.id);
}

 

All Answers

ministe2003ministe2003

Looks ok to me, you're querying for the ParentID where the ID is in your pre-made list.  What problem are you having?

Antonio_hotelbedsAntonio_hotelbeds

Im getting this error:

 

Save error: Invalid initial value type LIST<Account> for LIST<Id>

ministe2003ministe2003

Oh I see your problem.  You're querying the list of accounts, meaning you're returning accounts.  Even though you're only querying for the ID field the result set will still be Accounts.

 

Change the list type from ID to account and the query will work.  Then either work with the IDs from the queried accounts or if you just want a list of the IDs, loop over the list of accounts and add their IDs to a new list.  This should work:

 

List<id> listaToTrue=newList<id>();
for(Account a : [select id from Account where ParentId in:addAccountsActivate]){
    listaParaActivar.add(a.id);
}

 

This was selected as the best answer
Antonio_hotelbedsAntonio_hotelbeds

Thanks a lot ministe2003!!

Jordan TuckerJordan Tucker
List<Id> listaToTrue = new List<Id>(new Map<Id, Account>([select Id from Account where ParentId in :addAccountsActivate]).keySet());

Alan SierraAlan Sierra
Set<Id> RTIDFinPipe = new Set<Id>(New Map<Id, RecordType>([SELECT Id FROM RecordType WHERE DeveloperName =: 'EG_tre_FinanciamientoPipeline']).keySet());

 
Mike ArthurMike Arthur
Why was Alan Sierra's solution downvoted?
Tad Aalgaard 3Tad Aalgaard 3
Probably because the original poster asked for a List, not a Set.
Ionut Constantin SultanaIonut Constantin Sultana

Is Alan Sierra's solution documented somewhere by Salesforce? 

It's working so I was wondering where I can learn more about this tips and tricks :D 

Thanks you! 

Mike ArthurMike Arthur
Hi Ionut - have a look at the section on Collections in the Apex Developer Guide
https://developer.salesforce.com/docs/atlas.en-us.232.0.apexcode.meta/apexcode/langCon_apex_collections.htm

and check out the fun things that you can do with them using their methods:
https://developer.salesforce.com/docs/atlas.en-us.232.0.apexref.meta/apexref/apex_methods_system_list.htm#apex_methods_system_list
https://developer.salesforce.com/docs/atlas.en-us.232.0.apexref.meta/apexref/apex_methods_system_map.htm#apex_methods_system_map
https://developer.salesforce.com/docs/atlas.en-us.232.0.apexref.meta/apexref/apex_methods_system_set.htm#apex_methods_system_set