+ Start a Discussion
Sumit  61Sumit 61 

I getting null value on final map In trigger

trigger AppLeadCreation on alu_Application__c (before insert, before update) {
    
    // SOQL QUERY FOR ITERATES
    Map<Id,Lead> mapLeads = new Map<Id,Lead>([SELECT Id, Email FROM Lead]);
    System.debug('mapLeads'+mapLeads);
    
    // MAP OF IDs AND EMAIL ADDRESS OF LEAD
    Map<String,Id> mapIdsWithLeadEmail = new Map<String,Id>();
    
    // MAP OF IDs AND EMAIL ADDRESS OF APPLICATION
    Map<String,Id> mapIdsWithAppEmail = new Map<String,Id>();
    
    // MAP OF LEAD IDs AND APPLICATION ID
    Map<Id,Id> mapAppIdsEmailIds = new Map<Id,Id>();
        
    for(Lead le : mapLeads.values()){       
        mapIdsWithLeadEmail.put(le.email, le.Id);
    }
    System.debug('mapIdsWithLeadEmail'+mapIdsWithLeadEmail);
    
    for(alu_Application__c app : trigger.new){
         mapIdsWithAppEmail.put(app.Email__c, app.Id);
    }
    System.debug('mapIdsWithAppEmail'+mapIdsWithAppEmail);
    try{
   
        for(Id tmp : mapIdsWithAppEmail.Values()){
           if(mapIdsWithLeadEmail.ContainsKey(mapIdsWithAppEmail.get(tmp))){ 
               mapAppIdsEmailIds.put(tmp,mapIdsWithLeadEmail.get(tmp));       //Here I getting only ID of Application record not for Lead
            } 
        }
        System.debug('mapAppIdsEmailIds'+mapAppIdsEmailIds); 
     }Catch(Exception e){
        System.debug('ERROR:' + e.getMessage());
        System.debug('ERROR:' + e.getLineNumber());
    }
    
}
Guy Farrer FisherGuy Farrer Fisher
What line do you get the error?
Amit Chaudhary 8Amit Chaudhary 8
Try to update your code like below
trigger AppLeadCreation on alu_Application__c (before insert, before update) {

    Map<String,Id> mapIdsWithAppEmail = new Map<String,Id>();
	Set<String> setEmail = new Set<String>();
    for(alu_Application__c app : trigger.new){
         mapIdsWithAppEmail.put(app.Email__c, app.Id);
		 setEmail.add(app.Email__c);
    }
	
    List<Lead> listLead = [SELECT Id, Email FROM Lead WHERE Email in :setEmail ];
    
    // MAP OF IDs AND EMAIL ADDRESS OF LEAD
    Map<String,Id> mapIdsWithLeadEmail = new Map<String,Id>();
    for(Lead le : listLead ) {       
        mapIdsWithLeadEmail.put(le.email, le.Id);
    }

    Map<Id,Id> mapAppIdsEmailIds = new Map<Id,Id>();
    try{

		for(alu_Application__c app : trigger.new){
			if(mapIdsWithLeadEmail.containsKey(app.Email__c) )	{
				mapAppIdsEmailIds.put( app.id, mapIdsWithLeadEmail.get(app.Email__c) );
			}
		}

        System.debug('mapAppIdsEmailIds'+mapAppIdsEmailIds); 
    }Catch(Exception e){
        System.debug('ERROR:' + e.getMessage());
        System.debug('ERROR:' + e.getLineNumber());
    }
    
}

Let us know if this will help you