You need to sign in to do that
Don't have an account?
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
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
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.
All Answers
Try this code
And also make sure your email setting is correct in the org which you are working, See the below screenshot
Thanks
Sharat
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
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.
I put a query on user and Put in mail.setTargetObjectId()
I Appriciate your advice and assistance!
Thanks
L.N