Hi Vijay, I have done a similar task, my requirement was to convert the lead of Record Type VATM To Specific Record Type VATM of Contact, Account, and Opportunity Here my Code
trigger LeadTriggerHandler on Lead (After insert) { if(Trigger.isAfter && Trigger.isInsert){ LeadAction.CustomLeadConvert(Trigger.new); } }
public without sharing class LeadAction { public static void CustomLeadConvert(List<Lead> newLeadList){ Set < Id > setAccountIds = new Set < Id >(); Set < Id > setContactIds = new Set < Id >();
Set < Id > setOppIds = new Set < Id >(); set<Id> ConvertedLeadIds = new Set<Id>(); Map<Id,Id> AccountMap = new Map<Id,Id>(); Map<Id,String> AccountRecordTypeMap = new Map<Id,String>(); LeadStatus CLeadStatus= [SELECT Id, MasterLabel FROM LeadStatus WHERE IsConverted=true Limit 1]; List<Database.LeadConvert> MassLeadconvert = new List<Database.LeadConvert>();
if (!MassLeadconvert.isEmpty()) { List<Database.LeadConvertResult> lcrList = Database.convertLead(MassLeadconvert); for(Database.LeadConvertResult lcr:lcrList){ if(lcr.isSuccess()){ ConvertedLeadIds.add(lcr.getLeadId()); } } } //getting record Id of VATM recordType. Id vATMRecordTypeIdLead= Schema.SObjectType.Lead.getRecordTypeInfosByName().get('VATM').getRecordTypeId(); List<Lead> leadList = [SELECT Id, IsConverted,ConvertedAccountId,ConvertedContactId,ConvertedOpportunityId,recordtype.Name,recordtypeId FROM Lead WHERE Id IN :ConvertedLeadIds];
List < Account > listAcctForUpdate = new List < Account >(); List < Contact > listConForUpdate = new List < Contact >(); List<Opportunity> listOppForUpdate =new List<Opportunity>(); if ( setAccountIds.size() > 0 ) { Id vATMRecordTypeId = Schema.SObjectType.Account.getRecordTypeInfosByName().get('VATM').getRecordTypeId(); /* Fetching the accounts which are created as part of lead conversion and record type is not VATM */ for ( Account objAccount : [ SELECT Id, RecordTypeId FROM Account WHERE Id IN: setAccountIds] ) { /* Setting the Record Type Id to Account's VATM Record Type Id */ if(objAccount.RecordTypeId!=vATMRecordTypeId){ listAcctForUpdate.add( new Account( Id = objAccount.Id, RecordTypeId = vATMRecordTypeId ) ); }
} }
if ( setOppIds.size() > 0 ) { Id vATMRecordTypeId = Schema.SObjectType.Opportunity.getRecordTypeInfosByName().get('VATM').getRecordTypeId(); /* Fetching the accounts which are created as part of lead conversion and record type is not VATM */ for ( Opportunity objOpp : [ SELECT Id, RecordTypeId FROM Opportunity WHERE Id IN: setOppIds] ) { /* Setting the Record Type Id to Account's VATM Record Type Id */ if(objOpp.RecordTypeId!=vATMRecordTypeId){ listOppForUpdate.add( new Opportunity( Id = ObjOpp.Id, RecordTypeId = vATMRecordTypeId ) );
} } }
if ( setContactIds.size() > 0 ) { Id vATMRecordTypeId = Schema.SObjectType.Contact.getRecordTypeInfosByName().get('VATM').getRecordTypeId(); /* Fetching the accounts which are created as part of lead conversion and record type is not VATM */ for ( Contact objCon : [ SELECT Id, RecordTypeId FROM Contact WHERE Id IN: setContactIds ] ) { /* Setting the Record Type Id to Account's VATM Record Type Id */ if(objCon.RecordTypeId!=vATMRecordTypeId){
listConForUpdate.add( new Contact( Id = ObjCon.Id, RecordTypeId = vATMRecordTypeId ) ); }
Hi Vijay, I have done a similar task, my requirement was to convert the lead of Record Type VATM To Specific Record Type VATM of Contact, Account, and Opportunity Here my Code
trigger LeadTriggerHandler on Lead (After insert) { if(Trigger.isAfter && Trigger.isInsert){ LeadAction.CustomLeadConvert(Trigger.new); } }
public without sharing class LeadAction { public static void CustomLeadConvert(List<Lead> newLeadList){ Set < Id > setAccountIds = new Set < Id >(); Set < Id > setContactIds = new Set < Id >();
Set < Id > setOppIds = new Set < Id >(); set<Id> ConvertedLeadIds = new Set<Id>(); Map<Id,Id> AccountMap = new Map<Id,Id>(); Map<Id,String> AccountRecordTypeMap = new Map<Id,String>(); LeadStatus CLeadStatus= [SELECT Id, MasterLabel FROM LeadStatus WHERE IsConverted=true Limit 1]; List<Database.LeadConvert> MassLeadconvert = new List<Database.LeadConvert>();
if (!MassLeadconvert.isEmpty()) { List<Database.LeadConvertResult> lcrList = Database.convertLead(MassLeadconvert); for(Database.LeadConvertResult lcr:lcrList){ if(lcr.isSuccess()){ ConvertedLeadIds.add(lcr.getLeadId()); } } } //getting record Id of VATM recordType. Id vATMRecordTypeIdLead= Schema.SObjectType.Lead.getRecordTypeInfosByName().get('VATM').getRecordTypeId(); List<Lead> leadList = [SELECT Id, IsConverted,ConvertedAccountId,ConvertedContactId,ConvertedOpportunityId,recordtype.Name,recordtypeId FROM Lead WHERE Id IN :ConvertedLeadIds];
List < Account > listAcctForUpdate = new List < Account >(); List < Contact > listConForUpdate = new List < Contact >(); List<Opportunity> listOppForUpdate =new List<Opportunity>(); if ( setAccountIds.size() > 0 ) { Id vATMRecordTypeId = Schema.SObjectType.Account.getRecordTypeInfosByName().get('VATM').getRecordTypeId(); /* Fetching the accounts which are created as part of lead conversion and record type is not VATM */ for ( Account objAccount : [ SELECT Id, RecordTypeId FROM Account WHERE Id IN: setAccountIds] ) { /* Setting the Record Type Id to Account's VATM Record Type Id */ if(objAccount.RecordTypeId!=vATMRecordTypeId){ listAcctForUpdate.add( new Account( Id = objAccount.Id, RecordTypeId = vATMRecordTypeId ) ); }
} }
if ( setOppIds.size() > 0 ) { Id vATMRecordTypeId = Schema.SObjectType.Opportunity.getRecordTypeInfosByName().get('VATM').getRecordTypeId(); /* Fetching the accounts which are created as part of lead conversion and record type is not VATM */ for ( Opportunity objOpp : [ SELECT Id, RecordTypeId FROM Opportunity WHERE Id IN: setOppIds] ) { /* Setting the Record Type Id to Account's VATM Record Type Id */ if(objOpp.RecordTypeId!=vATMRecordTypeId){ listOppForUpdate.add( new Opportunity( Id = ObjOpp.Id, RecordTypeId = vATMRecordTypeId ) );
} } }
if ( setContactIds.size() > 0 ) { Id vATMRecordTypeId = Schema.SObjectType.Contact.getRecordTypeInfosByName().get('VATM').getRecordTypeId(); /* Fetching the accounts which are created as part of lead conversion and record type is not VATM */ for ( Contact objCon : [ SELECT Id, RecordTypeId FROM Contact WHERE Id IN: setContactIds ] ) { /* Setting the Record Type Id to Account's VATM Record Type Id */ if(objCon.RecordTypeId!=vATMRecordTypeId){
listConForUpdate.add( new Contact( Id = ObjCon.Id, RecordTypeId = vATMRecordTypeId ) ); }
I have done a similar task, my requirement was to convert the lead of Record Type VATM To Specific Record Type VATM of Contact, Account, and Opportunity
Here my Code
trigger LeadTriggerHandler on Lead (After insert) {
if(Trigger.isAfter && Trigger.isInsert){
LeadAction.CustomLeadConvert(Trigger.new);
}
}
public without sharing class LeadAction {
public static void CustomLeadConvert(List<Lead> newLeadList){
Set < Id > setAccountIds = new Set < Id >();
Set < Id > setContactIds = new Set < Id >();
Set < Id > setOppIds = new Set < Id >();
set<Id> ConvertedLeadIds = new Set<Id>();
Map<Id,Id> AccountMap = new Map<Id,Id>();
Map<Id,String> AccountRecordTypeMap = new Map<Id,String>();
LeadStatus CLeadStatus= [SELECT Id, MasterLabel FROM LeadStatus WHERE IsConverted=true Limit 1];
List<Database.LeadConvert> MassLeadconvert = new List<Database.LeadConvert>();
for(Lead currentlead : newLeadList ){
Database.LeadConvert Leadconvert = new Database.LeadConvert();
Leadconvert.setLeadId ( currentlead.id );
Leadconvert.setConvertedStatus(CLeadStatus.MasterLabel);
MassLeadconvert.add(Leadconvert);
}
if (!MassLeadconvert.isEmpty()) {
List<Database.LeadConvertResult> lcrList = Database.convertLead(MassLeadconvert);
for(Database.LeadConvertResult lcr:lcrList){
if(lcr.isSuccess()){
ConvertedLeadIds.add(lcr.getLeadId());
}
}
}
//getting record Id of VATM recordType.
Id vATMRecordTypeIdLead= Schema.SObjectType.Lead.getRecordTypeInfosByName().get('VATM').getRecordTypeId();
List<Lead> leadList = [SELECT Id, IsConverted,ConvertedAccountId,ConvertedContactId,ConvertedOpportunityId,recordtype.Name,recordtypeId FROM Lead WHERE Id IN :ConvertedLeadIds];
for(Lead objLead:leadList){
if ( objLead.IsConverted && objLead.recordtypeId==vATMRecordTypeIdLead){
setAccountIds.add( objLead.ConvertedAccountId );
setContactIds.add(objLead.ConvertedContactId);
setOppIds.add(objLead.ConvertedOpportunityId);
}
}
List < Account > listAcctForUpdate = new List < Account >();
List < Contact > listConForUpdate = new List < Contact >();
List<Opportunity> listOppForUpdate =new List<Opportunity>();
if ( setAccountIds.size() > 0 ) {
Id vATMRecordTypeId = Schema.SObjectType.Account.getRecordTypeInfosByName().get('VATM').getRecordTypeId();
/* Fetching the accounts which are created as part of lead conversion and record type is not VATM */
for ( Account objAccount : [ SELECT Id, RecordTypeId FROM Account WHERE Id IN: setAccountIds] ) {
/* Setting the Record Type Id to Account's VATM Record Type Id */
if(objAccount.RecordTypeId!=vATMRecordTypeId){
listAcctForUpdate.add( new Account( Id = objAccount.Id, RecordTypeId = vATMRecordTypeId ) );
}
}
}
if ( setOppIds.size() > 0 ) {
Id vATMRecordTypeId = Schema.SObjectType.Opportunity.getRecordTypeInfosByName().get('VATM').getRecordTypeId();
/* Fetching the accounts which are created as part of lead conversion and record type is not VATM */
for ( Opportunity objOpp : [ SELECT Id, RecordTypeId FROM Opportunity WHERE Id IN: setOppIds] ) {
/* Setting the Record Type Id to Account's VATM Record Type Id */
if(objOpp.RecordTypeId!=vATMRecordTypeId){
listOppForUpdate.add( new Opportunity( Id = ObjOpp.Id, RecordTypeId = vATMRecordTypeId ) );
}
}
}
if ( setContactIds.size() > 0 ) {
Id vATMRecordTypeId = Schema.SObjectType.Contact.getRecordTypeInfosByName().get('VATM').getRecordTypeId();
/* Fetching the accounts which are created as part of lead conversion and record type is not VATM */
for ( Contact objCon : [ SELECT Id, RecordTypeId FROM Contact WHERE Id IN: setContactIds ] ) {
/* Setting the Record Type Id to Account's VATM Record Type Id */
if(objCon.RecordTypeId!=vATMRecordTypeId){
listConForUpdate.add( new Contact( Id = ObjCon.Id, RecordTypeId = vATMRecordTypeId ) );
}
}
}
System.debug('listAcctForUpdate '+listAcctForUpdate);
System.debug('listOppForUpdate '+listOppForUpdate);
System.debug('listConForUpdate '+listConForUpdate);
if(listConForUpdate.size()>0){
try{
update listConForUpdate;
System.debug('listAcctForUpdate '+listConForUpdate);
}catch(DmlException e) {
System.debug('The following exception has occurred: ' + e.getMessage());
}
}
if ( listAcctForUpdate.size() > 0 ){
try{
update listAcctForUpdate;
}catch(DmlException e) {
System.debug('The following exception has occurred: ' + e.getMessage());
}
}
if(listOppForUpdate.size()>0){
try{
update listOppForUpdate;
}
catch(DmlException e) {
System.debug('The following exception has occurred: ' + e.getMessage());
}
}
}
}
Kindly mark it as the best answer if it helps.
Regards,
Prateek Prasoon
All Answers
I have done a similar task, my requirement was to convert the lead of Record Type VATM To Specific Record Type VATM of Contact, Account, and Opportunity
Here my Code
trigger LeadTriggerHandler on Lead (After insert) {
if(Trigger.isAfter && Trigger.isInsert){
LeadAction.CustomLeadConvert(Trigger.new);
}
}
public without sharing class LeadAction {
public static void CustomLeadConvert(List<Lead> newLeadList){
Set < Id > setAccountIds = new Set < Id >();
Set < Id > setContactIds = new Set < Id >();
Set < Id > setOppIds = new Set < Id >();
set<Id> ConvertedLeadIds = new Set<Id>();
Map<Id,Id> AccountMap = new Map<Id,Id>();
Map<Id,String> AccountRecordTypeMap = new Map<Id,String>();
LeadStatus CLeadStatus= [SELECT Id, MasterLabel FROM LeadStatus WHERE IsConverted=true Limit 1];
List<Database.LeadConvert> MassLeadconvert = new List<Database.LeadConvert>();
for(Lead currentlead : newLeadList ){
Database.LeadConvert Leadconvert = new Database.LeadConvert();
Leadconvert.setLeadId ( currentlead.id );
Leadconvert.setConvertedStatus(CLeadStatus.MasterLabel);
MassLeadconvert.add(Leadconvert);
}
if (!MassLeadconvert.isEmpty()) {
List<Database.LeadConvertResult> lcrList = Database.convertLead(MassLeadconvert);
for(Database.LeadConvertResult lcr:lcrList){
if(lcr.isSuccess()){
ConvertedLeadIds.add(lcr.getLeadId());
}
}
}
//getting record Id of VATM recordType.
Id vATMRecordTypeIdLead= Schema.SObjectType.Lead.getRecordTypeInfosByName().get('VATM').getRecordTypeId();
List<Lead> leadList = [SELECT Id, IsConverted,ConvertedAccountId,ConvertedContactId,ConvertedOpportunityId,recordtype.Name,recordtypeId FROM Lead WHERE Id IN :ConvertedLeadIds];
for(Lead objLead:leadList){
if ( objLead.IsConverted && objLead.recordtypeId==vATMRecordTypeIdLead){
setAccountIds.add( objLead.ConvertedAccountId );
setContactIds.add(objLead.ConvertedContactId);
setOppIds.add(objLead.ConvertedOpportunityId);
}
}
List < Account > listAcctForUpdate = new List < Account >();
List < Contact > listConForUpdate = new List < Contact >();
List<Opportunity> listOppForUpdate =new List<Opportunity>();
if ( setAccountIds.size() > 0 ) {
Id vATMRecordTypeId = Schema.SObjectType.Account.getRecordTypeInfosByName().get('VATM').getRecordTypeId();
/* Fetching the accounts which are created as part of lead conversion and record type is not VATM */
for ( Account objAccount : [ SELECT Id, RecordTypeId FROM Account WHERE Id IN: setAccountIds] ) {
/* Setting the Record Type Id to Account's VATM Record Type Id */
if(objAccount.RecordTypeId!=vATMRecordTypeId){
listAcctForUpdate.add( new Account( Id = objAccount.Id, RecordTypeId = vATMRecordTypeId ) );
}
}
}
if ( setOppIds.size() > 0 ) {
Id vATMRecordTypeId = Schema.SObjectType.Opportunity.getRecordTypeInfosByName().get('VATM').getRecordTypeId();
/* Fetching the accounts which are created as part of lead conversion and record type is not VATM */
for ( Opportunity objOpp : [ SELECT Id, RecordTypeId FROM Opportunity WHERE Id IN: setOppIds] ) {
/* Setting the Record Type Id to Account's VATM Record Type Id */
if(objOpp.RecordTypeId!=vATMRecordTypeId){
listOppForUpdate.add( new Opportunity( Id = ObjOpp.Id, RecordTypeId = vATMRecordTypeId ) );
}
}
}
if ( setContactIds.size() > 0 ) {
Id vATMRecordTypeId = Schema.SObjectType.Contact.getRecordTypeInfosByName().get('VATM').getRecordTypeId();
/* Fetching the accounts which are created as part of lead conversion and record type is not VATM */
for ( Contact objCon : [ SELECT Id, RecordTypeId FROM Contact WHERE Id IN: setContactIds ] ) {
/* Setting the Record Type Id to Account's VATM Record Type Id */
if(objCon.RecordTypeId!=vATMRecordTypeId){
listConForUpdate.add( new Contact( Id = ObjCon.Id, RecordTypeId = vATMRecordTypeId ) );
}
}
}
System.debug('listAcctForUpdate '+listAcctForUpdate);
System.debug('listOppForUpdate '+listOppForUpdate);
System.debug('listConForUpdate '+listConForUpdate);
if(listConForUpdate.size()>0){
try{
update listConForUpdate;
System.debug('listAcctForUpdate '+listConForUpdate);
}catch(DmlException e) {
System.debug('The following exception has occurred: ' + e.getMessage());
}
}
if ( listAcctForUpdate.size() > 0 ){
try{
update listAcctForUpdate;
}catch(DmlException e) {
System.debug('The following exception has occurred: ' + e.getMessage());
}
}
if(listOppForUpdate.size()>0){
try{
update listOppForUpdate;
}
catch(DmlException e) {
System.debug('The following exception has occurred: ' + e.getMessage());
}
}
}
}
Kindly mark it as the best answer if it helps.
Regards,
Prateek Prasoon