+ Start a Discussion
adambreen.ax1546adambreen.ax1546 

convertLead : how to specify Opportunity record type and stage?

I have this (working) trigger:

 

trigger convertLeadToImplementationOpportunity on Lead (after update) {
    Set<Lead> leadsToConvert = new Set<Lead>();
    for (Lead lead : Trigger.new){
    	leadsToConvert.add(lead);
    }
    
	for(Lead lead : leadsToConvert){   
		     
	    if(lead.Status == 'Qualified' && !lead.isConverted && lead.isApproved__c == true){	    	
	    		        
	        Database.LeadConvert leadConvert = new database.LeadConvert();
	        LeadStatus convertStatus = [SELECT Id, MasterLabel FROM LeadStatus WHERE IsConverted=true LIMIT 1];

            leadConvert.setConvertedStatus(convertStatus.MasterLabel); // ("Closed - Converted");
            leadConvert.setLeadId(lead.id);

	        Database.LeadConvertResult leadConvertResult = Database.convertLead(leadConvert);
	        System.assert(leadConvertResult.isSuccess());
	    }
    }
}

 

However, I want to specify (or set) each new Opportunity's record type and stage.

 

At the minimum, it's ok to rely on default SF behaviour, but for some reason my new Opportunities are currently created with "Closed - Lost" Stage, which is NOT the first Stage in my stages list.

 

For example purposes, let's say that I want the Opportunity to be created as a Record Type = "New Sale" and have a Stage = "First Presentation".

 

 

taaataaa

Hey,

 

Is it like, you want when Lead being converted, a specific record type of Opportunity automatically update?

 

regards

adambreen.ax1546adambreen.ax1546

Yes, I want a specific record type, and a specific opportunity stage.

taaataaa

So you would have been already made record type, and you have given by default stage also which one you want. Please follow the steps, hope it helps:

 

1) Create one text field on Lead, eg: "Opp Record type"

2)Make one workflow on Lead, with the criteria "Lead: Lead Record Typenot equal to null ",

Field Update : Lead: Opp Record type (custom text field)
Formula Value : $RecordType.Name

 

3)Now in Lead, you will find Map Field under Field section. Map the custom field "Opp Record Type" to Opportunity.Opportunity Record type.

 

4) Now make one workflow on Opportunity, with the criteria "Opportunity Record Type equals  (The name of the your Lead Record type(XYZ).

Field Update
Opportunity: Opportunity Record Type
Field Value: Your Opportunity Record Type(ABC)

 

Let me know if you find any issues.

 

regards

adambreen.ax1546adambreen.ax1546

I think you mean I should ALSO add a custom field to Opportunity, that the Lead OpportunityType maps into?

 

Then the Opportunity Workflow can compare the Opportunity's custom field (OpportunityType) with Opportunity's Record Type, and change that RecordType if they are not equal?

 

Is that right?

taaataaa

No, you can directly map that custom field to standard Opportunity record type. Or else you can add these code in your trigger also,

 

Map<Id, Id>OppId = new Map<Id, Id>();
 List<Opportunity> lstopp = new List<Opportunity>();

 

 for (Integer i = 0; i < Trigger.new.size(); i++){
        if (Trigger.new[i].IsConverted == true && Trigger.old[i].isConverted == false){
             {
                     OppId.put(Trigger.new[i].id,Trigger.new[i].convertedOpportunityId);
             }
             Opportunity oppty = new Opportunity(Id=Trigger.new[i].ConvertedOpportunityId);
             oppty.RecordtypeID='Your Record type ID';

             oppty.Stage='----';
              lstopp.add(oppty);
       }

adambreen.ax1546adambreen.ax1546

Thanks.  I tried directly mapping into the standard Opportunity Record Type, but the option was not available. I can only map into Custom Fields on Opportunity.

 

 

taaataaa

Yes, M sorry, make one custom field in Opp also..That code worked?

adambreen.ax1546adambreen.ax1546

Ok, so I like the idea of doing this via my trigger (at the top of this thread).

 

I will tell the leadConvert NOT to automatically create the Opportunity, like so:

 

trigger convertLeadToImplementationOpportunity on Lead (after update) {
    Set<Lead> leadsToConvert = new Set<Lead>();
    for (Lead lead : Trigger.new){
    	leadsToConvert.add(lead);
    }

	for(Lead lead : leadsToConvert){

	    if(lead.Status == 'Qualified' && !lead.isConverted && lead.isApproved__c == true){

	        Database.LeadConvert leadConvert = new database.LeadConvert();
	        LeadStatus convertStatus = [SELECT Id, MasterLabel FROM LeadStatus WHERE IsConverted=true LIMIT 1];

            leadConvert.setConvertedStatus(convertStatus.MasterLabel);
            leadConvert.setLeadId(lead.id);
            leadConvert.isDoNotCreateOpportunity(true);  //DO NOT CREATE OPPORTUNITY BY DEFAULT

	        Database.LeadConvertResult leadConvertResult = Database.convertLead(leadConvert);
	        System.assert(leadConvertResult.isSuccess());
	    }
    }
}

 

But I'm not sure how to then create the Opportunity so it is linked to the correct Account?

What would the SOQL be to find the right Account, based on the Lead id?

taaataaa

I didnt understood what you are trying to say, you dont want creation of opp as soon lead conversion happen?>