1

I am trying to send some welcoming message to new a contact whenever I create. Where I wrote a class which contain email, sender display name and etc. I want to send email using trigger where the trigger contain method in class. But I am getting the following error message. I know why i am getting error. Even I tried a lot to quit form the error. I am unable to retrieve it. Could anyone please provide solution it. Here is the class which I created.

public  class EmailExample {

    public static void mailSending(){
        Contact con= new Contact();
        Contact con1=[select Email from contact where id=:con.Id];
        String mail1=con1.email;
        String[] mail2= new String[]{mail1};  
        Messaging.SingleEmailMessage email= new Messaging.SingleEmailMessage();
        String[] toadd= new String[]{'mail'}; 
        email.setToAddresses(mail2);
        email.setSubject('Test mail');
        email.setSenderDisplayName('KS Kumar');
        email.setPlainTextBody('This is a sample email');
        Messaging.Email[] mail3 = new Messaging.Email[]{email};
        Messaging.sendEmail(mail3);
    }
}

Here it is the trigger:

trigger MailingContactTrigger on Contact (after insert) {
    EmailExample.mailSending();

}

Error which i faced:

Apex trigger MailingContactTrigger caused an unexpected exception, contact your administrator: MailingContactTrigger: execution of AfterInsert caused by: System.QueryException: List has no rows for assignment to SObject: Class.EmailExample.mailSending: line 5, column 1

Thanking you KS Kumar

1

By looking into your question looks like if a contact is inserted you want to sent an email to that contact.

SO you need to pass trigger.New from Trigger to your handler class

like below

trigger MailingContactTrigger on Contact (after insert) {
    EmailExample.mailSending(Trigger.New);

}

Handler code

public  class EmailExample {

    public static void mailSending(List<Contact> lstNewContact)
    {
        Contact con1=[select Email from contact where id IN: lstNewContact LIMIT 1];
        String mail1 = con1.email;
        String[] mail2= new String[]{mail1};  
        Messaging.SingleEmailMessage email= new Messaging.SingleEmailMessage();
        String[] toadd= new String[]{'mail'}; 
        email.setToAddresses(mail2);
        email.setSubject('Test mail');
        email.setSenderDisplayName('KS Kumar');
        email.setPlainTextBody('This is a sample email');
        Messaging.Email[] mail3 = new Messaging.Email[]{email};
        Messaging.sendEmail(mail3);
    }
}

Just for a side note this code will not work for bulk record. This will work only for single record.

Bulkify code

public  class EmailExample {

    public static void mailSending(List<Contact> lstNewContact)
    {
        List<Messaging.SingleEmailMessage>  mail3 = new List<Messaging.SingleEmailMessage>();
        for(Contact objContact: lstNewContact)
        {
            String mail1 = objContact.email;
            String[] mail2= new String[]{mail1};
            Messaging.SingleEmailMessage email= new Messaging.SingleEmailMessage();
            String[] toadd= new String[]{'mail'}; 
            email.setToAddresses(mail2);
            email.setSubject('Test mail');
            email.setSenderDisplayName('KS Kumar');
            email.setPlainTextBody('This is a sample email');
            mail3.add(email);
        }
        Messaging.sendEmail(mail3);        
    }
}
| improve this answer | |
  • 1
    Thanks a lot @Ratan, I tried this and finally i got my required one. You are helping me a lot. Hope it will continue in future also. – KS Kumaar May 23 '16 at 6:08
  • @KSKumar you are welcome. We are always here to help everyone also getting knowledge from others :-) – Ratan Paul May 23 '16 at 6:09

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.