You need to sign in to do that
Don't have an account?
Garlot22
Trigger to set opportunity owner to created by
I'm new to triggers and apex and am attempting to automatically set the Opportunity Owner based on the user who converts the lead to an opportunity. I've tried several different things but have been unsuccessful thus far.
//This gives me an error because opp.CreatedById is null and the Opp.OwnerID cannot be set to a null value //This works when setting it to a default value which i've currently commented out but that doesn't accomplish what i'm trying to do trigger setOpportunityOwner on Opportunity (before insert) { for (Opportunity opp : trigger.new) { if (Opp.OwnerID != Opp.CreatedById) { //Opp.OwnerID = '005i0000000ZZbh'; Opp.OwnerID = Opp.CreatedById; } } }
Assuming that this needs to be done after the insert i've taken a different approach and this doesn't give me an error but it doesn't seem to work either.
Trigger setOpportunityOwner on Opportunity (after insert) { for (Opportunity opp : trigger.new) { if (Opp.OwnerID != Opp.CreatedById) { Opp.OwnerID = Opp.CreatedById; } } }
Can someone please offer any advice?
Thanks,
Try using a workflow rule instead of a trigger. In the case of after insert you would need to update the opportunity record. But I would still suggest to use a workflow rule
opp.Ownerid = UserInfo.getUserId();
Raseshtcs - I don't believe a workflow will work in this case because it doesn't give me the option to use created_by. I had originally tried that approach.
Bhawani - I tried what you suggested and it seems to compile with no errors however it doesn't seem to be working. Here is what I did and perhaps i'm missing something. I created the trigger at the following location Setup->Customize->Opportunities->Triggers
I tried both before and after insert however neither seems to be working.
Don't use
condition. This is before insert trigger, so CreatedById and OwnerId is not set yet.
Thanks for all your help thus far. I actually tried that after your first suggestion and same result? Just an fyi, I don't think this would cause any issues but I'm testing in the sandbox.
It works if I put a user as the default (see commented out) however when I use the UserInfo.getUserID() it doesn't seem to work.
I'm not positve that I did this correctly. Here is my trigger.
And here is the debug log
Which owner is getting stamped when opportunity is being created?
Thanks again for your help thus far I really appreciate it. It doesn't appear that anything is changing. My end goal is to set the opportunity owner to the user who is converting the lead but that doesn't seem to be working.
When Enable Validation and Triggers from Lead Convert is selected, if the lead conversion creates an opportunity and the opportunity has Apex before triggers associated with it, the triggers run immediately after the opportunity is created, before the opportunity contact role is created. For more information, see “Customizing Lead Settings” in the Salesforce online help.
I apologize perhaps I should have stated that in the beginning. We're automatically inserting leads into our system and they are using the default admin account. Therefore the lead owner is set to our admin account. When the lead is converted to an opportunity it is still displaying our default admin account.
System.debug('User:::::'+ UserInfo.getUserId());
See the debug log result
trigger is working fine. Looks like, you have some other processwhich is changing the owner id.
Any thoughts on what type of process may be changing it? I've looked through the settings and don't see any custom objects which would be changing that. Also, the user id is displayed as 005i0000000Z3VY in the URL. The debug appears to have a suffix of 'AA0'. I'm not sure where else to go with this.
Thanks again,
URL is displaying 15 charater id, while debug log is 18 character. but both are same.
Good to know about the user id. Any thoughts on what processes may be setting this back to admin or interfering with the trigger? Our installation is fairly new therefore we haven't created many triggers or workflows.
Opportunity opp = new Opportunity(Id = lead.OpportunityId, OwnerId = UserInfo.getUserId());
I'm a little confused as to what you're suggesting. Are you suggesting to create a trigger under Setup->Customize->Leads->Triggers?
If so, I'm not exactly sure how to format it and I keep getting compile errors.
if(lead.IsConverted) {
Opportunity opp = new Opportunity(Id = lead.OpportunityId, OwnerId = UserInfo.getUserId());
}
and update opportunity.
So, this may be off but here is what I tried. lead.opporunityID does not appear to be a valid field.
error message
Error: Compile Error: Invalid field OpportunityId for SObject Lead at line 4 column 52
lead.ConvertedOpportunityId
As you previously mentioned. There must be some other process which is preventing this from happening or setting the opportunity owner back to admin. I did as you suggested and it compiles fine. When testing I receive no errrors however it doesn't set the opportunity owner as expected.
I appreciate all your help but I'm not sure what else may be breaking this.
What are your thoughts on after insert for opportunity. Perhaps something like this. It seems to compile fine as well but I doesn't appear to update the opportunity owner.
I'm not sure how to send anything here and it won't allow me to insert images.