+ Start a Discussion
SFDC Learning 6SFDC Learning 6 

Issue with Map method

I am trying to get id from value but i dont know why its not working.Can you please identify what is the issue with this

 
trigger MapDEmo on final__c (before insert,before update) {
    
    list<final__c> alist=new list<final__c>();
    integer mont;
    integer yr;
    Integer yyr;
    List<Matrix__c> mtx;
    Integer mnth_to_trade;
    Map<String,Id> matMap =new Map<String,Id>() ;

    
    for(final__c a:trigger.new) {
        alist.add(a);  }
                
 
    
        mtx=[select id,Cumulative__c,Calendar_Month__c,Fiscal_Year__c from database__c ];
        
        for(database__c mm:mtx){
         
          matMap.put(mm.Calendar_Month__c+mm.Fiscal_Year__c,mm.id);
         
        }
     
        
        system.debug('mappppp'+matMap);
        
        for(final__c af:alist) {
        
        
        if(af.Date_Won__c!=null)  {
        mont=af.Date_Won__c.month();
        yr=af.Date_Won__c.year(); 
        yyr=Integer.valueof(string.valueof(yr).right(2));
        }
        System.debug('Year : ' + yyr);
        System.debug('Month : ' + mont);
        
        if(mont < 6) {
            yyr = Integer.valueof((yyr-1) +''+ yyr);}
        else {
            yyr = Integer.valueof(yyr +''+ (yyr+1));
        }   
        
        String final__cl= string.valueof(mont+yyr);
        
                system.debug('mmmyyy'+mont+yyr);

        system.debug('ccccccccccc'+matMap.get(final__cl));
        
        //   af.Cumulative__c = matMap.get(string.valueof(mont+yyr)).Cumulative__c;

             
             } 
       
  
}

I want to get id of Database__c object when value match. Help me ! Thanks in advance
Ashish DevAshish Dev
Last line should be like below
af.Cumulative__c = matMap.get(string.valueof(mont+yyr))

 
SFDC Learning 6SFDC Learning 6
@ashish

tried but its null !!
Ashish DevAshish Dev
I would recommend you to use containskey method of map to check if string matches the key or not. 
SFDC Learning 6SFDC Learning 6
trigger MapDEmo on final__c (before insert,before update) {
    
    list<final__c> alist=new list<final__c>();
    integer mont;
    integer yr;
    Integer yyr;
    List<Matrix__c> mtx;
    Integer mnth_to_trade;
    Map<String,Id> matMap =new Map<String,Id>() ;

    
    for(final__c a:trigger.new) {
        alist.add(a);  }
                
 
    
        mtx=[select id,Cumulative__c,Calendar_Month__c,Fiscal_Year__c from database__c ];
        
        for(database__c mm:mtx){
         
          matMap.put(mm.Calendar_Month__c+mm.Fiscal_Year__c,mm.id);
         
        }
     
        
        system.debug('mappppp'+matMap);
        
        for(final__c af:alist) {
        
        
        if(af.Date_Won__c!=null)  {
        mont=af.Date_Won__c.month();
        yr=af.Date_Won__c.year(); 
        yyr=Integer.valueof(string.valueof(yr).right(2));
        }
        System.debug('Year : ' + yyr);
        System.debug('Month : ' + mont);
        
        if(mont < 6) {
            yyr = Integer.valueof((yyr-1) +''+ yyr);}
        else {
            yyr = Integer.valueof(yyr +''+ (yyr+1));
        }   
        
        String final__cl= string.valueof(mont+yyr);
        
                system.debug('mmmyyy'+mont+yyr);

        system.debug('ccccccccccc'+matMap.get(final__cl));
        
        //   af.Cumulative__c = matMap.get(string.valueof(mont+yyr)).Cumulative__c;

             
             } 

if(matmap.containskey(mont+yyr))
             {
             
             system.debug('ssssssssssssss');
             
             }
       
  
}

Tried before but its also null