+ 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.