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
Rakesh M 20Rakesh M 20 

Hi All ,can anyone help me to add values to map of sObject and update the same

Example : this is my map with list of sObject
Map<Id,List<Product_Stock__c>> updateProductStock = new Map<Id,List<Product_Stock__c>>();

Fields required to add a map :
id and available_qty
Best Answer chosen by Rakesh M 20
Malika Pathak 9Malika Pathak 9
Hi Rakesh,

your map consist id and list,
so to fill the map you have to iterate over the Product_Stock__c list:
List<Product_Stock__c> productStockList = new List<Product_Stock__c>();
Map<Id,List<Product_Stock__c>> updateProductStock = new Map<Id,List<Product_Stock__c>>();

for(Product_Stock__c stockObj : productStockList){
     if(!updateProductStock.ContainsKey(stockObj.id)){
      updateProductStock.put(stockObj.id, new List<Product_Stock__c>());
     }
       updateProductStock.get(stockObj.id).add(stockObj);
}
In your Map key will be the id of Product_Stock__c and value would be List of Product_Stock__c that contains all the fields that you have in productStockList.

For more reference, you can go: https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_map.htm

If you find this helpful, kindly mark this as the best answer to help others.

All Answers

Malika Pathak 9Malika Pathak 9
Hi Rakesh,

your map consist id and list,
so to fill the map you have to iterate over the Product_Stock__c list:
List<Product_Stock__c> productStockList = new List<Product_Stock__c>();
Map<Id,List<Product_Stock__c>> updateProductStock = new Map<Id,List<Product_Stock__c>>();

for(Product_Stock__c stockObj : productStockList){
     if(!updateProductStock.ContainsKey(stockObj.id)){
      updateProductStock.put(stockObj.id, new List<Product_Stock__c>());
     }
       updateProductStock.get(stockObj.id).add(stockObj);
}
In your Map key will be the id of Product_Stock__c and value would be List of Product_Stock__c that contains all the fields that you have in productStockList.

For more reference, you can go: https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_map.htm

If you find this helpful, kindly mark this as the best answer to help others.
This was selected as the best answer
Rakesh M 20Rakesh M 20
Thanks Malika Pathak 9