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
Kevin TullosKevin Tullos 

Change account Owner based on account and user attribute matching

I am trying to write a trigger that updates the account owner with the user who has the cooresponding salesman_ID field.  The account has OwnerID, and Salesman_Number__c, and I need to get the ID from the User Record that has the matching Salesman_Number__c.  I have tried a lot of things and I cannot get them to compile (error:  Error: Compile Error: Illegal assignment from Schema.SObjectField to Id at line 12 column 69)  .  Also I am unsure if the code will deliver the results that I need.  Please help.

Thanks.

Kevin



trigger Update_Account_Owner on Account (before insert, before update) {

    // get list of CURRENTLY ACTIVE Users
        List<User> UserList = [ SELECT ID, Salesman_Number__c
                        FROM User
                        WHERE IsActive = True
                        ORDER BY Salesman_Number__c ];

        // if a salesman number was found, assign the User.ID for the Account
        // if not, assign "00540000002LWA0AAO"
        for( Account Acct : Trigger.new ) {
            if( Acct.Salesman_Number__c == User.Salesman_Number__c) Acct.OwnerID = User.ID;
            else Acct.OwnerID = '00540000002LWA0AAO';
        }
    }

Best Answer chosen by Kevin Tullos
shiv@SFDCshiv@SFDC
Set<String> salesmanIdSet = new Set<String>();
	for(Account act : Trigger.new){
	
		if(act.Salesman_Number__c != NULL){
		
			salesmanIdSet.add(act.Salesman_Number__c) ;
		}
	}
	
	Map<String,User> salesmanIdToUser = new Map<String,User>();
	
	for(User u : [SELECT Id,Name, Salesman_Number__c FROM User WHERE Salesman_Number__c IN : salesmanIdSet])
	{
		salesmanIdToUser.put(u.Salesman_Number__c, u) ;
	}
	
	List<User> defaultUser = [SELECT Id, Name FROM User WHERE Username = 'ABC@XYZ.com' LIMIT 1] ; // Provide user name on ABC@XYZ which you want as defaultUser if salesmanId not found.
	
	for(Account act : Trigger.new)
	{
		if(salesmanIdToUser.get(act.Salesman_Number__c) != NULL){
			act.OwnerID = salesmanIdToUser.get(act.Salesman_Number__c).Id ;
		}else if(defaultUser.size() > 0) {
			act.OwnerID = defaultUser[0].Id ;
		}
		
	}

All Answers

shiv@SFDCshiv@SFDC
Please try this code
Set<String> salesmanIdSet = new Set<String>();
for(Account act : Trigger.new){

  if(act.Salesman_Number__c != NULL){
 
   salesmanIdSet.add(act.Salesman_Number__c) ;
  }
}

Map<String,User> salesmanIdToUser = new Map<String,User>();

for(User u : SELECT Id,Name FROM User WHERE Salesman_Number__c IN : salesmanIdSet)
{
  salesmanIdToUser.put(u.Salesman_Number__c, u) ;
}

for(Account act : Trigger.new)
{
  if(salesmanIdToUser.get(act.Salesman_Number__c) != NULL){
   act.OwnerID = salesmanIdToUser.get(act.Salesman_Number__c).Id ;
  }else {
   act.OwnerID = '00540000002LWA0AAO' ;
  }
 
}

If this answer resolve your issue please mark as solution
shiv@SFDCshiv@SFDC
More generic
Set<String> salesmanIdSet = new Set<String>();
	for(Account act : Trigger.new){
	
		if(act.Salesman_Number__c != NULL){
		
			salesmanIdSet.add(act.Salesman_Number__c) ;
		}
	}
	
	Map<String,User> salesmanIdToUser = new Map<String,User>();
	
	for(User u : SELECT Id,Name FROM User WHERE Salesman_Number__c IN : salesmanIdSet)
	{
		salesmanIdToUser.put(u.Salesman_Number__c, u) ;
	}
	
	List<User> defaultUser = [SELECT Id, Name FROM User WHERE Username = 'ABC@XYZ.com' LIMIT 1] ; // Provide user name on ABC@XYZ which you want as defaultUser if salesmanId not found.
	
	for(Account act : Trigger.new)
	{
		if(salesmanIdToUser.get(act.Salesman_Number__c) != NULL){
			act.OwnerID = salesmanIdToUser.get(act.Salesman_Number__c).Id ;
		}else if(defaultUser.size() > 0) {
			act.OwnerID = defaultUser[0].Id ;
		}
		
	}


