• Garlot22
  • NEWBIE
  • 0 Points
  • Member since 2013

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 14
    Replies

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,

 

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,