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

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';
}
}
All Answers
If this answer resolve your issue please mark as solution
This would be the 12th line of your code.
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
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!