i have 2 methods in trigger ,one is if the lead have initial and final fields values then i want to create opportunity line items and subscription and this is working fine. second is when converting i want to check the condition like lead email is equal to existing account's email then i want to create opportunity and opportunity under that existing account .but what i got is it creating a new account and new opportunity.please tell me how to prevent creating account when this condition fits

public static void createOpportunityAndLineItem(List<Lead> leads) {
    Set<String> leadEmails = new Set<String>();
    for (Lead lead : leads) {

    // Query Accounts where the Email__c matches with the Lead email
    List<Account> matchingAccounts = [SELECT Id, Email__c FROM Account WHERE Email__c IN :leadEmails];

    // Create a map of Email to Account Id
    Map<String, Id> emailToAccountIdMap = new Map<String, Id>();
    for (Account account : matchingAccounts) {
        emailToAccountIdMap.put(account.Email__c, account.Id);

    // Query converted Leads where the ConvertedAccountId is null and the Lead Email matches with an Account Email
    List<Lead> convertedLeads = [SELECT Id, ConvertedAccountId, LastName, Lead_Package__c, Email FROM Lead WHERE IsConverted = true AND ConvertedAccountId = null AND Email IN :emailToAccountIdMap.keySet()];

    // Create Opportunities and Opportunity Line Items under the matching Accounts
    List<Opportunity> opportunitiesToInsert = new List<Opportunity>();
    List<OpportunityLineItem> opportunityLineItemsToInsert = new List<OpportunityLineItem>();
    for (Lead convertedLead : convertedLeads) {
        Id accountId = emailToAccountIdMap.get(convertedLead.Email);
        if (accountId != null) {
            Opportunity opportunity = new Opportunity(
                Name = convertedLead.LastName + ' Opportunity',
                StageName = 'Prospecting',
                CloseDate = Date.today(),
                AccountId = accountId

            OpportunityLineItem opportunityLineItem = new OpportunityLineItem(
                Quantity = 1,
                Product2Id = convertedLead.Lead_Package__c,
                TotalPrice = 100
            opportunityLineItem.OpportunityId = opportunity.Id;

    // Insert Opportunities and Opportunity Line Items
    if (!opportunitiesToInsert.isEmpty()) {
        insert opportunitiesToInsert;
        insert opportunityLineItemsToInsert;

1 Answer 1


I think you should read about Database.LeadConvert

It gives you access to handle the lead conversion.

Here you can mention whether you want to use the existing account or not -

Database.LeadConvert dl = new Database.LeadConvert();
dl.setLeadId('Id of your lead');
dl.setDoNotCreateOpportunity(Boolean);// set to true if you don't want an opportunity
dl.setOpportunityId(opportunityId);//set the existing opportunity Id.
dl.setAccountId(ID);//If you want to merge a lead with an existing account
Database.LeadConvertResult result = Database.convertLead(dl);

You must log in to answer this question.

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