Skip to main content
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
2 risposte
  1. 10 feb 2021, 06:12
    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.
0/9000