You need to sign in to do that
Don't have an account?
Can't we send group mails on coustom object by using a triggers.
Hello All
I got some problem on triggers .I am try to send a group mails by using triggers on custom object.
I got this error message"INVALID_TYPE_FOR_OPERATION, Only Users, Contact or Lead allowed for targetObjectId ".
Can't we send group mails on coustom object by using a triggers.PLZ help me any one in this problem.
my code:
trigger RoomExpensesTrig on Rooom_mets__c (after insert) {
if(trigger.isInsert&&trigger.isAfter) {
Rooom_mets__c con1=[Select id from Rooom_mets__c limit 1];
List<Rooom_mets__c> emailID = [select Email_id__c from Rooom_mets__c];
String addresses;
if (emailID[0].Email_id__c!= null) {
addresses = emailID[0].Email_id__c;
for (Integer i = 1; i < emailID.size(); i++){
if (emailID[i].Email_id__c != null)
{
addresses += ':' + emailID[i].Email_id__c;
}
}
}
String[] toAddresses = addresses.split(':', 0);
EmailTemplate et=[Select id from EmailTemplate where name=:'VFTmpToEmail'];
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
mail.setToAddresses( toAddresses );
mail.setTargetObjectId(con1.Id);
mail.setSenderDisplayName('LAKSHMI3VENI');
mail.setTemplateId(et.id);
Messaging.sendEmail(new Messaging.SingleEmailMessage[] {mail});
}
}
As the message says, "only Users, Contact or Lead allowed for targetObjectId ".
The ID you are giving to this method is the ID of a Rooom_mets__c object.
To relate the email to a custom object, I would rather try setWhatId
You have some useful doc here:
http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_classes_email_outbound_single.htm
All Answers
As the message says, "only Users, Contact or Lead allowed for targetObjectId ".
The ID you are giving to this method is the ID of a Rooom_mets__c object.
To relate the email to a custom object, I would rather try setWhatId
You have some useful doc here:
http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_classes_email_outbound_single.htm
Use array of String .
You should collect list of address.
String[] addresses;
This will help you .
Also may be i understood your requirement wrongly .Its never wise to use list as email[0].............
Instead use
int i=0;
for(Rooom_mets__c R:emailID)
{
if (R.Email_id__c!= null) {
addresses[i] = R.Email_id__c;
i++;
}
}
Use loop for list to use any concept.
Your main problem is you have defined address string only .
Make it array and it will solve your issue.
setWhatId(<ID of custom object record>)
setTargetObjectId(<ID of Contact email is being sent to>)
setSaveAsActivity(true)