+ Start a Discussion
Nirmal ChristopherNirmal Christopher 

Null pointer exception on send email message

I wanted to pick the attachment record from contact and send the attachment in an email. this is the requirement im getting null pointer exception. I checked in debuglogs seems everything is fine. The email should fire on the condition  ID_card_Status__c=='Designer'. Below is my trigger kindly help me asap.
trigger autonumberid on Contact (before insert, before update) {
public string var1='';
public id[] attachmentid=new id[]{};


list<RecordType>ALLRT1=[SELECT DeveloperName,Id,SobjectType FROM RecordType
                       where SobjectType ='Contact' AND DeveloperName='Primary_Member'];
                      
list<RecordType>ALLRT2=[SELECT DeveloperName,Id,SobjectType FROM RecordType
                       where SobjectType ='Contact' AND DeveloperName='Student_Member'];
                      
list<RecordType>ALLRT3=[SELECT DeveloperName,Id,SobjectType FROM RecordType
                       where SobjectType ='Contact' AND DeveloperName='Business_Member'];

list<RecordType>ALLRT4=[SELECT DeveloperName,Id,SobjectType FROM RecordType
                       where SobjectType ='Contact' AND DeveloperName='Pillar_Member'];

list<RecordType>ALLRT5=[SELECT DeveloperName,Id,SobjectType FROM RecordType
                       where SobjectType ='Contact' AND DeveloperName='Life_Member'];
                      
list<RecordType>ALLRT6=[SELECT DeveloperName,Id,SobjectType FROM RecordType
                       where SobjectType ='Contact' AND DeveloperName='Patron_Member'];

list<contact>priList=[select id,Contact_Id__c,Contact_Record_Id__c,RecordTypeId  from contact
                     where Contact_Record_Id__c!=null];
                    
Decimal d1=priList.size();
for(contact c1:trigger.new){
string recordtypeid=c1.recordtypeid;
    if(recordtypeid.contains(ALLRT1[0].id)){
        c1.Contact_Record_Id__c='TN/CHE/PRI-000'+d1++;
    }
    if(recordtypeid.contains(ALLRT2[0].id)){
        c1.Contact_Record_Id__c='TN/CHE/STU-000'+d1++;
    }
    if(recordtypeid.contains(ALLRT3[0].id)){
        c1.Contact_Record_Id__c='TN/CHE/BUS-ME-000'+d1++;
    }
    if(recordtypeid.contains(ALLRT4[0].id)){
        c1.Contact_Record_Id__c='TN/CHE/PIL-000'+d1++;
    }
    if(recordtypeid.contains(ALLRT5[0].id)){
        c1.Contact_Record_Id__c='TN/CHE/LIF-000'+d1++;
    }
    if(recordtypeid.contains(ALLRT6[0].id)){
        c1.Contact_Record_Id__c='TN/CHE/PAT-000'+d1++;
    }
    if(trigger.isupdate){
    if(c1.ID_card_Status__c=='Designer'){
    if(trigger.isupdate){
     Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
      Messaging.EmailFileAttachment efa = new Messaging.EmailFileAttachment();
  
     attachment file=[select id,parentid,body from attachment where parentid=:c1.id AND Name LIKE '%photo%' Limit 1];
     Blob b =file.body ;
      efa.setBody(b);
     String[] emailAddrnew1 = new String[] {'nchristopher@gtr.net'};
    
            attachmentid.add(file.id);
            system.debug('***************'+file.id);
            system.debug('bbbbbbbbbbb'+b);
    
     mail.setToAddresses(emailAddrnew1);
     system.debug('emailAddrnew1'+emailAddrnew1);
      system.debug('eeeeeeeeeeeeeeeeeeeeeeeeeeee'+efa);
     mail.setSubject('New card print request'); 
     mail.setPlainTextBody('print this new id card'); 
     mail.setHtmlBody('This is to notify that the Owner of lead: https://ap1.salesforce.com/ <b>'); 
     mail.setFileAttachments(new Messaging.EmailFileAttachment[] {efa});
     Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail }); 
   
}}}
}
}
Best Answer chosen by Nirmal Christopher
Offshore Freelance ConsultantOffshore Freelance Consultant
Hi,

Below Code works for me.

