I wish to send a mail based on some condition (one of the company field's picklist value is airline) to the people who's email email ids are in custom field . If it is possible how can we send?..
Thanks in advance
Please refer the below code. It might help for your need.
EmailTemplate et; //using email template
Messaging.SingleEmailMessage mail;
String[] sendTo;
Contact cont = [SELECT Id,Name,email from Contact WHERE email != null LIMIT 1];
//Sending emails to defined email template
for(ContractObj__c ct:contracts)
{
mail = new Messaging.SingleEmailMessage();
et = [SELECT Id,Name,Body,HtmlValue,Subject from EmailTemplate ];
sendTo= new String[]{ct.Email__c};
mail.setToAddresses(sendTo);
mail.setTemplateId(et.id);
mail.setWhatId(ct.id);
mail.setTargetObjectId(cont.Id);
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
}
If you need any assistance let me know.
soql the email id of the particular contact and assign it to the setTargetObjectId and also pass the object id [mail.setWhatId(objectId); // Object Id for which data will be sent]
setToAddresses