+ Start a Discussion
Mohammad AzamMohammad Azam 

Lead Conversion with 2 record types in opportunity.

There are 2 record types & Page Layouts in lead (Cash and Insurance) & 2 record types & Page Layouts in Opportunity (Cash and Insurance).

If i create a lead in cash record type , when i convert the lead it should convert to opportunity in cash record type and if i create a lead in insurance record type, it should convert to opportunity with insurance record type.

Please Help
 
Best Answer chosen by Mohammad Azam
Roshan BaigRoshan Baig
Hi Azam,

You can actually create a workflow instead of Trigger.

1.  You will need to create a custom field on your lead page called i.e Lead Record Type 1.  This should be a Long       Text Area Data type.  Default value $RecordType.Name

2.  Create the same custom field on your opportunity.

3.  Map the custom field in the Lead to map to the custom field in the Opportunity

4.  Create a workflow for opportunity bases on Custom field opportunity that the custom field in opportunity = the record type name of  i.e. " New"

5.  Then update the field record type to change to "new" record type.

It worked for me.

All Answers

Abhishek BansalAbhishek Bansal
Hi Mohammad Azam,

As per your requirement i have written a trigger for you.
Please find the code below :
trigger updateOpportunityRecordType on Lead(after update){
    Set<Id> oppIds = new Set<Id>();
    
    for(Lead ld : trigger.new) {
        if (ld.isConverted && !trigger.oldMap.get(ld.Id).isConverted) {
            if(ld.convertedOpportunityId != null){
                oppIds.add(ld.convertedOpportunityId);
            }
            if(ld.RecordTypeId != null){
                recordTypeIds.add(ld.RecordTypeId);
            }
        }
    }

    if(oppIds.size() > 0){
        Map<Id,Opportunity> mapOfOpportunity = new Map<Id,Opportunity>([Select RecordTypeId from Opportunity where id IN :oppIds]);
        Map<Id,RecordType> mapOfRecordType = new Map<Id,RecordType>([Select Name from RecordType where id IN :recordTypeIds]);
        
        Map<String,Id> mapOfLeadRecordTypeNameWithOppRecordTypeId = new Map<String,Id>();
        mapOfLeadRecordTypeNameWithOppRecordTypeId.put('Cash',Schema.SObjectType.Opportunity.getRecordTypeInfosByName().get('Cash').getRecordTypeId());
        mapOfLeadRecordTypeNameWithOppRecordTypeId.put('Insurance',Schema.SObjectType.Opportunity.getRecordTypeInfosByName().get('Insurance').getRecordTypeId());
        
        List<Opportunity> oppToUpdateList = new List<Opportunity>();
        for(Lead ld : trigger.new){
            if (ld.isConverted && !trigger.oldMap.get(ld.Id).isConverted && ld.convertedOpportunityId != null) {
                mapOfOpportunity.get(ld.convertedOpportunityId).RecordTypeId = mapOfLeadRecordTypeNameWithOppRecordTypeId.get(mapOfRecordType.get(ld.RecordTypeId).Name);
                oppToUpdateList.add(mapOfOpportunity.get(ld.convertedOpportunityId));
            }
        }
        if(oppToUpdateList.size() > 0){
            update oppToUpdateList;
        }
    }
}

Let me know if you have any issue with this code or if you need any help on this.

Thanks,
Abhishek Bansal.
Roshan BaigRoshan Baig
Hi Azam,

You can actually create a workflow instead of Trigger.

1.  You will need to create a custom field on your lead page called i.e Lead Record Type 1.  This should be a Long       Text Area Data type.  Default value $RecordType.Name

2.  Create the same custom field on your opportunity.

3.  Map the custom field in the Lead to map to the custom field in the Opportunity

4.  Create a workflow for opportunity bases on Custom field opportunity that the custom field in opportunity = the record type name of  i.e. " New"

5.  Then update the field record type to change to "new" record type.

It worked for me.
This was selected as the best answer
Mohammad AzamMohammad Azam
Thank you Abhishek.