2

I want to write trigger which can automatically convert leads into account, contact and opportunity.

What I have tried so far:

    trigger ConvertLead on Lead (after insert, after update,before insert,before update) {

        if(trigger.isBefore){
            Map<String, Lead> leadMap = new Map<String, Lead>();
            for (Lead lead : System.Trigger.new) {   

                //We don't treat an email address that isn't changing during an update as a duplicate. 
                if ((lead.Email != null) && (System.Trigger.isInsert || (lead.Email != System.Trigger.oldMap.get(lead.Id).Email))){
                          //Checking whether another new lead isn't also a duplicate  
                          if (leadMap.containsKey(lead.Email)) {
                              lead.Email.addError('Another new lead has the  same email address.');
                          } else {
                              leadMap.put(lead.Email, lead);
                          }
                      }
            }

            list<opportunity> objOpportunity = new list<opportunity>();
            //Finding all the leads in the database that have the same email address as any of the leads being inserted or updated.  
            for (Lead lead : [SELECT Name, Email, IsConverted, ConvertedAccountId, ConvertedContactId, ConvertedOpportunityId FROM Lead WHERE Email IN :leadMap.KeySet()]){
                Lead newLead = leadMap.get(lead.Email);
                Opportunity ObjOpp = new Opportunity();
                ObjOpp.AccountId = lead.ConvertedAccountId;
                ObjOpp.Name =  lead.Name;
                ObjOpp.StageName = 'Prospecting';
                ObjOpp.CloseDate = System.today();
                objOpportunity.add(ObjOpp);
                system.debug('*************ConvertedAccountId************'+lead.ConvertedAccountId);
                //newLead.Email.addError('A lead with this email address already exists.');
               }
            insert objOpportunity;
            system.debug('*************objOpportunity************'+objOpportunity);
        }

        if(trigger.isAfter){
            LeadStatus convertStatus = [SELECT Id, MasterLabel FROM LeadStatus WHERE IsConverted=true LIMIT 1];  
            for (Lead lead : Trigger.new) {

                //Converting those leads whose Status is equal to Web
                if (lead.isConverted == false && lead.LeadSource == 'Web'){ 
                    system.debug('************lead*************'+lead);
                    Database.LeadConvert objLeadConvert = new Database.LeadConvert();
                    objLeadConvert.setLeadId(lead.Id);      
                    String oppName =  lead.Name;
                    objLeadConvert.setOpportunityName(oppName);     
                    objLeadConvert.setConvertedStatus(convertStatus.MasterLabel);
                    Database.LeadConvertResult objLeadConvertResult = Database.convertLead(objLeadConvert);

                }
            }
        }
    }

When creating new lead if we found duplicate (or same) email id in System, we should create only opportunity under Account who has same email.

2

1 Answer 1

0
trigger conversion on Lead (after insert) {
    set<id>idset=new set<id>();

    list<lead>lelist=new list<lead>();
    account ac=new account();
    contact con=new contact();
    account acs=new account();
    opportunity opp=new opportunity();
    list<account>acclist=new list<account>();

    list<contact>conlist=new list<contact>();
    list<opportunity>opplist=new list<opportunity>();
    if(Trigger.isDelete || Trigger.isInsert){
        if(Trigger.isDelete){
            for(Lead l:trigger.old){

                idset.add(l.id);
                ac.name=l.company;
                ac.id=l.id;

            }
        }
        else {
            for(lead ls:trigger.new){

                idset.add(ls.id);
                ac.name=ls.company;
                ac.id=ls.PartnerAccountid;

            }
        }
    }
    for(lead le:trigger.new){

        idset.add(le.id);
        ac.name=le.company;
        ac.id=le.PartnerAccountid;

    }
         acclist.add(ac);
    insert acclist;





    for(account acc:acclist){
        con.lastname=acc.name;    
        con.accountid=acc.id;
        opp.name=acc.name;
        opp.stagename='Prospecting';
        opp.closedate=system.today();
        opp.accountid=acc.id;   

    }
    conlist.add(con);
    opplist.add(opp);
    insert conlist;
    insert opplist;
    system.debug('***'+ac);

}
1
  • Please also add some words about how you solved it and where you had change some logic.
    – itsmebasti
    Commented Nov 30, 2017 at 8:06

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .