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
awaken88awaken88 

Mapping All Accounts - Search

Hey Guys,

 

I'm currently trying to go through all my code and make everyhing much more efficient..

 

Which means I'm trying to reduce the number of calls through statements etc.

 

I'm struggling to get my head around the MAP Class..

 

What I'm trying to do is Map All Accounts, but within my trigger I need to be able to call upon the specific account which matches Key__c from a custom object and return all account details.

 

I'm currently doing that with a query, but unsure how I can query a MAP to return results?

 

Any help always appreciated!


Regards,

 

Richard Hubbard

Best Answer chosen by Admin (Salesforce Developers) 
hpereirahpereira

I think i understand now :)

So try something like:

 

// build accounts map
Map<Id, Account> accountsMap = new Map<Id, Account>();
List<Account> allAccounts = [Select Key__c, Id, Name, etc. From Account];
For (Account a : allAccounts) {
   accountsMap.put(a.Key__c, a);
}

// get the account you need
Account myAccount = accountsMap.get(YOUR_OBJECT.key);

  Regards

All Answers

hpereirahpereira

Hello awaken88,

 

Not sure if i understood your problem, but i guess you need something like this:

 

// build accounts map
Map<Id, Account> accountsMap = new Map<Id, Account>();
List<Account> allAccounts = [Select Id, Name From Account];
For (Account a : allAccounts) {
   accountsMap.put(a.Id, a);
}

// get the account you need
Account myAccount = accountsMap.get(YOUR_OBJECT.key__c);

 

 

If this is not the case, please explain a bit better your issue.

 

Regards

awaken88awaken88

Hi Hpereira,

 

Sorry posting things at 1.30 am sometimes the brain isn't functioning 100%

 

Basically I have a field on the account called Key__c

 

And a field on the custom object called Key, But I need to search all the accounts for the Key__c and return that account ID, along with the possibilities of other fields on that account.

 

Hope that makes a bit more sense! 

 

Thanks :)

 

Richard

hpereirahpereira

I think i understand now :)

So try something like:

 

// build accounts map
Map<Id, Account> accountsMap = new Map<Id, Account>();
List<Account> allAccounts = [Select Key__c, Id, Name, etc. From Account];
For (Account a : allAccounts) {
   accountsMap.put(a.Key__c, a);
}

// get the account you need
Account myAccount = accountsMap.get(YOUR_OBJECT.key);

  Regards

This was selected as the best answer
awaken88awaken88

Hi again hyereira,

 

Thanks for replying again so quickly!

 

One thing I had to change was Map<Id, Account> to Map<String,Account> and it worked perfectly :)

 

Thanks ever so much!