Objective: To map each record type on lead with the record type of account object while lead conversion. If the mapped record type is not exists for Account object than default record type should be assigned.
There are 2 ways to map the record types of lead to account while lead conversion. Before starting create some record types on Lead object and Account object.
#1 way:
è Create a before update trigger on lead to map the record types
#2 way:
è Creating workflow rule for each record type mapping on account object
To achieve above objective using #1 way:
- Create 2 custom formula fields of type text:
a) Lead Record Type Name
b) Account Record Type Name
- Insert some record type mapping records which exists on Lead and Account object.
- Now create a before update trigger on Lead object
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
trigger trigRecordTypeMapping on Lead (before update) {
for(Lead lead:System.Trigger.new)
{
//Check whether convert request coming for the first time
if (Trigger.new[0].IsConverted == true && Trigger.old[0].isConverted == false)
{
//get lead object
Lead leadobj = [SELECT Id,RecordType.name FROM Lead WHERE Id = :lead.Id];
//get Account record type name mapped to Lead record type name in Record_Type_Mapping__c custom object
String AccRTypeName = [SELECT Account_Record_Type__c FROM Record_Type_Mapping__c WHERE Lead_Record_Type__c = :leadobj.RecordType.Name limit 1].Account_Record_Type__c;
//Check wether record type exists for Account
if([Select Id From RecordType where sobjecttype = 'Account' and name = :AccRTypeName limit 1].size() > 0 )
{
//get record type id using record type name
Id id = [Select Id From RecordType where sobjecttype = 'Account' and name = :AccRTypeName limit 1].id;
//get account object to update
Account acc = [SELECT Id,RecordType.Name FROM Account WHERE Id = :lead.ConvertedAccountId];
//assign record type id to Account
acc.RecordTypeId = id ;
//update Account
update acc;
}
}
}
}
--------------------------------------------------------------------------------------------------------------------------------------------------------------------
- Below is Test method for above trigger :
--------------------------------------------------------------------------------------------------------------------------------------------------------------------
@isTest
private class TestLeadConversion
{
static testMethod void leadConversionTest()
{
Lead lead = new Lead();
lead.Email = 'test@test.com';
lead.lastname = 'Testerson';
lead.Company = 'Market Services';
lead.RecordTypeId = '01280000000Lq5E';//'Polaris';
//lead.Status = 'Fresh';
lead.OwnerId = '00580000001rP69';
insert lead;
//start testing
//Test.startTest();
//convert lead
Database.Leadconvert lc = new database.Leadconvert();
lc.setLeadId(lead.Id);
lc.setOwnerId('00580000001rP69');
//lc.setConvertedStatus('Closed - Converted');
LeadStatus convertStatus = [Select Id, MasterLabel from LeadStatus where IsConverted=true limit 1];
lc.setConvertedStatus(convertStatus.MasterLabel);
Database.Leadconvertresult lcr = database.convertLead(lc);
//verify lead successfully converted
system.assert(lcr.isSuccess()); //PASSED
//get Account
Account acc = [SELECT Id,RecordType.Name FROM Account WHERE Id = :lcr.getAccountId()];
//get Lead
Lead leadobj = [SELECT Id,RecordType.name FROM Lead WHERE Id = :lead.Id];
//get Account record type name mapped to Lead record type name in Record_Type_Mapping__c custom object
String AccRTypeName = [SELECT Account_Record_Type__c FROM Record_Type_Mapping__c WHERE Lead_Record_Type__c = :leadobj.RecordType.Name limit 1].Account_Record_Type__c;
system.assertEquals(acc.recordType.Name,AccRTypeName);
//end testing
//Test.stopTest();
}
}
--------------------------------------------------------------------------------------------------------------------------------------------------------------------
To achieve above objective using #2 way:
1. Create a formula field on the Lead and populate this with the Lead Record Type ID field
2. Create a Large Text field on the Account with length = 1300 (needed to match length of formula field) called Lead Record Type
3. Update Lead Mapping to map the Lead Record Type field with the Opportunity: Lead Record Type field
4. Create a workflow rule on the Account for each record type with the criteria of Lead Record Type = record type id.
5. Add a Field Update on the rule to set the Account Record Type to the appropriate record type matching Lead Record Type.
Note: for each record type mapping you should create a workflow rule and field update action on that.