Question

I have a field on the account object that updates when the account is edited. When the record is edited individually, the correct value gets written to the field. When I use the data loader . . . One value applies to all records. Here is my trigger, please advise.

trigger Populate_Transfer_Manager on Account (before insert, before Update) {   

    list<id> aid = new list<id>();
    for(account a: trigger.new){           
        aid.add(a.ownerid);
    }

list<user> managers = new list<user>();
    managers = [select managerid from user where id in: aid];


set<id> manid = new set<id>();                             
    for(user u: managers){
        manid.add(u.managerid);
    }


for(account a: trigger.new){        
    for(id i: manid){           
            a.transfer_manager__c = i;       
        }
    }
}
Was it helpful?

Solution

The trigger was only firing one time for each batch of 200 and assigning the value to all records in the batch. Using maps I was able to assign the correct value to all records in my database.

trigger Populate_Transfer_Manager on Account (before insert, before Update) {    

    list<id> aid = new list<id>();
    for(account a: trigger.new){                   
        aid.add(a.ownerid);
            }
    map<id, user> users = new map<id, user>(
        [select managerid from user where id in: aid]);

    for(account a: trigger.new){
        a.Transfer_Manager__c = users.get(a.ownerid).managerid;
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top