Hi I am working on the scenario, and need some help from the community, kindly help me out for the below .

I have created a below map in my class : -

Private Map <Id, string> testMap =  new Map <Id, string>();

and i am populating the the map as below with the key and value pair,

for(Test_obj__c sId : listOfObject){
                testMap .put(sId.id, sId.TextField);

Values in the above field as below, printed with the help of system.debug;
]|DEBUG|>>>>>>>>>Required Map as follows in batch in Execute Method== 
{a1f3M000000aSCYQUA3=TestValueA, a1f3M000000aSD7Q8M=TestValue4

Question or help needed:-- I need to query all the records as per the Key as Id and Update on particular field in the data base, for example:- a1f3M000000aSCYQUA3--Stored as (sourcetestFiled) with this ID if i have got the 4 records then i need to update the particular field as TestValueA in for all the 4 record and update;

Similarly for the another ID (a1f3M000000aSD7Q8M)

I have done from below approach:-

for(id icId : testMap .keySet()){
            for(Test_obj__c icToUpdate : [select id,TextField, textId__c from Test_obj__c Where textId__c =:icId]){
                icToUpdate.TextField= testMap.get(icId);

Update List

I ahve done from the above approach, need some help to do it in the best possible way. thanks

  • What is the object name that needs to be updated? and what is particular field name? How object needs to be updated and Test_obj__c are related? May 9, 2022 at 12:05
  • Hi I have updated the question
    – Taniya
    May 9, 2022 at 12:34
  • welcome to SFSE! Please see How to Ask on getting the most from the Community. I suggest using edit and putting in a concrete, more detailed example of the values in several records (use a CSV-type layout to illustrate)- I'm unable to ascertain what the business problem is from your description
    – cropredy
    May 9, 2022 at 22:45
  • Apologies ;; But i have solved the issue thanks :)
    – Taniya
    May 10, 2022 at 5:29

2 Answers 2


Purpose of your approach is not clear to me. But it can be optimized in following way to remove query inside for loop and nested for loop.

for(Test_obj__c icToUpdate : [select id,TextField, textId__c from Test_obj__c Where textId__c IN : testMap.keySet()]){
    icToUpdate.TextField= testMap.get(icToUpdate.textId__c);
  • Hi I can have more then one value in the keyset() of my map...and i need to loop through all the values, kindly help me out for this
    – Taniya
    May 9, 2022 at 13:33
  • You can have upto 50,000 values and this query will work. May 9, 2022 at 13:36

Your code looks functional. What I would caution against is making queries inside of a nested loop (you're liable to hit governor limits) and you can do some of this logic more efficiently. If you already have existing SObjects with Ids, there is no need to query for the objects again. In fact, all you need to do is update the field(s) that need updated, and write that to the database. I also don't see a reason to construct a map of your records.

for(SObject obj : someObjects) {
  obj.TestValueA = 'Updated';

update someObjects;

If you do need to query for multiple records, use a WHERE Id IN <collection> clause in your SOQL. Example:

List<Some_Object__c> someObject = [
  SELECT Id, Field1, Field2
  FROM Some_Object__c
  WHERE Id IN :testMap.keySet()

That consumes only 1 query and can return hundreds of records which is much more efficient than querying for one record at a time.

  • Hi Thankyou so much but value incoming in my class for operation an be set of multiple
    – Taniya
    May 9, 2022 at 13:18
  • I'm not sure I understand. It could be helpful to include more code in your original post so I can see what you're trying accomplish.
    – nbrown
    May 9, 2022 at 13:30
  • Sure updating the question
    – Taniya
    May 9, 2022 at 13:34

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .