You need to sign in to do that
Don't have an account?
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
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:
All Answers
Looks ok to me, you're querying for the ParentID where the ID is in your pre-made list. What problem are you having?
Im getting this error:
Save error: Invalid initial value type LIST<Account> for LIST<Id>
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:
Thanks a lot ministe2003!!
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!
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