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
Garlot22Garlot22 

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,

 

raseshtcsraseshtcs

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

Bhawani SharmaBhawani Sharma
In trigger context, logged in user will be only the creator. So you can use following statement. I don't see any issue with this:

opp.Ownerid = UserInfo.getUserId();
Garlot22Garlot22

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

 

trigger setOpportunityOwner on Opportunity (before insert) {
    for (Opportunity opp : trigger.new) {
    
            // Has Owner changed?
            if (Opp.OwnerID != Opp.CreatedById) {
                 Opp.OwnerID = UserInfo.getUserId();
            }
        }
    }

 I tried both before and after insert however neither seems to be working.  

 

 

 

Bhawani SharmaBhawani Sharma

Don't use 

 

if (Opp.OwnerID != Opp.CreatedById) {

condition. This is before insert trigger, so CreatedById and OwnerId is not set yet.

 

 

Garlot22Garlot22

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. 

 

trigger setOpportunityOwner on Opportunity (before insert) {
    for (Opportunity opp : trigger.new) {
            //Opp.OwnerID = '005i0000000ZZbh';
            Opp.OwnerID = UserInfo.getUserId();
        }
    }

 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. 

 

 

Bhawani SharmaBhawani Sharma
Can you put a debug statement in trigger and share what it is displaying?
Garlot22Garlot22

I'm not positve that I did this correctly.  Here is my trigger.

 

trigger setOpportunityOwner on Opportunity (before insert) {
    for (Opportunity opp : trigger.new) {
    system.debug('Test');
            Opp.OwnerID = UserInfo.getUserId();
        }
    }

 

And here is the debug log

 

 

27.0 APEX_CODE,DEBUG;APEX_PROFILING,INFO;CALLOUT,INFO;DB,INFO;SYSTEM,DEBUG;VALIDATION,INFO;VISUALFORCE,INFO;WORKFLOW,INFO
10:36:07.329 (329525000)|CODE_UNIT_STARTED|[EXTERNAL]|01qe000000006UR|setOpportunityOwner on Opportunity trigger event BeforeInsert for [new]
10:36:07.330 (330858000)|USER_DEBUG|[3]|DEBUG|Test
10:36:08.063 (331118000)|CUMULATIVE_LIMIT_USAGE
10:36:08.063|LIMIT_USAGE_FOR_NS|(default)|
  Number of SOQL queries: 0 out of 100
  Number of query rows: 0 out of 50000
  Number of SOSL queries: 0 out of 20
  Number of DML statements: 0 out of 150
  Number of DML rows: 0 out of 10000
  Number of code statements: 2 out of 200000
  Maximum heap size: 0 out of 6000000
  Number of callouts: 0 out of 10
  Number of Email Invocations: 0 out of 10
  Number of fields describes: 0 out of 100
  Number of record type describes: 0 out of 100
  Number of child relationships describes: 0 out of 100
  Number of picklist describes: 0 out of 100
  Number of future calls: 0 out of 10

10:36:08.063|CUMULATIVE_LIMIT_USAGE_END

10:36:07.331 (331315000)|CODE_UNIT_FINISHED|setOpportunityOwner on Opportunity trigger event BeforeInsert for [new]
10:36:07.541 (541774000)|CODE_UNIT_STARTED|[EXTERNAL]|01qe000000006Mc|Leads on Lead trigger event AfterUpdate for [00Qe0000001Ltq6]
10:36:07.542 (542213000)|METHOD_ENTRY|[1]|01pe00000000Rlk|Leads.Leads()
10:36:07.542 (542232000)|METHOD_EXIT|[1]|Leads
10:36:07.542 (542273000)|CONSTRUCTOR_ENTRY|[4]|01pe00000000Rlk|<init>()
10:36:07.542 (542342000)|CONSTRUCTOR_EXIT|[4]|01pe00000000Rlk|<init>()
10:36:07.555 (555531000)|METHOD_ENTRY|[5]|01pe00000000Rlk|Leads.SetContactRoleDefaults(LIST<Lead>, MAP<Id,Lead>)
10:36:07.555 (555623000)|SYSTEM_CONSTRUCTOR_ENTRY|[7]|<init>(Integer)
10:36:07.555 (555660000)|SYSTEM_CONSTRUCTOR_EXIT|[7]|<init>(Integer)
10:36:07.555 (555680000)|SYSTEM_METHOD_ENTRY|[10]|LIST<Lead>.iterator()
10:36:07.555 (555713000)|SYSTEM_METHOD_EXIT|[10]|LIST<Lead>.iterator()
10:36:07.555 (555729000)|SYSTEM_METHOD_ENTRY|[10]|system.ListIterator.hasNext()
10:36:07.555 (555752000)|SYSTEM_METHOD_EXIT|[10]|system.ListIterator.hasNext()
10:36:07.555 (555894000)|SYSTEM_METHOD_ENTRY|[11]|MAP<Id,Lead>.get(Object)
10:36:07.555 (555933000)|SYSTEM_METHOD_EXIT|[11]|MAP<Id,Lead>.get(Object)
10:36:07.556 (556023000)|SYSTEM_METHOD_ENTRY|[13]|SET<Id>.add(Object)
10:36:07.556 (556064000)|SYSTEM_METHOD_EXIT|[13]|SET<Id>.add(Object)
10:36:07.556 (556075000)|SYSTEM_METHOD_ENTRY|[10]|system.ListIterator.hasNext()
10:36:07.556 (556088000)|SYSTEM_METHOD_EXIT|[10]|system.ListIterator.hasNext()
10:36:07.556 (556110000)|SYSTEM_CONSTRUCTOR_ENTRY|[19]|<init>()
10:36:07.556 (556134000)|SYSTEM_CONSTRUCTOR_EXIT|[19]|<init>()
10:36:07.556 (556536000)|SOQL_EXECUTE_BEGIN|[20]|Aggregations:0|select Id, IsPrimary, Role from OpportunityContactRole 
10:36:07.560 (560255000)|SOQL_EXECUTE_END|[20]|Rows:1
10:36:07.560 (560333000)|SYSTEM_METHOD_ENTRY|[20]|Database.QueryLocator.iterator()
10:36:07.560 (560460000)|SYSTEM_METHOD_EXIT|[20]|Database.QueryLocator.iterator()
10:36:07.560 (560478000)|SYSTEM_METHOD_ENTRY|[20]|Database.QueryLocatorIterator.hasNext()
10:36:07.560 (560501000)|SYSTEM_METHOD_ENTRY|[33]|LIST<OpportunityContactRole>.size()
10:36:07.560 (560533000)|SYSTEM_METHOD_EXIT|[33]|LIST<OpportunityContactRole>.size()
10:36:07.560 (560543000)|SYSTEM_METHOD_EXIT|[20]|Database.QueryLocatorIterator.hasNext()
10:36:07.560 (560553000)|SYSTEM_METHOD_ENTRY|[20]|Database.QueryLocatorIterator.next()
10:36:07.560 (560566000)|SYSTEM_METHOD_ENTRY|[48]|Database.QueryLocatorIterator.hasNext()
10:36:07.560 (560589000)|SYSTEM_METHOD_ENTRY|[33]|LIST<OpportunityContactRole>.size()
10:36:07.560 (560602000)|SYSTEM_METHOD_EXIT|[33]|LIST<OpportunityContactRole>.size()
10:36:07.560 (560610000)|SYSTEM_METHOD_EXIT|[48]|Database.QueryLocatorIterator.hasNext()
10:36:07.560 (560642000)|SYSTEM_METHOD_EXIT|[20]|Database.QueryLocatorIterator.next()
10:36:07.560 (560732000)|SYSTEM_METHOD_ENTRY|[23]|LIST<OpportunityContactRole>.add(Object)
10:36:07.560 (560767000)|SYSTEM_METHOD_EXIT|[23]|LIST<OpportunityContactRole>.add(Object)
10:36:07.560 (560776000)|SYSTEM_METHOD_ENTRY|[20]|Database.QueryLocatorIterator.hasNext()
10:36:07.560 (560788000)|SYSTEM_METHOD_EXIT|[20]|Database.QueryLocatorIterator.hasNext()
10:36:07.560 (560798000)|SYSTEM_METHOD_ENTRY|[26]|LIST<OpportunityContactRole>.size()
10:36:07.560 (560822000)|SYSTEM_METHOD_EXIT|[26]|LIST<OpportunityContactRole>.size()
10:36:07.560 (560875000)|DML_BEGIN|[27]|Op:Update|Type:OpportunityContactRole|Rows:1
10:36:07.572 (572811000)|DML_END|[27]
10:36:07.572 (572862000)|METHOD_EXIT|[5]|01pe00000000Rlk|Leads.SetContactRoleDefaults(LIST<Lead>, MAP<Id,Lead>)
10:36:08.305 (572893000)|CUMULATIVE_LIMIT_USAGE
10:36:08.305|LIMIT_USAGE_FOR_NS|(default)|
  Number of SOQL queries: 1 out of 100
  Number of query rows: 1 out of 50000
  Number of SOSL queries: 0 out of 20
  Number of DML statements: 1 out of 150
  Number of DML rows: 1 out of 10000
  Number of code statements: 11 out of 200000
  Maximum heap size: 0 out of 6000000
  Number of callouts: 0 out of 10
  Number of Email Invocations: 0 out of 10
  Number of fields describes: 0 out of 100
  Number of record type describes: 0 out of 100
  Number of child relationships describes: 0 out of 100
  Number of picklist describes: 0 out of 100
  Number of future calls: 0 out of 10

10:36:08.305|CUMULATIVE_LIMIT_USAGE_END

10:36:07.572 (572943000)|CODE_UNIT_FINISHED|Leads on Lead trigger event AfterUpdate for [00Qe0000001Ltq6]
Bhawani SharmaBhawani Sharma

Which owner is getting stamped when opportunity is being created?

Garlot22Garlot22

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.  

Bhawani SharmaBhawani Sharma
Which owner you are seeing on Opportunity record after being created?
Bhawani SharmaBhawani Sharma
Please note the following when working with triggers:
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.
Garlot22Garlot22

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.

 

 

Bhawani SharmaBhawani Sharma
Can you put this statement in trigger:

System.debug('User:::::'+ UserInfo.getUserId());
Bhawani SharmaBhawani Sharma
Also please share the debug results.
Garlot22Garlot22
27.0 APEX_CODE,DEBUG;APEX_PROFILING,INFO;CALLOUT,INFO;DB,INFO;SYSTEM,DEBUG;VALIDATION,INFO;VISUALFORCE,INFO;WORKFLOW,INFO
11:20:38.331 (331487000)|CODE_UNIT_STARTED|[EXTERNAL]|01qe000000006UR|setOpportunityOwner on Opportunity trigger event BeforeInsert for [new]
11:20:38.333 (333313000)|USER_DEBUG|[3]|DEBUG|User:::::005i0000000Z3VYAA0
11:20:39.107 (333541000)|CUMULATIVE_LIMIT_USAGE
11:20:39.107|LIMIT_USAGE_FOR_NS|(default)|
  Number of SOQL queries: 0 out of 100
  Number of query rows: 0 out of 50000
  Number of SOSL queries: 0 out of 20
  Number of DML statements: 0 out of 150
  Number of DML rows: 0 out of 10000
  Number of code statements: 2 out of 200000
  Maximum heap size: 0 out of 6000000
  Number of callouts: 0 out of 10
  Number of Email Invocations: 0 out of 10
  Number of fields describes: 0 out of 100
  Number of record type describes: 0 out of 100
  Number of child relationships describes: 0 out of 100
  Number of picklist describes: 0 out of 100
  Number of future calls: 0 out of 10

11:20:39.107|CUMULATIVE_LIMIT_USAGE_END

11:20:38.333 (333792000)|CODE_UNIT_FINISHED|setOpportunityOwner on Opportunity trigger event BeforeInsert for [new]
11:20:38.735 (735852000)|CODE_UNIT_STARTED|[EXTERNAL]|01qe000000006Mc|Leads on Lead trigger event AfterUpdate for [00Qe0000001LtrY]
11:20:38.736 (736252000)|METHOD_ENTRY|[1]|01pe00000000Rlk|Leads.Leads()
11:20:38.736 (736272000)|METHOD_EXIT|[1]|Leads
11:20:38.736 (736311000)|CONSTRUCTOR_ENTRY|[4]|01pe00000000Rlk|<init>()
11:20:38.736 (736372000)|CONSTRUCTOR_EXIT|[4]|01pe00000000Rlk|<init>()
11:20:38.737 (737640000)|METHOD_ENTRY|[5]|01pe00000000Rlk|Leads.SetContactRoleDefaults(LIST<Lead>, MAP<Id,Lead>)
11:20:38.737 (737716000)|SYSTEM_CONSTRUCTOR_ENTRY|[7]|<init>(Integer)
11:20:38.737 (737764000)|SYSTEM_CONSTRUCTOR_EXIT|[7]|<init>(Integer)
11:20:38.737 (737789000)|SYSTEM_METHOD_ENTRY|[10]|LIST<Lead>.iterator()
11:20:38.737 (737828000)|SYSTEM_METHOD_EXIT|[10]|LIST<Lead>.iterator()
11:20:38.737 (737852000)|SYSTEM_METHOD_ENTRY|[10]|system.ListIterator.hasNext()
11:20:38.737 (737878000)|SYSTEM_METHOD_EXIT|[10]|system.ListIterator.hasNext()
11:20:38.738 (738030000)|SYSTEM_METHOD_ENTRY|[11]|MAP<Id,Lead>.get(Object)
11:20:38.738 (738084000)|SYSTEM_METHOD_EXIT|[11]|MAP<Id,Lead>.get(Object)
11:20:38.738 (738208000)|SYSTEM_METHOD_ENTRY|[13]|SET<Id>.add(Object)
11:20:38.738 (738260000)|SYSTEM_METHOD_EXIT|[13]|SET<Id>.add(Object)
11:20:38.738 (738279000)|SYSTEM_METHOD_ENTRY|[10]|system.ListIterator.hasNext()
11:20:38.738 (738299000)|SYSTEM_METHOD_EXIT|[10]|system.ListIterator.hasNext()
11:20:38.738 (738331000)|SYSTEM_CONSTRUCTOR_ENTRY|[19]|<init>()
11:20:38.738 (738358000)|SYSTEM_CONSTRUCTOR_EXIT|[19]|<init>()
11:20:38.738 (738827000)|SOQL_EXECUTE_BEGIN|[20]|Aggregations:0|select Id, IsPrimary, Role from OpportunityContactRole 
11:20:38.741 (741823000)|SOQL_EXECUTE_END|[20]|Rows:1
11:20:38.741 (741912000)|SYSTEM_METHOD_ENTRY|[20]|Database.QueryLocator.iterator()
11:20:38.742 (742043000)|SYSTEM_METHOD_EXIT|[20]|Database.QueryLocator.iterator()
11:20:38.742 (742068000)|SYSTEM_METHOD_ENTRY|[20]|Database.QueryLocatorIterator.hasNext()
11:20:38.742 (742103000)|SYSTEM_METHOD_ENTRY|[33]|LIST<OpportunityContactRole>.size()
11:20:38.742 (742140000)|SYSTEM_METHOD_EXIT|[33]|LIST<OpportunityContactRole>.size()
11:20:38.742 (742158000)|SYSTEM_METHOD_EXIT|[20]|Database.QueryLocatorIterator.hasNext()
11:20:38.742 (742174000)|SYSTEM_METHOD_ENTRY|[20]|Database.QueryLocatorIterator.next()
11:20:38.742 (742197000)|SYSTEM_METHOD_ENTRY|[48]|Database.QueryLocatorIterator.hasNext()
11:20:38.742 (742230000)|SYSTEM_METHOD_ENTRY|[33]|LIST<OpportunityContactRole>.size()
11:20:38.742 (742249000)|SYSTEM_METHOD_EXIT|[33]|LIST<OpportunityContactRole>.size()
11:20:38.742 (742264000)|SYSTEM_METHOD_EXIT|[48]|Database.QueryLocatorIterator.hasNext()
11:20:38.742 (742311000)|SYSTEM_METHOD_EXIT|[20]|Database.QueryLocatorIterator.next()
11:20:38.742 (742429000)|SYSTEM_METHOD_ENTRY|[23]|LIST<OpportunityContactRole>.add(Object)
11:20:38.742 (742472000)|SYSTEM_METHOD_EXIT|[23]|LIST<OpportunityContactRole>.add(Object)
11:20:38.742 (742488000)|SYSTEM_METHOD_ENTRY|[20]|Database.QueryLocatorIterator.hasNext()
11:20:38.742 (742512000)|SYSTEM_METHOD_EXIT|[20]|Database.QueryLocatorIterator.hasNext()
11:20:38.742 (742536000)|SYSTEM_METHOD_ENTRY|[26]|LIST<OpportunityContactRole>.size()
11:20:38.742 (742569000)|SYSTEM_METHOD_EXIT|[26]|LIST<OpportunityContactRole>.size()
11:20:38.742 (742633000)|DML_BEGIN|[27]|Op:Update|Type:OpportunityContactRole|Rows:1
11:20:38.752 (752745000)|DML_END|[27]
11:20:38.752 (752826000)|METHOD_EXIT|[5]|01pe00000000Rlk|Leads.SetContactRoleDefaults(LIST<Lead>, MAP<Id,Lead>)
11:20:39.526 (752856000)|CUMULATIVE_LIMIT_USAGE
11:20:39.526|LIMIT_USAGE_FOR_NS|(default)|
  Number of SOQL queries: 1 out of 100
  Number of query rows: 1 out of 50000
  Number of SOSL queries: 0 out of 20
  Number of DML statements: 1 out of 150
  Number of DML rows: 1 out of 10000
  Number of code statements: 11 out of 200000
  Maximum heap size: 0 out of 6000000
  Number of callouts: 0 out of 10
  Number of Email Invocations: 0 out of 10
  Number of fields describes: 0 out of 100
  Number of record type describes: 0 out of 100
  Number of child relationships describes: 0 out of 100
  Number of picklist describes: 0 out of 100
  Number of future calls: 0 out of 10

11:20:39.526|CUMULATIVE_LIMIT_USAGE_END

11:20:38.752 (752939000)|CODE_UNIT_FINISHED|Leads on Lead trigger event AfterUpdate for [00Qe0000001LtrY]
Bhawani SharmaBhawani Sharma

See the debug log result

 

USER_DEBUG|[3]|DEBUG|User:::::005i0000000Z3VYAA0

trigger is working fine. Looks like, you have some other processwhich is changing the owner id.

 

Garlot22Garlot22

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,

Bhawani SharmaBhawani Sharma

URL is displaying 15 charater id, while debug log is 18 character. but both are same. 

Garlot22Garlot22

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.

 

 

Bhawani SharmaBhawani Sharma
Can you try in in Lead after update trigger?

Opportunity opp = new Opportunity(Id = lead.OpportunityId, OwnerId = UserInfo.getUserId());
Bhawani SharmaBhawani Sharma
Alaso make sure to update the opportunity;
Garlot22Garlot22

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.  

Bhawani SharmaBhawani Sharma
Yes, I am saying the same. Create a after update trigger on Lead and check if

if(lead.IsConverted) {
Opportunity opp = new Opportunity(Id = lead.OpportunityId, OwnerId = UserInfo.getUserId());
}

and update opportunity.
Garlot22Garlot22

So, this may be off but here is what I tried.  lead.opporunityID does not appear to be a valid field.

 

trigger setOpportunityOwner on Lead (after update) {
          for (lead lead: trigger.new){
            if(lead.IsConverted) {
            Opportunity opp = new Opportunity(Id = lead.OpportunityId, OwnerId = UserInfo.getUserId());
            update opp;
            }
        }
    }

 error message

Error: Compile Error: Invalid field OpportunityId for SObject Lead at line 4 column 52

Bhawani SharmaBhawani Sharma
Sorry, correct field name is ConvertedOpportunityId, so you should access it with
lead.ConvertedOpportunityId
Garlot22Garlot22

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.

 

 

Garlot22Garlot22

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.

 

trigger setOpportunityOwner on Opportunity (after insert) {
          for (Opportunity opp: trigger.new){
          System.debug('User:::::'+ UserInfo.getUserId());
            if (Opp.OwnerID != Opp.CreatedById) {
            opp.OwnerId = Opp.CreatedById;
            update opp;
            }
        }
    }

 

Bhawani SharmaBhawani Sharma
Can you send a screen-shot or the opportunity record with CreatedBy and OwnerId populated
Garlot22Garlot22

I'm not sure how to send anything here and it won't allow me to insert images.

Garlot22Garlot22
Let me know if this works.
Bhawani SharmaBhawani Sharma
This time my suggestion would be , please create case in Salesforce and check the issue with them.