• sales manager 983
  • NEWBIE
  • 0 Points
  • Member since 2022

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 0
    Questions
  • 1
    Replies
hello i am writting a trigger to send mail. i am having two objects one is custom and one is contact. on custom object there is lookup relationship with contact. means from custom object we can choose contact. contact will be having email as a mandatory field. when i will choose contact from custom object(Sent feedback) using lookup field then mail will be sent to email address of that contact. can you help me with this?? this is my code. i am not getting mail.

trigger SentFbtrigger on Sent_Feedback__c (after insert) {
    
//Create a master list to hold the emails we'll send

   List<Messaging.SingleEmailMessage> mails = new List<Messaging.SingleEmailMessage>();
    for(Sent_Feedback__c s : Trigger.new) {
    if(s.Contact__r.Email != null && s.Contact__r.Name != null) {
       
        //Create a new Email
      Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
    
      //Set list of people who should get the email
      List<String> sendTo = new List<String>();
      sendTo.add(s.Contact__r.Email);
      mail.setToAddresses(sendTo);
        mail.setTargetObjectId(s.Contact__r.Id);
        
         //Set who the email is sent from
       mail.setReplyTo('james12@ror.com');
      mail.setSenderDisplayName('James R');
    
    
      // Set email contents - you can use variables!
      mail.setSubject('Subject Content');
      String body = 'Dear ' + s.Contact__r.FirstName + ', ';
      body += 'Email Body';
      
      mail.setHtmlBody(body);
    
      //Add your email to the master list
      mails.add(mail);
    }
  }
  // Send all emails in the master list
  Messaging.sendEmail(mails);
}