Kevin TullosKevin Tullos
Error: Compile Error: unexpected token: 'SELECT' at line 14 column 13

This would be the 12th line of your code.
shiv@SFDCshiv@SFDC
Set<String> salesmanIdSet = new Set<String>();
	for(Account act : Trigger.new){
	
		if(act.Salesman_Number__c != NULL){
		
			salesmanIdSet.add(act.Salesman_Number__c) ;
		}
	}
	
	Map<String,User> salesmanIdToUser = new Map<String,User>();
	
	for(User u : [SELECT Id,Name FROM User WHERE Salesman_Number__c IN : salesmanIdSet])
	{
		salesmanIdToUser.put(u.Salesman_Number__c, u) ;
	}
	
	List<User> defaultUser = [SELECT Id, Name FROM User WHERE Username = 'ABC@XYZ.com' LIMIT 1] ; // Provide user name on ABC@XYZ which you want as defaultUser if salesmanId not found.
	
	for(Account act : Trigger.new)
	{
		if(salesmanIdToUser.get(act.Salesman_Number__c) != NULL){
			act.OwnerID = salesmanIdToUser.get(act.Salesman_Number__c).Id ;
		}else if(defaultUser.size() > 0) {
			act.OwnerID = defaultUser[0].Id ;
		}
		
	}

shiv@SFDCshiv@SFDC
Please check now. 
Kevin TullosKevin Tullos
That compiled, but there is a snag on row 14,1.  Thanks again!!

Here is the error...

Error: Invalid Data.
Review all error messages below to correct your data.
Apex trigger Update_Account_Owner caused an unexpected exception, contact your administrator: Update_Account_Owner: execution of BeforeUpdate caused by: System.SObjectException: SObject row was retrieved via SOQL without querying the requested field: User.Salesman_Number__c: Trigger.Update_Account_Owner: line 16, column 1
shiv@SFDCshiv@SFDC
Set<String> salesmanIdSet = new Set<String>();
	for(Account act : Trigger.new){
	
		if(act.Salesman_Number__c != NULL){
		
			salesmanIdSet.add(act.Salesman_Number__c) ;
		}
	}
	
	Map<String,User> salesmanIdToUser = new Map<String,User>();
	
	for(User u : [SELECT Id,Name, Salesman_Number__c FROM User WHERE Salesman_Number__c IN : salesmanIdSet])
	{
		salesmanIdToUser.put(u.Salesman_Number__c, u) ;
	}
	
	List<User> defaultUser = [SELECT Id, Name FROM User WHERE Username = 'ABC@XYZ.com' LIMIT 1] ; // Provide user name on ABC@XYZ which you want as defaultUser if salesmanId not found.
	
	for(Account act : Trigger.new)
	{
		if(salesmanIdToUser.get(act.Salesman_Number__c) != NULL){
			act.OwnerID = salesmanIdToUser.get(act.Salesman_Number__c).Id ;
		}else if(defaultUser.size() > 0) {
			act.OwnerID = defaultUser[0].Id ;
		}
		
	}

This was selected as the best answer
shiv@SFDCshiv@SFDC
sorry I forgot to include that filed in my query. I have added that field now..

Kevin TullosKevin Tullos
That worked perfectly!!  Thank you so much for your help.  Do you have any thing that I can read on how I can improve my skills in writing triggers, Vforce and soql query?
Tanmay SahaiTanmay Sahai
Hi @shiv,

Using the same logic that you provided for the account owner change, can you help me with the trigger code to change the Account owner to a default user when the LastActivitydate is more than 30 days.

I have a custom field on Account: User_Last_Activity__c which gives me the #of days since last activity (calulated using a formula using the standard system field: LastActivityDate). So, I want to write a trigger to chage the ownership of the Account to a default user when the value of User_Last_Activity__c > 30 days. Do I need to write a scheduler class as well.

Thanks!