Availability__c is a picklist containing Yes and No values

want to know how to put this condition if map keyset contains provided key and its value is NO get inside If condition which is in loop I have used if( newmap.containskey(pbdObj.product__c) =='No' )

    public static List<PriceDetail> getPriceBookDetails(pricebook__c pbdObj){
        Map<Id,String> newmap = new Map<Id,String>();
        for( Combination__c temp:[SELECT Id, product__c, Sales_District__c, Availability__c FROM Combination__c where Sales_District__c =:regCode] )
            newmap.put(temp.product__c, temp.Availability__c);
        pd.productId = pbdObj.product__c;
        if( newmap.containskey(pbdObj.product__c) =='No'  ){
            system.debug('**Inside Map Availability No **');
            pd.avail = false;

1 Answer 1


You have to add 2 conditions in IF ie

  1. Check if the key exists in the map AND

  2. If the value corresponding to the key id 'NO'

    if(newmap.containskey(pbdObj.product__c) && newmap.get(pbdObj.product__c) == 'No')

  • You could skip the containsKey() check in this case, because if the map doesn't contain the key, newmap.get(pbdObj.product__c) == 'No' becomes ` null == 'No'` which is false (and prevents you from entering the If block). It's also safe because there is no .methodCall() or .classVariable being applied (null.<anything> will give you a Null Pointer Exception)
    – Derek F
    Jul 2, 2021 at 11:25

You must log in to answer this question.

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