1) Create a custom metadata type to store Product Information.
Let's say Product_Type_Mapping__mdt
will have 2 columns:
Product_Category__c
- stores SF recognizable Picklist value like. Great Product
Product_Category_Ext__c
- stores External System recognizable picklist value like. greatproduct.
2) Create 2 factory methods which will be used for transformation.
a) When you pass value from SF to External system, use this getProductNameForExternalSystem()
method. If you pass Great Product
, it will return greatproduct
.
public Map<String, String> SFToExternalMap = new Map<String,String>();
public static String getProductNameForExternalSystem(String productCategory){
if(SFToExternalMap.containsKey(productCategory))
{
return SFToExternalMap.get(productCategory);
}
for(Product_Type_Mapping__mdt mdt: [SELECT Product_Category__c,Product_Category_Ext__c
FROM Product_Type_Mapping__mdt]){
if(mdt.Product_Category__c !=null){
if(!SFToExternalMap.containsKey(mdt.Product_Category__c)){
SFToExternalMap.put(mdt.Product_Category__c, mdt.Product_Category_Ext__c); //Great Product---greatproduct
}
}
}
return SFToExternalMap.get(productCategory);
}
b) Similarly, use getProductNameForSF()
for reverse transformation.
public Map<String, String> ExternalMapToSF = new Map<String,String>();
public static String getProductNameForSF(String trimmedproductCategory){
if(ExternalMapToSF.containsKey(trimmedproductCategory))
{
return ExternalMapToSF.get(trimmedproductCategory);
}
for(Product_Type_Mapping__mdt mdt: [SELECT Product_Category__c,Product_Category_Ext__c
FROM Product_Type_Mapping__mdt]){
if(mdt.Product_Category__c !=null){
if(!ExternalMapToSF.containsKey(mdt.Product_Category_Ext__c)){
ExternalMapToSF.put(mdt.Product_Category_Ext__c,mdt.Product_Category__c); //greatproduct----Great Product
}
}
}
return ExternalMapToSF.get(trimmedproductCategory);
}
Just to note that, use of SFToExternalMap
map at class level variable will hold the mapping. So, for the first time it will fire SOQL query and creates a map and second time onwards it will retrieve from the map.