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
marylarmstrongmarylarmstrong 

Can anyone get this to pass the test?

This trigger works just fine in my sandbox, but doesn't pass the test so I can upload into production. Can anyone please help?

 

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, CustomObject__c> entries = new Map<Id, CustomObject__c>([select Contact__c, Opportunity__c, Account__c, Lead__c from CustomObject__c where lead__c in :leadIds]);        
            if(!Trigger.new.isEmpty()) {
                for (Lead lead : Trigger.new)  {
                    for (CustomObject__c CustomObject : entries.values()) {
                        if (CustomObject.Lead__c == lead.Id) {
                            CustomObject.contact__c = lead.ConvertedContactId;
                            CustomObject.opportunity__c = lead.ConvertedOpportunityId;
                            CustomObject.account__c = lead.ConvertedAccountId;
                            update CustomObject;
                            
                        }
                    }
                }
            }
        }
    }

}

MauricioArdilaMauricioArdila

can you give more details of what is the problem ?

dmchengdmcheng

As Mauricio said, you need to give more details about the error as well as include your unit test code.  When you post code, you should ciick "C" icon so you can paste the code into another window and that formats using fixed font.

 

I can see several other problems that are not related:

* Your first for loop seems to enclose all the code that follows it.  The first for loop should only be use to populate the lead ID set, then you should close out the loop.

 

* your second for loop doesn't have any curly braces.  I'm not sure how you were able to save the code without an error.  But it's not needed anyway.

 

* "entries" should be a list, not a map.

 

* You are performing a DML update inside your 3rd  for loop which can cause you to exceed governor limits.  You should perform the update entries statement outside the for loop.

 

dmchengdmcheng

A better design for your trigger would be this:

-- Integer for loop as you are doing now but add the converted lead to a list as well as the ID to a set.  Example: cvtLeads.add(Trigger.new[i]);

 

-- Select the custom object records into a List, not a Map.  Custom_Object__c entries = [select .....];

 

-- loop through the converted leads for(Lead ld : cvtLeads) {}

-- Inside this loop, loop through the custom object records for(Custom_Object__c co :  entries){}

-- Assign the values co.Contact__c = lead.convertedcontactID, etc.

 

-- When the converted Lead loop is done, "update entries".