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

Help? Trigger to insert user id when oppty changed from particular stage
We change the owner of our opportunities when the owner changes from one certain stage to another, but need to have the previous owner for a report. I want to put the name of the previous owner in a custom lookup field that I created when the stage changes from "App In" to something else. I'm really a beginner with triggers, can anyone help? I've tried doing it with workflows and it doesn't allow me to do it. I'm thinking I need an apex trigger.
Any ideas?
Yeah, it should be before update.
All Answers
Shoudl be something like this:
Sort of in a hurry, so didn't have a chance to test or check for types. Let me know if it throws any errors.
It doesn't seem to like the line:
opp.App_Rep__c == Trigger.oldMap.get(opp.ID).OwnerID;
Compile error: Expression cannot be a statement at line 6 column 10
Yeah, that line should be an assignment, not a comparison. Use only one equal sign.
Thanks again.
Compiled ok but then when I ran a test record it gave me this error:
Error:Apex trigger OppUpdatePriorOwner caused an unexpected exception, contact your administrator: OppUpdatePriorOwner: execution of AfterUpdate
caused by: System.FinalException: Record is read-only: Trigger.OppUpdatePriorOwner: line 6, column 1
Not sure why it says the record is read-only. It's a normal custom lookup field and I have all the permissions to edit it.
Yeah, it should be before update.
@craigmh, you beat me to it :)
I had the easy part, you did all the grunt work.
Thanks again. Still trouble. Wish I wasn't so clueless and could try something myself, but I'm over my head here.
Error:Apex trigger OppUpdatePriorOwner caused an unexpected exception, contact your administrator: OppUpdatePriorOwner: execution of BeforeUpdate
caused by: System.SObjectException: DML statment cannot operate on trigger.new or trigger.old: Trigger.OppUpdatePriorOwner: line 11, column 1
You shouldn't need anything more than what I posted. Make sure that you remove the update statement.
In a before insert/update trigger, any changes made to the sObject collection are automatically committed when the object(s) are inserted/updated.
Right. Working great now. Not sure what I had wrong.
Thanks so much for your help!