function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
vijendahr kvijendahr k 

Dynamically create field mapping in between 2 objects

Hello all, 

  I have one urgent requrement, i have two objects integration_object, and integration_staging object , we have callouts to insert record in integration_staging object.after inserting record in the stageing object, i have to replicate all the values in  integration_object objet . for that i have to crate field mapping dynamically and assign values to it. like 

 stagingreclist= new integration_staging ();
for( integration_staging is: stagingreclist)
{
  integration_object  io= new integration_object ();
i0.f1 = 1s.f1;
i0.f2= is.f3;
  i have to map above fields dynamically , 


}


Can any one help me achive scenario or help me better approch for it. your help is very help full for me.
 
Best Answer chosen by vijendahr k
Amit Chaudhary 8Amit Chaudhary 8
You can create custom setting for mapping and do the code like below
Map<string, string> MapMappingTable=new map<string,string>();
// Get mapping from custom setting
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);
	}
}

for(obj o: test)
{
	acc a= new acc();
	for(String sMyLeadField: MapMappingTable.keySet())
	{
		String sLeadField = MapMappingTable.get(sMyLeadField);
		a.put(sLeadField, MyLead.get(sMyLeadField));
	}

}

 

All Answers

vijendahr kvijendahr k
i0.f1 = 1s.f1;
i0.f2= is.f2;


 Have to do Auto mapp fields like that in the above format.
Amit Chaudhary 8Amit Chaudhary 8
Please check below post for same "Dynamic field mapping using Custom Settings". I hope that will help you
1) http://amitsalesforce.blogspot.com/2014/11/dynamic-field-mapping-using-custom.html

 
vijendahr kvijendahr k
Hello Amit,

 i have already gone through your post, before post quuesion, but in the example its creating dynamic query , at getAllMapping() funcion, but
i need to achive like

for(obj o: test)
{

 acc a= new acc();
a.f1= o.f1;
a.f2= o.f2.....

}

the above mapping shold happen. how can i achive it ?

Thanks for replay amit:)
 
Amit Chaudhary 8Amit Chaudhary 8
You can create custom setting for mapping and do the code like below
Map<string, string> MapMappingTable=new map<string,string>();
// Get mapping from custom setting
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);
	}
}

for(obj o: test)
{
	acc a= new acc();
	for(String sMyLeadField: MapMappingTable.keySet())
	{
		String sLeadField = MapMappingTable.get(sMyLeadField);
		a.put(sLeadField, MyLead.get(sMyLeadField));
	}

}

 
This was selected as the best answer
vijendahr kvijendahr k
Hello amit, thanks for reply but in the above example for loop is cming inside for loop, how can you handle it.