+ Start a Discussion
D J RobertsD J Roberts 

How to use SOQL records from another Method

I have a method called getAccountIds, that is successfully returning a set of Account Ids. I second method that is countContacts(List<Id>acctIds). But when I return the acctIds, it doesn't get added picked up by the second method.
I'm calling the class from an execute anonymous window like this --> 
AccountProcessor.getAccountIds(); 
AccountProcessor.countContacts();



Actual Class and methods. 
 
public class AccountProcessor {
    
    public static List<Id> getAccountIds(){
        List<Id> acctIds = new List<Id>(); 
        For(Account a:[SELECT Id FROM Account LIMIT 200] ){
            acctIds.add(a.Id);
        }
        System.debug('The list of Ids = ' + acctIds); 
        System.debug('The size of List = ' +acctIds.size());
        return acctIds; 
        
    }
   
    
    @future
  
    
    public static void  countContacts(List<Id> acctIds){
       	//Declare Variables.
       	Account accountToUpdate; 
        List<Account> accountsToUpdate = new List<Account>(); 
       	Integer totalContacts = 0; 
        System.debug('Size of List =' + acctIds.size());
        List<Contact> cons = [SELECT Id, AccountId FROM Contact WHERE AccountId IN: acctIds]; 
        System.Debug('Contact List = ' + cons.size()); 
		
        //Loop through the contacts and sum the account Id for each contact. 
        for( Id a : acctIds){
            
            System.Debug('Account Ids on Contact = ' + a); 
          	Contact[] contactIdArray = new List<Contact>();  
                   for(Contact c : cons){
                      // System.debug('Enter Contact Loop ');
                      // System.Debug('Account Id = ' + a);
                       Boolean resultType = a.equals(c.AccountId);
                       //System.Debug('ResultType is ? ' + resultType); 
                        
                      	If(a==c.AccountId){
                             contactIdArray.add(c);
                          	
                       		}
                       
					
	        }
             
             
             accountToUpdate = [SELECT Id FROM Account WHERE Id =: a LIMIT 1]; 
             totalContacts = contactIdArray.size();
             accountToUpdate.Number_of_Contacts__c = totalContacts; 
             accountsToUpdate.add(accountToUpdate); 
             System.debug('Total Contacts = ' + totalContacts);
             System.debug('Total Update Accounts = ' + accountsToUpdate.size());
            
        }
       update accountsToUpdate; 
        
    }
	 	
}

 
Best Answer chosen by D J Roberts
Alain CabonAlain Cabon
@D J Roberts
AccountProcessor.countContacts( AccountProcessor.getAccountIds() );

It is a future method (asynchronous ).

All Answers

Alain CabonAlain Cabon
@D J Roberts
AccountProcessor.countContacts( AccountProcessor.getAccountIds() );

It is a future method (asynchronous ).
This was selected as the best answer
D J RobertsD J Roberts
@Alain Cabon, 

That did the trick! Thanks for the help. 

I do have a question though, why doesn't the class just execute in logical order, and just finish when resources are available? My understanding from the Trailhead info, was that the resources are allocated when they are available, so I was thinking that once the class executed it would just wait to execute the method AccountProcessor.countContacts(). Am I misunderstanding the nature of the @future methods, or is this something unique to executing a class anonymously that contains a future method? 

Thanks again for the help. 
Alain CabonAlain Cabon
It is correct for the future method that the resources are allocated when they are available.

The variables acctIds above have the same names indeed but they are not the same for the compiler.

That is is misleading at the beginning but just remember that you always need to pass the same number of variables (and types) when it is not empty between the parentheses. The apex compiler doesn't search a variable with the same name elsewhere here. 

Sometimes, we can call methods or constructors having the same names with or without parameters but both definitions exist clearly (overloading) and you have perhaps see that in the documentation but here you don't have the method without parameter.