0

I have created a Apex trigger on opportunity when the available__C checkbox unchecked then mail needs to go to opportunity Owners manager.. I have written below trigger.Here mail is getting sent but no opp details are coming in mail..I have used Text template with merge field(like opp.name) which are correct

    trigger OnOpportunity on Opportunity (after insert,after update) { 
    set<id>oppId=new set<id>(); 
    map<id,opportunity> OppMap=new map<id,opportunity>(); 
    list<id> userid=new list<id>(); 
    List<Messaging.SingleEmailMessage> emails = new List<Messaging.SingleEmailMessage>(); 
    if (trigger.isinsert && trigger.isAfter){ 
        for(opportunity opp:trigger.new){ 
            oppmap.put(opp.OwnerId,opp);  
        }
    }
    if (trigger.isupdate && trigger.isAfter){ 
        for(opportunity opp:trigger.new){ 
            if(opp.Avialable__c==false && opp.Avialable__c!=trigger.oldmap.get(opp.id).Avialable__c){
                oppmap.put(opp.OwnerId,opp); 
            } 
        } 
    }     
    Map<Id,User> userMap = new Map<Id,User>([select id,manager.email,managerid from user where id in:oppmap.keySet()]);
    for (opportunity opp:trigger.new){ 
        if(userMap.containsKey(opp.ownerid)){ 
            Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage(); 
            mail.setTargetObjectId(userMap.get(opp.ownerid).managerid); 
            mail.setTemplateId('00X28000001Fiuv');     
            mail.setSaveAsActivity(false); 

            Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail }); 
        } 
    } 

}

1 Answer 1

1

in setWhatId method pass Opportunity Id.

List<contact> conList = [select id from contact limit 1];

    Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage(); 
    mail.setTargetObjectId(conList[0].Id); 
    mail.setToAddresses(new List<string>{userMap.get(opp.ownerid).manager.email}); 
    mail.setTreatTargetObjectAsRecipient(false);
    mail.setTemplateId('00X28000001Fiuv'); 
    mail.setWhatid(Opp.Id);    
    mail.setSaveAsActivity(false); 

It will populate the values.

I am passing contact Id because we are using email template.

5
  • Thanks.I added this in code but getting below error ----"Error:Apex trigger OnOpportunity caused an unexpected exception, contact your administrator: OnOpportunity: 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.: [whatId, 0062800000BrhcV]: Trigger.OnOpportunity: line 37, column 1 " Aug 17, 2016 at 9:38
  • @ajaybagdad check my updated answer. Aug 17, 2016 at 9:52
  • 1
    Thanks tushar. Its working..One more thing .do you suggest any changes in above code for best practices ,as i am new to coding. Aug 17, 2016 at 9:59
  • @ajaybagdad one point use trigger handler class to write logic. Other its nice. Aug 17, 2016 at 10:02
  • 1
    Your help is very much appreciated. thanks very much. Aug 17, 2016 at 10:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.