+ Start a Discussion

Auto lead conversion trigger.How to insert account(lookup)field on opportunity record.

i want to  insert opportunity record(custom) when auto lead conversion trigger process.How to insert account(lookup)field on opportunity record.here is my trigger.please guide me


trigger autoconversion on Lead (after insert) 
  Lead ld=Trigger.new[0];  
  if( ld.Delivery_Mode__c == 'Pickup' || ld.Car_donation__c == true || (ld.recordtype.name=='Money'&& ld.Amount__c>=1000)  )
      //****  here lead conversion logic 
      Database.LeadConvert lc = new database.LeadConvert();
      lc.setDoNotCreateOpportunity(TRUE); //**IMPORTANT METHOD HERE**
      LeadStatus convertStatus = [SELECT Id, MasterLabel FROM LeadStatus WHERE IsConverted=true LIMIT 1];
      Database.LeadConvertResult lcr = Database.convertLead(lc);
      //** create oppt record

opportunity opp =new opportunity ();



opp.accoundid=???                          //here how to get account name (lookup)
      insert opp;




If lead is already converted, it will populate ConvertedAccountId field on Lead. You can use this. If your trigger is working, you can use below to populate AccountId on Opportunity record.


opp.AccountId = ld.ConvertedAccountId;


Happy to help you!

gAuTaM sInGh_on cLoUdsgAuTaM sInGh_on cLoUds

Hi , 


trigger autoconversion on Lead (after insert) {
Lead ld = Trigger.new[0]; 
if( ld.LastName == 'Gautam')
//**** here lead conversion logic 
Database.LeadConvert lc = new database.LeadConvert();
lc.setDoNotCreateOpportunity(FALSE); //**IMPORTANT METHOD HERE**
LeadStatus convertStatus = [SELECT Id, MasterLabel FROM LeadStatus WHERE IsConverted= true LIMIT 1];
Database.LeadConvertResult lcr = Database.convertLead(lc);



When a Lead is converted .. a 'NEW' Contact , Account and an Opportunity is created.
So when the above code runs , you will find that the value entered in Company Field is the Account Name for the newly account created.
Its Id is stored in 'ConvertedAccountId' .  Also , The new opportunity will have the the same account in the Lookup Field populated.



Hope this fulfills your requirement. 

Important :

Click on the Star Icon aside if this post provides you with useful information and if this is what you where looking for then please mark it as a solution for others benefits.

Thank You