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
AnnaTAnnaT 

Trigger for Email Alert

Could you help me??

I created the followin trigger which sends e-mail when RFT_Category__c is updated.

But when I try to update it, the error occurrs.

"Apex trigger PDSendEmailtoOC caused an unexpected exception, contact your administrator: PDSendEmailtoOC: execution of AfterUpdate caused by: System.EmailException: SendEmail failed. First exception on row 0; first error: INVALID_ID_FIELD, WhatId is not available for sending emails to UserIds.: []: Trigger.PDSendEmailtoOC: line 26, column 1"

 

trigger PDSendEmailtoOC on RFT_Category__c (after update) {

  List<RFT_Category__c > pdList= [SELECT Location__c,Object_No__c FROM RFT_Category__c ];
  List<Object_Champion__c> ocList=[SELECT Location__c,Object_No__c,Champion_email__c FROM Object_Champion__c];
  
  for (RFT_Category__c rft : Trigger.new) {
    
    String [] locationList = rft.Location__c.split(';');
    Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
    List<String> toAddresses= new List<String>();
    
    //Set recipients
    for(integer i=0; i<locationList.size(); i++){
      for( Object_Champion__c oc :ocList){
        if( oc.Location__c == locationList[i] && oc.Object_No__c ==rft.Object_No__c ){
          toAddresses.add (oc.Champion_email__c );
        }
      }
    }
    mail.setToAddresses(toAddresses);
    mail.setSenderDisplayName('RFT Auto Mail');
    mail.setTemplateId('00yyyyyyyyyyy');
    mail.setWhatId(rft.Id);
    mail.saveAsActivity = false;      
    mail.setTargetObjectId('00xxxxxxxxxxx');
    Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
  }        
}

 How can I solve this error?

 

Thanks in advance for your support!!!

 

Anna

Best Answer chosen by Admin (Salesforce Developers) 
Vinit_KumarVinit_Kumar

I think you are sending email to user as you must be using UserId in setTargetObjectId.As per the below doc:-

 

http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_classes_email_outbound_single.htm

 

setWhatId is only available for Contact object not for User object and hence you are getting the error.

 

You can remove the below line as it is optional,else you have to use a Contact Id in setTargetObjectId.

 

mail.setWhatId(rft.Id);

 

 

All Answers

@anilbathula@@anilbathula@
Hi AnnaT,

Just check this link how to use what id in email messages.
http://stackoverflow.com/questions/11105086/setwhatid-in-salesforce-using-email-template
You can send a email with out using a what id its optional
Check this link:-
http://www.salesforce.com/us/developer/docs/api/Content/sforce_api_calls_sendemail.htm

Vinit_KumarVinit_Kumar

I think you are sending email to user as you must be using UserId in setTargetObjectId.As per the below doc:-

 

http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_classes_email_outbound_single.htm

 

setWhatId is only available for Contact object not for User object and hence you are getting the error.

 

You can remove the below line as it is optional,else you have to use a Contact Id in setTargetObjectId.

 

mail.setWhatId(rft.Id);

 

 

This was selected as the best answer
AnnaTAnnaT
Thank you, Anil and Vinit_Kumar !!
I can use WhatId by using contact ID in setTargetObjectId.

Anna
monikamonika
I have a working code that uses a userid for targetid and a custom object for whatId.
The template has recipientType set to user and relatedTo to the custom object.
I get no exceptions. Why?!