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
sashamsasham 

Help on inserting records into map to an other map with all the fields

I have a map called memberUpdatmap which has some membe'srecord from member object . key is the id 
Map <Id, member__c> memberUpdatmap = new Map <Id, member__c>();
I have an other map  memberUpdatmap2 which has some member records and some record has same id from memberUpdatmap.
but i do not have all the fields for those records from memberUpdatmap .
I need to update all members at one time (one dml statement) .from both map. how do i get all the records from memberUpdatmap  to memberUpdatmap2.

Example: Pls answer this 
Map <Id, member__c> memberMap = new Map <Id, member__c>();
member__c p1 = new member__c();
p1.Id = id;
p1.put('FirstName__c', 'testaug1');
p1.put('coacher__c', '34561');
p1.put('LastName__c', 'check');
memberMap.put(p1.Id ,p1);
Map <Id, member__c> memberUpdateMap2 = new Map <Id, member__c>();
p2.Id = id;
p2.put('FirstName__c', 'testaug1');
p2.put('Employee__c', 'xyZ');
p2.put('LastName__c', 'check');
memberMap2.put(p2.Id ,p2);
p3.Id = id;
p3.put('FirstName__c', 'testaug2');
p3.put('Employee__c', 'xyZ');
p3.put('LastName__c', 'check1');
memberMap2.put(p3.Id ,p3);

but p2.id and p1.id are the same member object(id's are same) and  i need to get p1 with all the fields Employee__c, coacher__c in to memberMap2
v varaprasadv varaprasad
Hi Sasham,

Please check once below sample code : 
 
Map <Id, member__c> memberMap = new Map <Id, member__c>();
member__c p1 = new member__c();
p1.Id = id;
p1.put('FirstName__c', 'testaug1');
p1.put('coacher__c', '34561');
p1.put('LastName__c', 'check');
memberMap.put(p1.Id ,p1);

Map <Id, member__c> memberUpdateMap2 = new Map <Id, member__c>();
p2.Id = id;
p2.put('FirstName__c', 'testaug1');
p2.put('Employee__c', 'xyZ');
p2.put('LastName__c', 'check');
memberMap2.put(p2.Id ,p2);

p3.Id = id;
p3.put('FirstName__c', 'testaug2');
p3.put('Employee__c', 'xyZ');
p3.put('LastName__c', 'check1');
memberMap2.put(p3.Id ,p3);


for(id id2 : memberUpdateMap2.keyset()){
     if(memberMap.containskey(id2)){
	    member__c mb = memberMap.get(id2);
	 }
  memberMap2.put(id2,mb);
}

//If you list of records then update like below.

//update memberMap2.values();


Hope this helps you!
If my answer helps resolve your query, please mark it as the 'Best Answer' & upvote it to benefit others.

Thanks
Varaprasad
@For  Support: varaprasad4sfdc@gmail.com


 
sashamsasham
Thanks.but i woulk like to have memberMap2  to get all the records from memberMap2 and memberMap the tricky part  is that someof the ids are same from both map and have differnt fields but i need to get all the fields 
Or i can have a map let's say memberMap3 which is going to have all the records from memberMap2 and memberMap but if  any of the id exist in both map , i need to get all fields for the id whcih is from both map.Example p2 and p1 are the id so need get the feilds FirstName__c, coacher__c,LastName__c, Employee__c