Take the 2-minute tour ×
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.

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);
        }  
    }

}

share|improve this question
    
I Think you need to move that update out of your for loop –  EricSSH Jul 7 at 17:09
add comment

2 Answers 2

You are trying to update your custom object in for loop which is causing the same error and which is also not a good practice in salesforce. Below is the code snippet I have updated plz see if it helps you

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.
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]); 
    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);


    List<Reached_Out__c > customObjList = new List<Reached_Out__c >();        
    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;
                            customObjList.add(CustomObject);
                        }
                    }
                }
    update customObjList ;
            }
        }
    } 

Hope this helps you.

share|improve this answer
    
To add on to this you have SOQL queries inside your for loops which is a no bueno developer.salesforce.com/page/… –  EricSSH Jul 7 at 17:21
    
Thanks Eric for correcting. I have missed this point also. –  tandonprateek Jul 7 at 17:45
    
I tried both of these suggestions as well as combining them and I still run into the same error –  Alex Jul 8 at 13:20
add comment

It looks like you have both a SOQL query and a DML statement inside of your for loop. You'll want to move both of them outside of the loop(s). There are also some redundant inner loops that can be removed. I've included a revised version of your code below, with comments to explain the edits. You can also read more about removing SOQL queries from for loops here: https://developer.salesforce.com/page/Best_Practice:_Avoid_SOQL_Queries_Inside_FOR_Loops

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.
    Set<Id> leadIds = new Set<Id>();
    for (Integer i = 0; i < Trigger.new.size(); i++)
    {
        if (Trigger.new[i].IsConverted == true && Trigger.old[i].isConverted == false)
        {
            // for (Lead lead : Trigger.new)
            // You are already looping through the Leads in Trigger.new
                // leadIds.add(lead.Id);
                // You can add the Lead Id to the Set by referencing the Lead directly in Trigger.new
                leadIds.add(Trigger.new[i].Id);
        }
    }

    if (leadIds.size() > 0)
    // Check to see that you have converted leads in the leadIds Set 
    {
        Map<Id, Reached_Out__c> customObjectsToUpdate = new Map<Id, Reached_Out__c>();
        // Create a map to hold the custom object you are going to update
        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]);
        // Now the SOQL query is no longer in a for loop

        // if(!Trigger.new.isEmpty()) {
            // Trigger.new won't ever be empty in before update context 
            // for (Lead lead : Trigger.new) {
                // You can just loop through the custom object records returned in your query  
                for (Reached_Out__c CustomObject : entries.values()) 
                {
                    // if (CustomObject.Lead__c == lead.Id)
                    // Since you are looping through the returned custom object records that were in the Lead Ids Set, no need to check this, you can just reference the
                    //  Lead that corresponds to the custom object record by using Trigger.newMap

                    // Get the value from the Lead's ConvertedOpportunityId field by referencing the Lead in Trigger.newMap
                    CustomObject.test__c = trigger.newMap.get(CustomObject.Lead__c).ConvertedOpportunityId;
                    customObjectsToUpdate.put(CustomObject.Id, CustomObject);
                }
         // }
     // }
        // Now your update is outside of a for loop also
        update customObjectsToUpdate;
    }
}
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.