You need to sign in to do that
Don't have an account?
NKrishna
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);
}
}
}
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..
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
Why do u doing it as before insert...?? any specific requirement/....
Try doing it at after insert.
YEs I want to pass some values to account object. After insert the fields are read only.
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!
Hi Martin,
Thanks.