+ Start a Discussion
btran3btran3 

Trigger for Opportunity Type

I would like to make a trigger for after an opportunity is created.  I want it so that when an opportunity is created the trigger checks if the opportunity owner's role is either 'success' or 'sales'.  If the owner's role is sales the trigger will then update the opportunity type to 'initial sale' or if the owner's role is success the the type will be 'add/subtract'.  Below is my code, even though I know it is completely wrong I thought I'd share.

 

trigger myOpptType on Opportunity (after insert)
{
{
Opportunity myOpptType = trigger.new[0];
if (myOpptType.Owner = Matthew Wolach)
{
myOpptType.Type = 'Add/Subtract';
update myOpptType;
}
}
}

 

thanks for the help!

Best Answer chosen by Admin (Salesforce Developers) 
JBabuJBabu

I have modified the code and created a sample one for you. Hopefully it works.

 

trigger myOpptType on Opportunity (after insert)
{

 

List<Opportunity> oppList = new List<Opportunity>();

 

  for(Opportunity opp : trigger.New) {

       if( opp.owner.userrole.name == 'success')  {

             opp.type = 'add/subtract';

             oppList.add(opp);

       }

      if( opp.owner.userrole.name == 'sales')  {

              opp.type = 'initial success';

              oppList.add(opp);

     }

  }
   if(oppList.size() > 0 ) {

     Database.update(oppList, false);

   }
}

 

Thanks,

Babu.

All Answers

JBabuJBabu

I have modified the code and created a sample one for you. Hopefully it works.

 

trigger myOpptType on Opportunity (after insert)
{

 

List<Opportunity> oppList = new List<Opportunity>();

 

  for(Opportunity opp : trigger.New) {

       if( opp.owner.userrole.name == 'success')  {

             opp.type = 'add/subtract';

             oppList.add(opp);

       }

      if( opp.owner.userrole.name == 'sales')  {

              opp.type = 'initial success';

              oppList.add(opp);

     }

  }
   if(oppList.size() > 0 ) {

     Database.update(oppList, false);

   }
}

 

Thanks,

Babu.

This was selected as the best answer
btran3btran3

Thank you for your help!

 

I put your code in but it doesn't seem to populate the type when I create an opportunity by either a success or sales rep.  Do you have any suggestions?

JBabuJBabu

Hi,

 

It might not be satisfying the "if" conditions make sure role name is same.

 

Thanks,

Babu.