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
JustinWilliams2381JustinWilliams2381 

Can I convert a lead AND update the new opportunity in the same trigger?

I have a decent lead conversion trigger that works to convert the lead and check for existing contacts by email to add to existing contact and account rather than create new.  What I want to do is to also set fields on the opportunity that is created by the lead conversion in the same trigger.

 

I have a few lines to see if I could update that opportunity but they didn't seem to work.  is it possible?

 

trigger OnlineProposalConvertSE on Lead (after insert, after update) {
    //variable used to filter out non submittal exchange contacts.
    ID ContactType = Schema.SObjectType.Contact.getRecordTypeInfosByName().get('Submittal Exchange Contact').getRecordTypeId();
    
    for (Lead lead : Trigger.new) {
        if (lead.isConverted == false && lead.LeadSource == 'Online Order Form'){ //to prevent recursion
        
            Database.LeadConvert lc = new Database.LeadConvert();
            lc.setLeadId(lead.Id);
            
            String oppName =  lead.Project_Name__c;
            lc.setOpportunityName(oppName);
            
            LeadStatus convertStatus = [SELECT Id, MasterLabel FROM LeadStatus WHERE IsConverted=true LIMIT 1];
            lc.setConvertedStatus(convertStatus.MasterLabel);    
            
            //This section searches for existing contacts based on emails and instead of creating a new contact and account it 
            //uses the existing contact and its account record to add the new opportunity to.  Notice the filter in the SOQL
            //includes a variable created earlier in the code to limit the search to Submittal Exchange contacts 'ContactType'.
            //Despite this there may still be duplicate occurances of the same email address.  As far as I know it takes will 
            //associate the lead to the last result on the list.  Hopefully that won't be a problem :) 
            if (lead.email != null){
                String CurrentEmail = lead.email;
                List<Contact> ExistingContact = [SELECT ID, email, Account.Id FROM Contact WHERE email = :CurrentEmail AND RecordTypeID = :ContactType LIMIT 1 ];
                if(ExistingContact.size()!=0){
                    for(Contact c: ExistingContact){
                        ID aID = c.Account.Id;
                        ID cID = c.Id;
                        lc.setAccountID(aID);
                        lc.setContactID(cID);
                    }
                }
            }

            Database.LeadConvertResult lcr = Database.convertLead(lc);
            System.assert(lcr.isSuccess());
            
            String NewOpp = lead.ConvertedOpportunityId;
            
            For(Opportunity OppUpdate:[SELECT amount, Standard_List_Price__c, Contact__c FROM Opportunity WHERE ID = :NewOpp]){
                
                OppUpdate.Big_Deal_Post__c = true;
                OppUpdate.amount = OppUpdate.Standard_List_Price__c;
                OppUpdate.Contact__c = lc.getContactId();
                Update OppUpdate;
            }

        }

 

Avidev9Avidev9

You were so close!!!!!!!!!!!!

Add a query after you convert the lead to get the opportunity id

 

trigger OnlineProposalConvertSE on Lead (after insert, after update) {
    //variable used to filter out non submittal exchange contacts.
    ID ContactType = Schema.SObjectType.Contact.getRecordTypeInfosByName().get('Submittal Exchange Contact').getRecordTypeId();
    
    for (Lead lead : Trigger.new) {
        if (lead.isConverted == false && lead.LeadSource == 'Online Order Form'){ //to prevent recursion
        
            Database.LeadConvert lc = new Database.LeadConvert();
            lc.setLeadId(lead.Id);
            
            String oppName =  lead.Project_Name__c;
            lc.setOpportunityName(oppName);
            
            LeadStatus convertStatus = [SELECT Id, MasterLabel FROM LeadStatus WHERE IsConverted=true LIMIT 1];
            lc.setConvertedStatus(convertStatus.MasterLabel);    
            
            //This section searches for existing contacts based on emails and instead of creating a new contact and account it 
            //uses the existing contact and its account record to add the new opportunity to.  Notice the filter in the SOQL
            //includes a variable created earlier in the code to limit the search to Submittal Exchange contacts 'ContactType'.
            //Despite this there may still be duplicate occurances of the same email address.  As far as I know it takes will 
            //associate the lead to the last result on the list.  Hopefully that won't be a problem :) 
            if (lead.email != null){
                String CurrentEmail = lead.email;
                List<Contact> ExistingContact = [SELECT ID, email, Account.Id FROM Contact WHERE email = :CurrentEmail AND RecordTypeID = :ContactType LIMIT 1 ];
                if(ExistingContact.size()!=0){
                    for(Contact c: ExistingContact){
                        ID aID = c.Account.Id;
                        ID cID = c.Id;
                        lc.setAccountID(aID);
                        lc.setContactID(cID);
                    }
                }
            }

            Database.LeadConvertResult lcr = Database.convertLead(lc);
            System.assert(lcr.isSuccess());
            
            String NewOpp = [SELECT ConvertedOpportunityId FROM Lead WHERE Id =:lead.Id].ConvertedOpportunityId;
            
            For(Opportunity OppUpdate:[SELECT amount, Standard_List_Price__c, Contact__c FROM Opportunity WHERE ID = :NewOpp]){
                
                OppUpdate.Big_Deal_Post__c = true;
                OppUpdate.amount = OppUpdate.Standard_List_Price__c;
                OppUpdate.Contact__c = lc.getContactId();
                Update OppUpdate;
            }

        }