+ Start a Discussion
John Neilan 2John Neilan 2 

Email Contacts from Process

Hello,

I am trying to invoke an Apex method from a Process that will email a certain set of Contacts associated with an Account that gets marked via a custom field.  I have the class below, but nothing is happening when I change the Account.  The Process is fired, but nothing happens from the Apex.  Does anyone know why?
 
public class VF_ClassComplCustEmail {

    @InvocableMethod
    public static void SendEmailCompl(List<Id> acctId){

    EmailTemplate templateId = [SELECT Id
                                FROM EmailTemplate
                                WHERE Id = '00XL0000000IePcMAK'];
                                
    List<Contact> complCont = [SELECT Id, Non_Comp_Contact__c, Email
                               FROM Contact
                               WHERE Id in: acctId AND Non_Compl_Contact__c = TRUE];


    List<Messaging.SingleEmailMessage> allmsg = new List<Messaging.SingleEmailMessage>();
        for(contact con : complCont)
        {
            Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
                mail.setTemplateID(templateId.Id); 
                mail.setTargetObjectId(con.id);
                mail.setSaveAsActivity(false);
                allmsg.add(mail);
        }
    Messaging.sendEmail(allmsg,false);
    
    }
}

 
Naresh YadavNaresh Yadav
Hi John

On which object your process is ?

If it is on account then below query retuen nothing.
List<Contact> complCont = [SELECT Id, Non_Comp_Contact__c, Email FROM Contact WHERE Id in: acctId AND Non_Compl_Contact__c = TRUE];

If you process on account then use this query.

List<Contact> complCont = [SELECT Id, AccountId, Non_Comp_Contact__c, Email FROM Contact WHERE AccountId in: acctId AND Non_Compl_Contact__c = TRUE];

Mark it as solution if this helps you out.
Thanks
Naresh
John Neilan 2John Neilan 2
Thanks.  I didn't even notice I forgot to include AccountId in my query.  However, it's still not working.  I put in a debug statement to show acctId, and it's coming back as null.  Do I need to set the value of acctId in my Process or in the Class, and how would I do it in the class?
Naresh YadavNaresh Yadav
Hi 

You need to pass account object. See the below code.
@InvocableMethod
public static void Init(List<Account> accRec){
    system.debug('####'+accRec);
}
Mark it as solution if this helps you out.
Thanks
Naresh