-------
trigger TrgEmail on Contact (Before Update) {

    public id[] attachmentid=new id[]{};
                  
    for(contact c1:trigger.new){

        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();

        Attachment file= [select id,name, parentid,body,ContentType from attachment where parentid=:c1.id AND Name LIKE '%photo%' Limit 1];

        Messaging.EmailFileAttachment efa = new Messaging.EmailFileAttachment();
      
        efa.setFileName(file.Name);
        efa.setBody(file.Body);
        //efa.setContentType(mAttachment.ContentType);
    
        mail.setFileAttachments(new Messaging.EmailFileAttachment[] {efa});

        String[] emailAddrnew1 = new String[] {'jgsfdc@gmail.com'};
   
        mail.setToAddresses(emailAddrnew1);
        mail.setSubject('New card print request');
        mail.setPlainTextBody('print this new id card');
        mail.setHtmlBody('This is to notify that the Owner of lead: https://ap1.salesforce.com/ <b>');
        Messaging.SendEmailResult [] r =
        Messaging.sendEmail(new Messaging.SingleEmailMessage[] {mail});
    }
}

----------

the only difference is efa.setFileName.

Its good to set the efa.setContentType as well.

Regards,
----------------------------

J G

All Answers

Offshore Freelance ConsultantOffshore Freelance Consultant
Hi,

I am not seeing any use for Before Insert event, if so, please remove that event from trigger definition)

I am not seeing the field Contact_Record_Id__c, is used anywher in the trigger(other than assigning value to it), if not necessary remove those code.

I see 6 queries on same object, make one query and user for loop and 6 lists to assign values.


Also, I see athe query is written inside a for loop, which is not good.


You are looking for second condition, name like '%photo%',  May be there are no records matching the condition.

You may use a condition to check whether the query returns any record, before going into email code.

Run the query in Developer console (with hardcoded contact id and see whether it fetches any records.

select id,parentid,body from attachment where parentid=:c1.id AND Name LIKE '%photo%' Limit 1


Please find out exactly on which line the error is raised and also the values passed for that scenario and then post ur findings.


Regards,

J G



Nirmal ChristopherNirmal Christopher
While inserting a record im doing a field update based on record type is thats the reason i used before insert.  I checked the record id of the list "file" it returned a value in debug logs. I checkd the other variables like "efa" "b" "emailAddrnew1" everything returned a value. yeah its a bad practice to put a query inside a forloop i will remove that. But i am unable to track where the issue exactly is. can you please help me code snippets i will modify the code and try again.
Offshore Freelance ConsultantOffshore Freelance Consultant
Hi,

Below Code works for me.

-------
trigger TrgEmail on Contact (Before Update) {

    public id[] attachmentid=new id[]{};
                  
    for(contact c1:trigger.new){

        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();

        Attachment file= [select id,name, parentid,body,ContentType from attachment where parentid=:c1.id AND Name LIKE '%photo%' Limit 1];

        Messaging.EmailFileAttachment efa = new Messaging.EmailFileAttachment();
      
        efa.setFileName(file.Name);
        efa.setBody(file.Body);
        //efa.setContentType(mAttachment.ContentType);
    
        mail.setFileAttachments(new Messaging.EmailFileAttachment[] {efa});

        String[] emailAddrnew1 = new String[] {'jgsfdc@gmail.com'};
   
        mail.setToAddresses(emailAddrnew1);
        mail.setSubject('New card print request');
        mail.setPlainTextBody('print this new id card');
        mail.setHtmlBody('This is to notify that the Owner of lead: https://ap1.salesforce.com/ <b>');
        Messaging.SendEmailResult [] r =
        Messaging.sendEmail(new Messaging.SingleEmailMessage[] {mail});
    }
}

----------

the only difference is efa.setFileName.

Its good to set the efa.setContentType as well.

Regards,
----------------------------

J G
This was selected as the best answer
Nirmal ChristopherNirmal Christopher
Hi J.G thanks for ur help.
efa.setFileName is it required for all these type of scenarios i thought filename will be defaulted from the attachment.
Offshore Freelance ConsultantOffshore Freelance Consultant
Thanks Nirmal for selecting best answer.

Best Answers & Likes, motivate people who are spending their time, skills & effort to solve other's problems!

JG