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
VK86VK86 

Trigger on Opportunity to create new leads

Hello all,

 

I need to create two new leads when an Opportunity is closed-won and map over some date from Opp to the new leads.

I am a newbie to Apex programming and any pointers how to do this is really appreciated.

 

Thanks much,

VK86

Best Answer chosen by Admin (Salesforce Developers) 
jkucerajkucera

Is the goal to track renewals?  Why not create another oppty instead?

 

If you go the lead route, you want to create an oppty trigger that will add the info required.

 

trigger createLeads on opportunity(before update){
  List<Lead> leads = new List<lead>();
  for (Opportunity o: trigger.new){
    Lead l=new Lead();
    l.LastName='??';
    l.Company=o.Account;
    //...
    leads.add(l);
  }
  try{
    insert leads;
  }catch(exception e){
    system.debug('the leads weren't properly inserted: '+e);
  }
}

 

 

All Answers

ahab1372ahab1372

Definitely bookmark the Apex Developer's Guide:

http://www.salesforce.com/us/developer/docs/apexcode/index.htm

 

I also recommend browsing the documentation start page on developer.force.com, you can learn from the examples in there:

http://wiki.developerforce.com/index.php/Documentation

 

Your trigger will look similar to this (not tested):

 

 

trigger createLeads on Opportunity(before update)
{
  List<lead> newLeadList = new List<Lead>();  //in here you will temporarily store the new leads
  Lead newLead;

  for(Opportunity theOpp:Trigger.New) //more than one opp can enter the trigger at the same time. They all "sit" in Trigger.New, so you have to loop through it
  {
     if(theOpp.StageName == 'Closed-won')
     {
        newLead = new Lead (FirstName = someName, LastName = someName);  //someName will probably reference values from theOpp?
        newLeadList.add(newLead);   //add the new lead to the list
     }
  }
  
  insert newLeadList   //insert the new leads into the database. Never do any DML inside a for loop

}  

 

 

 

VK86VK86

Hello ahab1372,

 

Thanks much for the info.

Will definitely try this out.

 

Thanks,

VK86

jkucerajkucera

Is the goal to track renewals?  Why not create another oppty instead?

 

If you go the lead route, you want to create an oppty trigger that will add the info required.

 

trigger createLeads on opportunity(before update){
  List<Lead> leads = new List<lead>();
  for (Opportunity o: trigger.new){
    Lead l=new Lead();
    l.LastName='??';
    l.Company=o.Account;
    //...
    leads.add(l);
  }
  try{
    insert leads;
  }catch(exception e){
    system.debug('the leads weren't properly inserted: '+e);
  }
}

 

 

This was selected as the best answer
jkucerajkucera

Oops - looks like I was slow on the draw ;)

VK86VK86

Thanks much jkucera & ahab1372!!

 

I was able to successfully run the trigger.

All i want to do now is fire the 'Active Assignment Rule' (i checked it by default but still it is not getting fired)

 

Thanks,

VK86

VK86VK86

Thanks a lot ahab1372!!!

 

Its working now as i wanted.

 

 

Thanks again!