0

iam trying to insert the data from dataloader getting error as nullpointerexception in line29 column1.

trigger emailmatchdatacontact on ContactRelationship__c (before insert,before update) 
{
    List<ContactRelationship__c> con= new List<ContactRelationship__c>();
    List<Contact> conlist=new List<Contact>();
    Set<String> valtocheck=new Set<String>();
    Map<String,Contact> valfound=new Map<String,Contact>();
    for(ContactRelationship__c cr:trigger.new)
    {
      if(!string.isBlank(cr.EmailId__c)) valToCheck.add(cr.EmailId__c);

    }
    system.debug(valToCheck);
    Map<String, boolean> contactRelationCheckMap = new Map<String, boolean>();
    if(valtocheck != null)
    {
    List<Contact> conrelList = new List<Contact>();
    conrelList = [select id,Email from Contact where Email IN:valtocheck];
    if(conrelList.size()>0)
    { 
    for(Contact c:conrelList)
    {   
        contactRelationCheckMap.put(c.Email, true);
    }
        for(ContactRelationship__c cr:trigger.new) 
        {  
            System.debug('null condition value' + cr.EmailId__c);
            for(Contact c:conrelList)
            {
            if(cr.EmailId__c !=null && contactRelationCheckMap.get(cr.EmailId__c))
            {   
                contact connn=new contact();
                connn.Opt_out_status__c=true; 
                connn.opt_out_transaction__c=system.today();
                connn.id=c.Id;
                conlist.add(connn);
                system.debug(c);
                system.debug(connn);
                system.debug('data available');
                system.debug(contactRelationCheckMap);
                } 

            }
        }
         system.debug('conlistconlistconlistconlist');
         system.debug(conlist);
        // valfound.put(cr.Email,cr);
    } 
     // update conlist;
      //System.debug(conlist);

    }

    try
   {
    system.debug('data available');
    system.debug(conlist);
    update conlist;
    }
    catch(system.NullPointerException e)
    {
        system.debug('data not available');
    }  
}    
  • Which line is 29 – Eric Dec 3 '18 at 7:00
2

This may be due to contactRelationCheckMap not contains the key you are using to get values. Add a containskey() check before getting values from map.

Update your if condition to:

if(cr.EmailId__c !=null && contactRelationCheckMap.containsKey(cr.EmailId__c) && contactRelationCheckMap.get(cr.EmailId__c))
| improve this answer | |
0

The problem is you are querying records here at line conrelList = [select id,Email from Contact where Email IN:valtocheck]; trigger runs before and after insert, this query will get newly inserted records after insert but in case of before insert if valtocheck contains any value that do not have any contact before then contactRelationCheckMap will not contain any value for it. So may be you can use contains() method or split logic for before insert and after insert.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.