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
Justin.WilliamsJustin.Williams 

Moving SOQL query outside For statement for Lead Convert Trigger into existing Account

Created a trigger for the service team to easily create a lead and contact if one doesnt exist. (basicaly create a lead then auto convert).  I recently ran a lead inport using the out of the box wizzard and got a error.

 

"

Apex script unhandled trigger exception by user/organization: -------------------------

CPMNewContact: System.LimitException: Too many SOQL queries: 101"

 

I'm guessing I didn't bulkify or put the query in the right spot.  Any thoughts? 

trigger CPMNewContact on Lead (after insert) {
    for (Lead l : Trigger.new) {
        
        Id rtId = [select Id, name from RecordType where name = 'CPM Support Contact' and SObjectType = 'Lead' limit 1].id;

        if (l.isConverted == false && l.recordtypeid == rtId) //to prevent recursion
        {
            Database.LeadConvert lc = new Database.LeadConvert();
            lc.setLeadId(l.Id);
            lc.setDoNotCreateOpportunity(true);
            
            LeadStatus convertStatus = [SELECT Id, MasterLabel FROM LeadStatus WHERE IsConverted=true LIMIT 1];
            lc.setConvertedStatus(convertStatus.MasterLabel);
            
            Database.LeadConvertResult lcr = Database.convertLead(lc);
            System.assert(lcr.isSuccess());
            
            //String A = [SELECT ConvertedAccountId FROM Lead WHERE Id =:l.Id].ConvertedAccountId;
            //String C = [SELECT ConvertedContactId FROM Lead WHERE Id =:l.Id].ConvertedContactId;
            //String Org = [SELECT CPM_Org_ID__c FROM Lead WHERE Id =:l.Id];
            
            //Case NewCase = new Case(AccountId=A, ContactId=C);
            //insert NewCase;
        }
    }
}

 

Saikishore Reddy AengareddySaikishore Reddy Aengareddy

Move the below line of code to outside the for loop and see if it runs smoothly...

 

LeadStatus convertStatus = [SELECT Id, MasterLabel FROM LeadStatus WHERE IsConverted=true LIMIT 1];
Puja_mfsiPuja_mfsi

HI,

Use this code.

1). You don't need to query on recordType ,I have use the Schema.Sobject to get the recordTypeId.

2). Do not put query inside for loop,In this case your query is not depends on your coming lead i.e. your query is same for all lead so put this query outside the for loop.

3). Do not perform DML operation inside for loop ,store all the value in a list after that perform operation.

 

trigger CPMNewContact on Lead (after insert) {

    // Find out the lead record Type Id.
    String CPMSUPPORT = 'CPM Support Contact';
    Map<String, Schema.RecordTypeInfo>  leadRecordTypes = new Map<String, Schema.RecordTypeInfo>();
    leadRecordTypes = Schema.SObjectType.Lead.getRecordTypeInfosByName();
    String leadRecordTypeId = leadRecordTypes.get(CPMSUPPORT).getRecordTypeId();
    
    //Query on LeadStatus 
    LeadStatus convertStatus = [SELECT Id, MasterLabel FROM LeadStatus WHERE IsConverted=true LIMIT 1];
    
    list<Database.LeadConvert> leadConverts = new list<Database.LeadConvert>();
    for (Lead l : Trigger.new) {
        if (l.isConverted == false && l.recordtypeid == leadRecordTypeId  ) //to prevent recursion
        {
            Database.LeadConvert lc = new Database.LeadConvert();
            lc.setLeadId(l.Id);
            lc.setDoNotCreateOpportunity(true);
            lc.setConvertedStatus(convertStatus.MasterLabel);
            leadConverts.add( lc );
        }
    }
    if(!leadConverts.isEmpty()){
        Database.LeadConvertResult[] leadCRList = Database.convertLead(leadConverts, false);
        for(Database.LeadConvertResult lcr : leadCRList )
            System.assert(lcr.isSuccess());
    }
}

 

Please let me know if u have any problem on same ,and if this post helps to u please give KUDOS by click on star at left.