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
Alex DornAlex Dorn 

Problem with Trigger or with App?

I have edited a trigger and test class to apply to my needs. I just need the custom object on conversion to update the custom object on an opportunity where previously it updated contact, account, and the opportunity. I recieve this error when testing
Failure Message: "System.Exception: SFSSDupeCatcher:Too many SOQL queries: 101", Failure Stack Trace: "(SFSSDupeCatcher)"
Is this an error with DupeCatcher or with the Trigger/test class.

Trigger:
trigger UpdateCustomeObject_Trigger on Lead (before update) {
//This trigger will associate a Custom Object record with the contact and opportunity associated to the
//lead after it has been converted.
//The Custom Object is associated to an opportunity only if an opportunity record exist on the Lead.
    for (Integer i = 0; i < Trigger.new.size(); i++){
        if (Trigger.new[i].IsConverted == true && Trigger.old[i].isConverted == false){
            Set<Id> leadIds = new Set<Id>();
            for (Lead lead : Trigger.new)
                leadIds.add(lead.Id);
           
            Map<Id, Reached_Out__c> entries = new Map<Id, Reached_Out__c>([select test__c, Lead__c from Reached_Out__c where lead__c in :leadIds]);       
            if(!Trigger.new.isEmpty()) {
                for (Lead lead : Trigger.new)  {
                    for (Reached_Out__c CustomObject : entries.values()) {
                        if (CustomObject.Lead__c == lead.Id) {
                            CustomObject.test__c = lead.ConvertedOpportunityId;
                            update CustomObject;
                        }
                    }
                }
            }
        }
    }

}

Test Class:
@isTest
//This is a test case for a situation where a lead will be converted.  The developer must explicitly call the convert lead
//method to simulate the user action.

private class TestTriggerCustomObjectUpdate {
        static testMethod void TestReferralUpdate() {
        // Insert the Lead
        List<Lead> leads = new List<Lead>();
        Lead leadt = new Lead (FirstName ='fname', LastName ='test', Company ='myCompany',  Website = 'TestWebsite.com', Origin_Date__C = Date.newInstance(2014,12,31), Origin__c = 'Other', Status = 'Exception', Exception_Notes__c='asdf');
        insert leadt;
        // Insert the custom object Record
        Reached_Out__C customobject = new Reached_Out__C (Lead__c = leadt.Id);
        insert customobject;       
       
        //Convert the Lead
        Database.LeadConvert lc = new database.LeadConvert();
        lc.setLeadId(leadt.Id);
        LeadStatus convertStatus = [Select Id, MasterLabel from LeadStatus where IsConverted=true limit 1];
        lc.setConvertedStatus(convertStatus.MasterLabel);
        Database.LeadConvertResult lcr = Database.convertLead(lc);   
       
        //Requery for the referral record to see if it is updated
        Reached_Out__C ref_upd = [select Test__c from Reached_Out__C where Lead__c = :leadt.Id];
       
        //Check that the test passed
        System.assertEquals(ref_upd.Test__c,[Select ConvertedOpportunityId From Lead Where Id = :customobject.Lead__c].ConvertedOpportunityId);      
   
        //Test if no opty is created
        string NoOpty = 'Y';       
        if (NoOpty =='Y'){
            Lead leadto = new Lead (FirstName ='fnameo', LastName ='testo', Company ='myCompanyo');
            insert leadto;
            // Insert the custom object record
            Reached_Out__C customobjecto = new Reached_Out__C (Lead__c = leadto.Id);
            insert customobjecto;
           
            Database.LeadConvert lco = new database.LeadConvert();
            lco.setLeadId(leadto.Id);
            lco.isDoNotCreateOpportunity();
            lco.setDoNotCreateOpportunity(true);
            LeadStatus convertStatuso = [Select Id, MasterLabel from LeadStatus where IsConverted=true limit 1];
            lco.setConvertedStatus(convertStatuso.MasterLabel);
            Database.LeadConvertResult lcro = Database.convertLead(lco);
       
            Reached_Out__C ref_updo = [select Test__c from Reached_Out__C where Lead__c = :leadto.Id];
           
            //Check that the test passed
            System.assert(ref_updo.Test__c == null);
        }  
    }

    static testMethod void testBulkUpdate() {
        List<Lead> leads = new List<Lead>();      
        for (Integer i=0;i<5;i++) {
            Lead l = new Lead (FirstName ='bulk', LastName ='Test', Company ='myCompanyo');
            insert l;
            // Insert the Custom Record
            Reached_Out__C r = new Reached_Out__C (Lead__c = l.Id);
            insert r;
           
            //Convert the Lead
            Database.LeadConvert lcb = new database.LeadConvert();
            lcb.setLeadId(l.Id);
            LeadStatus convertStatusb = [Select Id, MasterLabel from LeadStatus where IsConverted=true limit 1];
            lcb.setConvertedStatus(convertStatusb.MasterLabel);
            Database.LeadConvertResult lcrb = Database.convertLead(lcb);
           
            Reached_Out__C bulkup = [select  Test__c from Reached_Out__C where Lead__c =:l.Id];
           
            //Check that the test has passed
            System.assertEquals(bulkup.Test__c,[Select ConvertedOpportunityId From Lead Where Id = :r.Lead__c].ConvertedOpportunityId);
            }  
        }
}
Andy BoettcherAndy Boettcher
Your trigger appears to be the issue, but without seeing the DupeCatcher code I can't be 100% sure - you are both doing SOQL and DML within loops, so you'll hit a limit no matter what when you get to a certain number of Leads running through there.
Pavan Kumar KajaPavan Kumar Kaja
Hi Alex,

You are doing the soql, DML in for loop . have modified in below code, try this. if u have any issues contact me @ pavanthetech@gmail.com
trigger UpdateCustomeObject_Trigger on Lead (before update) {
	Set<Id> leadIds = new Set<Id>();
	List<Reached_Out__c> lstReachedOut = new List<Reached_Out__c>();
    for(Integer i = 0; i < Trigger.new.size(); i++){
        if(Trigger.new[i].IsConverted == true && Trigger.old[i].isConverted == false){
			leadIds.add(lead.Id);
		} 
	}
	
	Map<Id, Reached_Out__c> entries = new Map<Id, Reached_Out__c>([select test__c, Lead__c from Reached_Out__c where lead__c in :leadIds]);       
	
	if(!Trigger.new.isEmpty()) {
		for (Lead lead : Trigger.new)  {
			for (Reached_Out__c CustomObject : entries.values()) {
				if (CustomObject.Lead__c == lead.Id) {
					CustomObject.test__c = lead.ConvertedOpportunityId;
					lstReachedOut.add(CustomObject);
				}
			}
		}
		update lstReachedOut;
	}
}