You need to sign in to do that
Don't have an account?

Converted Lead Opportunity Field Seems Blank
Hey all.
I am writting a simple trigger that will update records related to leads when the lead is converted. When the lead is converted it needs to populate all associated lead_detail__c objects project__c field with the Id of the created opportunity. Issue is it doesn't seem like when the lead is converted that it's ConvertedOpportunityId is getting populated, so my lead_detail__c objects are not getting updated. I'm not totally sure why (pretty new to the lead conversion process) so any help would be appreciated. Here is my class.
public class leadClasses { public static void transferLeadDetails(lead[] leadList) { //map that holds list of lead id's to the opportunities that they created after conversion map<id,id> LeadToOpportunityMap = new map<id,id>(); //iterate over every lead passed in for(Lead lead : leadList) { // if a new opportunity was created due to a conversion if (lead.ConvertedOpportunityId != null) { //add the lead id and it's associated opportunity to a map LeadToOpportunityMap.put(lead.id,lead.ConvertedOpportunityId); } } //if any of those leads passed in had opportunities created, then continue if(!LeadToOpportunityMap.isEmpty()) { //find any lead details that belong to the leads in the list Lead_Detail__c[] leadDetails = [select id from Lead_Detail__c where lead__c in :LeadToOpportunityMap.keySet()]; //iterate over all those lead details for (Lead_Detail__c leadDetail : leadDetails) { //set the project__c relationship field on the lead to the created opportunity which is stored in the map leadDetail.Project__c = LeadToOpportunityMap.get(leadDetail.lead__c); } //update the lead detail objects update leadDetails; } } @isTest public static void testLeadClasses() { //create a lead Lead lead = testDataGenerator.createTestLead(); //create a lead detail associated to the lead Lead_Detail__c thisTestLeadDetail = testDataGenerator.createTestLeadDetail(lead.id); //convert the lead, which will create an opportunity Database.LeadConvert lc = new database.LeadConvert(); lc.setLeadId(lead.Id); LeadStatus convertstatus = [select Id, MasterLabel from LeadStatus where IsConverted=true limit 1]; lc.setConvertedStatus(convertStatus.MasterLabel); Database.LeadConvert[] lcArray = new Database.LeadConvert[] {lc}; Database.LeadConvertResult[] results = Database.convertLead(lcArray); //check to make sure the conversion was a success System.assert(results[0].IsSuccess()); System.debug('----------- CREATED OPPORTUNITY ID! --------------'); System.debug(results[0].getOpportunityId()); //query the lead detail so we can see if the project__c got updated with the created opportunity id thisTestLeadDetail = [select lead__c, project__c from Lead_Detail__c where id = : thisTestLeadDetail.id]; //check to make sure the lead detail's project__c field got updated with the created opportunity id. System.assertEquals(results[0].getOpportunityId(), thisTestLeadDetail.project__c); //This assert fails. Says opportnity id is null. } }
The class gets called by my trigger properly, just non of them pass the first if statment that checks to see if the ConvertedOpportunityId is not null, meaning it things they are all null. Shrug, I'll keep playing around with it, but I figured I'd see if anyone knew off the top of their head whats up. Thanks!
Oops, I'm dumb. I figured it out. My trigger was accidentally set as after insert, instead of after update. Sorry about that! Problem solved.
All Answers
Just an additional note, the debug line that prints the getOpportunityId does indeed have a value.
10:52:09.932 (3932344000)|USER_DEBUG|[58]|DEBUG|----------- CREATED OPPORTUNITY ID! --------------
10:52:09.932 (3932394000)|USER_DEBUG|[59]|DEBUG|006S0000005i42YIAQ
So an opportunity does get created, there is just something wrong with my if statment, or my logic or something. An opportunity is getting created, just I'm not detecting it in my actual method.
Oops, I'm dumb. I figured it out. My trigger was accidentally set as after insert, instead of after update. Sorry about that! Problem solved.