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
NKrishnaNKrishna 

Apex trigger Issue

Hi,

I created a new Apex trigger in Account object.

 

trigger AccountTrigger on Account (before insert) {
    if(Trigger.IsInsert) {

        List<Account> AccountList = new List<Account>();
        Map<Id,Account> AccountMap = new Map<Id, Account>();
        Map<Id, Boolean> UsersActiveMap = new Map<Id, Boolean>();
        
        for(Account AccountNew : Trigger.New) {
            AccountMap.put(AccountNew.OwnerId, AccountNew);
        }
        system.debug('Account Map Values '+ AccountMap);
        system.debug('Account Map SIZE Values '+ AccountMap.size());
        
        List<User> UserList = [select Id, name, Username, IsActive from user where Id IN : AccountMap.Keyset()];
        for(User Users : UserList) {
            UsersActiveMap.put(Users.Id, Users.IsActive);
        }
        system.debug('UsersActiveMap Values '+ UsersActiveMap);
        
        for(Account Acc : AccountMap.Values()) {
            system.debug('AccountMap Size()' + AccountMap.size() + ' '+ Acc.OwnerId );
            if(UsersActiveMap.get(Acc.OwnerId)) {
                AccountList.add(Acc);
                system.debug('AccountList' + AccountList.size());    
            }
        }
        system.debug('AccountList Values '+ AccountList);
        if(Trigger.IsBefore) {
            system.debug('SSS AccountList Size()' + AccountList.size());
            AccountTriggerClass.UpdateCallFrequency(AccountList);
        }
    }    
}   

I run via data loader I have 20 records in CSV file.

I got only One record on line 12

system.debug('Account Map SIZE Values '+ AccountMap.size());

but I want 20.

Is anything I am missing..

 

 

 

Best Answer chosen by Admin (Salesforce Developers) 
MartinHaagenMartinHaagen

Hi NKrishna,

 

if you have the same account owner on all of the inserted account the map will only contain one account (the latest added). If you need to store multiple accounts in a map, you can nest a list in the map. Like:

 

Map<string, List<Account>> name = new Map<string, List<Account>>();

 

However this will require some more work when adding accounts to the Map. 

 

if (name.containsKey(key)) {

  List<Account> accounts = name.get(key);

  accounts.add(YourAccount);

} else {

  List<Account> accounts = new List<Account>();

  accounts.add(YourAccount);

  name.put(key, account);

}

 

Please note this code is totaly untested, only added it to illustrate what you can do.

 

Hope it helps!

All Answers

Santosh KumbarSantosh Kumbar

Why do u doing it as before insert...?? any specific requirement/....

 

 

Try doing it at after insert.

NKrishnaNKrishna

YEs I want to pass some values to account object. After insert the fields are read only.

MartinHaagenMartinHaagen

Hi NKrishna,

 

if you have the same account owner on all of the inserted account the map will only contain one account (the latest added). If you need to store multiple accounts in a map, you can nest a list in the map. Like:

 

Map<string, List<Account>> name = new Map<string, List<Account>>();

 

However this will require some more work when adding accounts to the Map. 

 

if (name.containsKey(key)) {

  List<Account> accounts = name.get(key);

  accounts.add(YourAccount);

} else {

  List<Account> accounts = new List<Account>();

  accounts.add(YourAccount);

  name.put(key, account);

}

 

Please note this code is totaly untested, only added it to illustrate what you can do.

 

Hope it helps!

This was selected as the best answer
NKrishnaNKrishna

Hi Martin,

 

Thanks.