Some time we need to pass one object information to another object. So in this regards developer needs to write a trigger / apex code and needs to define each column mapping in the code. But it may be possible that in future field mapping will change or some new fields will introduce. For that we again need to change code.
In that case we need to create a dynamic field mapping between two object. That functionality we can achieve with custom setting.
In my last project i have done some code for same functionality
Requirement :-
As per client requirement he want to store the Lead information in Pre - Lead and then he want to convert the pre- lead into Lead after some validation.
So for above requirement we have created a object Pre-lead and on same object we have created a button convert lead. while converting the pre- lead into lead we need to provide dynamic field mapping between two object. For that i have created the custom setting "MyLeadToLeadMapping__c" with one custom field "Lead_Field_API_Name__c"
After that i have added the field mapping in custom setting
Code :---
public with sharing class ConvertMyLead
{
public Boolean showmessage{get;set;}
public string MyLeadid{get;set;}
public Lead LeadObj;
string qry = '';
Map<string, string> MapMappingTable=new map<string,string>();
public Pagereference MapLeadfields()
{
Pagereference pageref;
Savepoint sp = Database.setSavepoint();
try
{
LeadObj=new Lead();
MyLeadid=ApexPages.currentPage().getParameters().get('MyLeadid');
getAllMapping();
qry = 'select ' + qry + 'id FROM My_Lead__c where id =: MyLeadid';
My_Lead__c MyLead = Database.query(qry);
for(String sMyLeadField: MapMappingTable.keySet())
{
String sLeadField = MapMappingTable.get(sMyLeadField);
LeadObj.put(sLeadField, MyLead.get(sMyLeadField));
}
LeadObj.OwnerID = UserInfo.getUserId() ;
LeadObj.status='new';
insert LeadObj;
showmessage=true;
pageref=new Pagereference('/'+LeadObj.Id);
return pageref;
}
catch(Exception ex)
{
Database.rollback(sp);
ApexPages.Message msg = new ApexPages.Message(ApexPages.Severity.ERROR, ex.getMessage());
Apexpages.addMessage(msg);
return null;
}
}
public Map<string,string> getAllMapping()
{
qry ='';
try{
for (MyLeadToLeadMapping__c mappingTableRec : MyLeadToLeadMapping__c.getall().Values())
{
if (mappingTableRec.Name != null && mappingTableRec.Lead_Field_API_Name__c != Null )
{
MapMappingTable.put(mappingTableRec.Name , mappingTableRec.Lead_Field_API_Name__c);
qry += mappingTableRec.Name + ',';
}
}
}
catch(exception ex)
{
ApexPages.Message msg = new ApexPages.Message(ApexPages.Severity.ERROR, ex.getMessage());
Apexpages.addMessage(msg);
}
return MapMappingTable;
}
public PageReference goBack()
{
PageReference pf = new PageReference('/'+MyLeadid);
return pf;
}
public boolean getHasErrors()
{
return ApexPages.hasMessages(ApexPages.severity.ERROR);
}
}
In that case we need to create a dynamic field mapping between two object. That functionality we can achieve with custom setting.
In my last project i have done some code for same functionality
Requirement :-
As per client requirement he want to store the Lead information in Pre - Lead and then he want to convert the pre- lead into Lead after some validation.
Solution :
So for above requirement we have created a object Pre-lead and on same object we have created a button convert lead. while converting the pre- lead into lead we need to provide dynamic field mapping between two object. For that i have created the custom setting "MyLeadToLeadMapping__c" with one custom field "Lead_Field_API_Name__c"
After that i have added the field mapping in custom setting
public with sharing class ConvertMyLead
{
public Boolean showmessage{get;set;}
public string MyLeadid{get;set;}
public Lead LeadObj;
string qry = '';
Map<string, string> MapMappingTable=new map<string,string>();
public Pagereference MapLeadfields()
{
Pagereference pageref;
Savepoint sp = Database.setSavepoint();
try
{
LeadObj=new Lead();
MyLeadid=ApexPages.currentPage().getParameters().get('MyLeadid');
getAllMapping();
qry = 'select ' + qry + 'id FROM My_Lead__c where id =: MyLeadid';
My_Lead__c MyLead = Database.query(qry);
for(String sMyLeadField: MapMappingTable.keySet())
{
String sLeadField = MapMappingTable.get(sMyLeadField);
LeadObj.put(sLeadField, MyLead.get(sMyLeadField));
}
LeadObj.OwnerID = UserInfo.getUserId() ;
LeadObj.status='new';
insert LeadObj;
showmessage=true;
pageref=new Pagereference('/'+LeadObj.Id);
return pageref;
}
catch(Exception ex)
{
Database.rollback(sp);
ApexPages.Message msg = new ApexPages.Message(ApexPages.Severity.ERROR, ex.getMessage());
Apexpages.addMessage(msg);
return null;
}
}
public Map<string,string> getAllMapping()
{
qry ='';
try{
for (MyLeadToLeadMapping__c mappingTableRec : MyLeadToLeadMapping__c.getall().Values())
{
if (mappingTableRec.Name != null && mappingTableRec.Lead_Field_API_Name__c != Null )
{
MapMappingTable.put(mappingTableRec.Name , mappingTableRec.Lead_Field_API_Name__c);
qry += mappingTableRec.Name + ',';
}
}
}
catch(exception ex)
{
ApexPages.Message msg = new ApexPages.Message(ApexPages.Severity.ERROR, ex.getMessage());
Apexpages.addMessage(msg);
}
return MapMappingTable;
}
public PageReference goBack()
{
PageReference pf = new PageReference('/'+MyLeadid);
return pf;
}
public boolean getHasErrors()
{
return ApexPages.hasMessages(ApexPages.severity.ERROR);
}
}
No comments:
Post a Comment