function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
laxmi narayan 11laxmi narayan 11 

Send Email issue on custom object.

Hi there,
I have an custom object and has field State__c, When State__c value will Booked. Email Send on associated account. Account is lookup on this Custom Object. Here is my Code:


trigger EmailToPatient on Proposed_Appointment__c (after insert,after update) {
    list<Account> acconts2Update = new list<Account>();
    List<Messaging.SingleEmailMessage> allMails = new List<Messaging.SingleEmailMessage>();
        for(aqthc__Proposed_Appointment__c pro :[SELECT Id,Patient__r.Name,
                                   Patient__r.PersonMobilePhone,Patient__r.PersonEmail,Patient__r.FirstName,Patient__r.LastName,patient__r.id,
                                   aqthc__State__c,Patient__c FROM aqthc__Proposed_Appointment__c
                                   WHERE Id IN: trigger.newMap.keySet()]){
            //Account acc = new Account();
            if(pro.aqthc__State__c == 'Booked'){
             Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
             list<String> toAddresses = new list <string>{pro.Patient__r.PersonEmail};
             mail.setToAddresses(toAddresses);
             system.debug('==========='+toAddresses);
             mail.setTemplateId('00X1U000000uRqr');
             mail.setWhatId(pro.id);
             mail.setBccSender(false);
             mail.setUseSignature(false);
             mail.setReplyTo('Laxminarayan.sfdc@gmail.com');
             mail.setSenderDisplayName('My service');
             mail.setSaveAsActivity(false);
             allMails.add(mail);
       }   
       //acconts2Update.add(acc);
   }
}

Thanks
Best Answer chosen by laxmi narayan 11
Sharat C 2Sharat C 2
Hi,
Since you have Initialized "setWhatId" you have to initialize "setTargetObjectId" as well which should be either Contact / Lead / User Id. Here in the below code, I have initialized with the current logged in user Id at line # 16 so that email will be sent to both "toAddresses" and User's email.
 
trigger EmailToPatient on Proposed_Appointment__c (after insert,after update) {
    list<Account> acconts2Update = new list<Account>();
    List<Messaging.SingleEmailMessage> allMails = new List<Messaging.SingleEmailMessage>();
        for(aqthc__Proposed_Appointment__c pro :[SELECT Id,Patient__r.Name,
                                   Patient__r.PersonMobilePhone,Patient__r.PersonEmail,Patient__r.FirstName,Patient__r.LastName,patient__r.id,
                                   aqthc__State__c,Patient__c FROM aqthc__Proposed_Appointment__c
                                   WHERE Id IN: trigger.newMap.keySet()]){
            //Account acc = new Account();
            if(pro.aqthc__State__c == 'Booked'){
             Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
             list<String> toAddresses = new list <string>{pro.Patient__r.PersonEmail};
             mail.setToAddresses(toAddresses);
             system.debug('==========='+toAddresses);
             mail.setTemplateId('00X1U000000uRqr');
             mail.setWhatId(pro.id);
			 mail.setTargetObjectId (UserInfo.getUserId());
             mail.setBccSender(false);
             mail.setUseSignature(false);
             mail.setReplyTo('Laxminarayan.sfdc@gmail.com');
             mail.setSenderDisplayName('My service');
             mail.setSaveAsActivity(false);
             allMails.add(mail);
		}
		
		if(!allMails.isEmpty())
			List<Messaging.SendEmailResult> result = Messaging.sendEmail(allMails);       
   }
}

 

All Answers

Sharat C 2Sharat C 2
Hi,

Try this code 
 
trigger EmailToPatient on Proposed_Appointment__c (after insert,after update) {
    list<Account> acconts2Update = new list<Account>();
    List<Messaging.SingleEmailMessage> allMails = new List<Messaging.SingleEmailMessage>();
        for(aqthc__Proposed_Appointment__c pro :[SELECT Id,Patient__r.Name,
                                   Patient__r.PersonMobilePhone,Patient__r.PersonEmail,Patient__r.FirstName,Patient__r.LastName,patient__r.id,
                                   aqthc__State__c,Patient__c FROM aqthc__Proposed_Appointment__c
                                   WHERE Id IN: trigger.newMap.keySet()]){
            //Account acc = new Account();
            if(pro.aqthc__State__c == 'Booked'){
             Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
             list<String> toAddresses = new list <string>{pro.Patient__r.PersonEmail};
             mail.setToAddresses(toAddresses);
             system.debug('==========='+toAddresses);
             mail.setTemplateId('00X1U000000uRqr');
             mail.setWhatId(pro.id);
             mail.setBccSender(false);
             mail.setUseSignature(false);
             mail.setReplyTo('Laxminarayan.sfdc@gmail.com');
             mail.setSenderDisplayName('My service');
             mail.setSaveAsActivity(false);
             allMails.add(mail);
		}
		
		if(!allMails.isEmpty())
			List<Messaging.SendEmailResult> result = Messaging.sendEmail(allMails);       
   }
}

And also make sure your email setting is correct in the org which you are working, See the below screenshot
User-added image

Thanks
Sharat
laxmi narayan 11laxmi narayan 11

HI Sharat,

I have an error when i changed status Booked and Click save.

SendEmail failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Missing targetObjectId with template:


Thanks
L.N

Sharat C 2Sharat C 2
Hi,
Since you have Initialized "setWhatId" you have to initialize "setTargetObjectId" as well which should be either Contact / Lead / User Id. Here in the below code, I have initialized with the current logged in user Id at line # 16 so that email will be sent to both "toAddresses" and User's email.
 
trigger EmailToPatient on Proposed_Appointment__c (after insert,after update) {
    list<Account> acconts2Update = new list<Account>();
    List<Messaging.SingleEmailMessage> allMails = new List<Messaging.SingleEmailMessage>();
        for(aqthc__Proposed_Appointment__c pro :[SELECT Id,Patient__r.Name,
                                   Patient__r.PersonMobilePhone,Patient__r.PersonEmail,Patient__r.FirstName,Patient__r.LastName,patient__r.id,
                                   aqthc__State__c,Patient__c FROM aqthc__Proposed_Appointment__c
                                   WHERE Id IN: trigger.newMap.keySet()]){
            //Account acc = new Account();
            if(pro.aqthc__State__c == 'Booked'){
             Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
             list<String> toAddresses = new list <string>{pro.Patient__r.PersonEmail};
             mail.setToAddresses(toAddresses);
             system.debug('==========='+toAddresses);
             mail.setTemplateId('00X1U000000uRqr');
             mail.setWhatId(pro.id);
			 mail.setTargetObjectId (UserInfo.getUserId());
             mail.setBccSender(false);
             mail.setUseSignature(false);
             mail.setReplyTo('Laxminarayan.sfdc@gmail.com');
             mail.setSenderDisplayName('My service');
             mail.setSaveAsActivity(false);
             allMails.add(mail);
		}
		
		if(!allMails.isEmpty())
			List<Messaging.SendEmailResult> result = Messaging.sendEmail(allMails);       
   }
}

 
This was selected as the best answer
laxmi narayan 11laxmi narayan 11
Hi

I put a query on user and Put in mail.setTargetObjectId()

I Appriciate your advice and assistance!

Thanks
L.N