You need to sign in to do that
Don't have an account?

Code isn't giving me the right results (or any results really)
This Trigger that I have written isn't working correctly. What i am trying to do is get a list of the users that includes ID and Salesman Number. Then I trying to update the account with the new user ID. Currently nothing is changing, once I click edit and I don't know why.
thanks.
kevin
trigger AccountOwnerUpdateTrigger on Account (before insert, before update) {
// this map will contain a list of Accounts with the same salesman #
Map<String, List<Account>> AccountOwnerMap = new Map<String, List<Account>> ();
for( Account Acc : trigger.new ) {
// create the list if it doesn't already exist
List<Account> AcctList = AccountOwnerMap.get( Acc.Ownerid );
if( AcctList == null ) {
AcctList = new List<Account>();
}
AcctList.add( Acc );
AccountOwnerMap.put( Acc.OwnerID, AcctList );
}
// get list of CURRENTLY ACTIVE Users using the field Salesman_Number__c
List<User> UserList = [
SELECT ID, Salesman_Number__c
FROM User
WHERE ID IN :AccountOwnerMap.keySet() AND IsActive = True ];
// go through the User List and get a list of Accounts that have the same salesman
for( User User : UserList ) {
List<Account> AcctList = AccountOwnerMap.get( User.ID );
// assign the Account Owner for each Account in the list
for( Account Acc : AcctList )
Acc.Ownerid = User.ID;
}
}
thanks.
kevin
trigger AccountOwnerUpdateTrigger on Account (before insert, before update) {
// this map will contain a list of Accounts with the same salesman #
Map<String, List<Account>> AccountOwnerMap = new Map<String, List<Account>> ();
for( Account Acc : trigger.new ) {
// create the list if it doesn't already exist
List<Account> AcctList = AccountOwnerMap.get( Acc.Ownerid );
if( AcctList == null ) {
AcctList = new List<Account>();
}
AcctList.add( Acc );
AccountOwnerMap.put( Acc.OwnerID, AcctList );
}
// get list of CURRENTLY ACTIVE Users using the field Salesman_Number__c
List<User> UserList = [
SELECT ID, Salesman_Number__c
FROM User
WHERE ID IN :AccountOwnerMap.keySet() AND IsActive = True ];
// go through the User List and get a list of Accounts that have the same salesman
for( User User : UserList ) {
List<Account> AcctList = AccountOwnerMap.get( User.ID );
// assign the Account Owner for each Account in the list
for( Account Acc : AcctList )
Acc.Ownerid = User.ID;
}
}
I did not understand youe requirement.Can you explain it again.
Here are some mistakes in you trigger so your code not working properly.
1. In trigger before event you need to make changes when Looping through Trigger’s collections variables.Otherwisw noting going to happen.
Ex.
for(Account acc : Trigger.new) {
acc.OwnerId = Somethins;
}
In your code
// assign the Account Owner for each Account in the list
for( Account Acc : AcctList )
Acc.Ownerid = User.ID;
}
2. You are not changing anything by this trigger.
Resion :
1. You are using AccountOwnerMap map's keyset to get users from your system
Let's suppose your map have userId as Key and Account records as value
Like :
UserId => 1. Account 1
2. Account 2
(List of Account).
Now you get user by this key and looping through User List to get account related to that user.
But these accounts already own by that user and you are also doing same thing
Acc.Ownerid = User.ID
So noting going to change.
3. Error that you are getting after apply update AcctList statement in youe code,because in before event DML statements not allow on same object.
For more dtails about apex trigger follow this links
http://abhithetechknight.blogspot.in/2014/06/basics-of-apex-trigger-for-beginners.html
All Answers
update AcctList;
After adding the line, now I am getting the following error when saving a record:
Error: Invalid Data.
Review all error messages below to correct your data.
Apex trigger AccountOwnerUpdateTrigger caused an unexpected exception, contact your administrator: AccountOwnerUpdateTrigger: execution of BeforeUpdate caused by: System.SObjectException: DML statment cannot operate on trigger.new or trigger.old: Trigger.AccountOwnerUpdateTrigger: line 30, column 1
Thanks.
Kevin
I did not understand youe requirement.Can you explain it again.
Here are some mistakes in you trigger so your code not working properly.
1. In trigger before event you need to make changes when Looping through Trigger’s collections variables.Otherwisw noting going to happen.
Ex.
for(Account acc : Trigger.new) {
acc.OwnerId = Somethins;
}
In your code
// assign the Account Owner for each Account in the list
for( Account Acc : AcctList )
Acc.Ownerid = User.ID;
}
2. You are not changing anything by this trigger.
Resion :
1. You are using AccountOwnerMap map's keyset to get users from your system
Let's suppose your map have userId as Key and Account records as value
Like :
UserId => 1. Account 1
2. Account 2
(List of Account).
Now you get user by this key and looping through User List to get account related to that user.
But these accounts already own by that user and you are also doing same thing
Acc.Ownerid = User.ID
So noting going to change.
3. Error that you are getting after apply update AcctList statement in youe code,because in before event DML statements not allow on same object.
For more dtails about apex trigger follow this links
http://abhithetechknight.blogspot.in/2014/06/basics-of-apex-trigger-for-beginners.html
Thanks.
Kevin