+ Start a Discussion
Manjunath SC 18Manjunath SC 18 

trigger to auto convert lead into account and contact

Hello Guys

i am new to the trigges and apex development, so here  is my requirement - There is checkbox Called "Private__c" in account object
Once locked, if a new Lead is created with this Private Locked Account which already has a Allocated Owner, then a new Contact should be created for this Account under the allocated Account owner name. Lead will not be created. 

And If a new lead is added without a Private Account checkbox being checked then allow the Lead to be created with Lead creator as the Lead owner. 

so far i have tried to design a trigger, but its not meeting up my goal
 
trigger AutoConvertLeadAccount on Lead (after insert) {
    Set<String> leademails = new Set<String>();
    List<Lead> newLeads = new List<Lead>();

    // Get all the new leads
    for(Lead l : system.trigger.new){
        newLeads.add(l);
        leademails.add(l.email);
    }

    /* Make some maps of account and email addresses */
    List<Account> AccountList = [select Id, Website, OwnerId from Account where Website IN: leademails];
    Map<ID, String> peAccounts = new Map<ID, String>();
    Map<ID, ID> peAccountsOwner = new Map<ID, ID>();

if(AccountList.size()>0){
    // Generic map for preventing loss of ids
    for(Account a : AccountList){
        peAccounts.put(a.id, a.Website);
        peAccountsOwner.put(a.id, a.OwnerId);
    }

    // We will need this to get the id from the email address
    Map<String, ID> peAccountsFlipped = new Map<String, ID>();
    for(ID i : peAccounts.keyset()){
        peAccountsFlipped.put(peAccounts.get(i), i);
    }

    /* System Conversion Requirements */
    leadStatus convertStatus = [SELECT Id, MasterLabel FROM LeadStatus WHERE IsConverted=true LIMIT 1];
    List<Database.LeadConvert> leadConverts = new List<Database.LeadConvert>();
    Database.LeadConvert lc = new Database.LeadConvert();

    /* Configuring Payload */    
    for (Lead nl : newleads) {
        lc = new Database.LeadConvert();
        lc.setLeadId(nl.id);
        lc.setOverwriteLeadSource(false);
        lc.setConvertedStatus(convertStatus.MasterLabel);

        // Check to see if account already exists
        if(!peAccounts.isEmpty()){
            if(peAccountsFlipped.get(nl.email)!=null){
                lc.setAccountId(peAccountsFlipped.get(nl.email));
                lc.setOwnerId(peAccountsOwner.get(peAccountsFlipped.get(nl.email)));
                lc.setDoNotCreateOpportunity(true);
            }    
        } else {
            // In the event an account doesn't exist
            lc.setOwnerId(nl.OwnerId);
            lc.setDoNotCreateOpportunity(false);
            lc.setOpportunityName(nl.Name);
        }
        leadConverts.add(lc);
    }

    // Fire Payload
    Database.LeadConvertResult[] lcr = Database.convertLead(leadConverts);
    System.debug(LoggingLevel.INFO, lcr);
 }
}

 
Sainath VenkatSainath Venkat
HI Manjunath,

try below code.

Trigger:
trigger LeadAutoContactConverter on Lead (after insert) {
 
LeadStatus convertStatus = [Select MasterLabel from LeadStatus where IsConverted = true limit 1];
 
List<Database.LeadConvert> leadConverts = new List<Database.LeadConvert>();
for (Lead lead: Trigger.new) {
if (!lead.isConverted && lead.Status == 'Open' ) {
Database.LeadConvert lc = new Database.LeadConvert();
String opportunityName = lead.Name;
 
lc.setLeadId(lead.Id);
//lc.setAccountId(AccountId); // you can set the AccountId instead of create a new account and contact is created the mention account
lc.setSendNotificationEmail(false);
lc.setOpportunityName(opportunityName);
//lc.setDoNotCreateOpportunity(true); // Optional to create a Opportunity
lc.setConvertedStatus(convertStatus.MasterLabel);
leadConverts.add(lc);
}
}
if (!leadConverts.isEmpty()) {
List<Database.LeadConvertResult> lcr = Database.convertLead(leadConverts);
}
}

Test Class:
 
@isTest
private class TestLeadTrigger {
static testMethod void TestLeadTrigger() {
Test.startTest();
Lead l = new Lead(FirstName = 'FName', LastName = 'LName', Company = 'Test Account', Status = 'Open', Email='info@theblogreaders.com');
insert l;
system.assertEquals(l.FirstName, 'FName');
test.stopTest();
 
}
}


Mark it best if it solves your problem.