0
trigger userDetailupdateFromAccount on Account (before update) {

    //Get all accounts
    Map<ID,Account> acc = new Map<ID,Account>([Select OwnerID,Name FROM Account WHERE ID IN : Trigger.oldMap.keyset()]);
    //Get all users
    Map<ID,user> allUsers = new Map<ID,User>([Select ID,Name,email from User where ID IN : acc.keyset()]);
    Map<ID,User> accToUser = new Map<ID,User>();
    for(Account a : trigger.new){
        User u = allUsers.get(a.OwnerID);
      //  a.description__c = u.Email;
      system.debug('zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz'+allUsers+acc );
      system.debug('xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'+acc.keyset());
    }    
}

Map acc = new Map([Select OwnerID,Name FROM Account WHERE ID IN : Trigger.oldMap.keyset()]); - is setting AccountID to Map. How can I assign it to OwnerID instead of AccountID ?

Thanks

  • The reason for no users found is because in the map acc the id key field is the Account.Id not the Owner.Id. Kieth C's answer is the way to go. – Richard Durrant Sep 25 '15 at 8:42
2

You don't need to query to get the immediate fields of the Account so your trigger can look like this:

trigger userDetailupdateFromAccount on Account (before update) {

    Set<Id> owners = new Set<Id>();
    for (Account a : Trigger.new) {
        owners.add(a.OwnerId);
    }

    Map<ID,user> users = new Map<ID,User>([
            Select ID,Name,email
            from User
            where ID IN :owners
            ]);

    for (Account a : Trigger.new) {
        User u = users.get(a.OwnerID);
        ...
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.