0

I get an compile error when as Invalid Type: Trigger.new error for Line 3 for the following class;

public class NotifyOppyOwnerOnCaseTriggerHandler{

    //Find closed cases and collect EmailIds on Oppty's
    public List<String> findClosedCases(Trigger.new, String TempName ){
       EmailTemplate et = [SELECT Id FROM EmailTemplate WHERE Name =:TempName];
       List<string> toAddress = new List<string>();


        Map<id,Case> case_map = new Map<id,Case>([Select Id, status,
                                                    Opportunity__r.Owner.email,
                                                    Opportunity__r.Id, Opportunity__r.IsClosed
                                                    From Case Where Status = 'Closed Won' OR Status = 'Closed Lost']);


        //Find out how many new cases got closed and collect emailids
        for(case c: trigger.new){
            if(case_map.containsKey(c.id)){
                if(case_map.get(c.id).status == 'Closed Won' || case_map.get(c.id).status == 'Closed Lost'){
                    //send email to the oppty owner by extracting oppty owner using case_map.get(c.id).Opportunity__r.Owner.email
                    //store the email ids of the recipients
                    toAddress.add(case_map.get(c.id).Opportunity__r.Owner.email);
                }
            }
        }
        return toAddress;
    }

    sendNotification(List<string> toAddress){
        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
        mail.setTemplateId(et.Id);
        mail.setToAddresses(toAddress);
        mail.setSubject(et.Subject);
        List<Messaging.SingleEmailMessage> allmsg = new List<Messaging.SingleEmailMessage>();
        allmsg.add(mail);

        //Catch exceptions while sending email to recipients
        try {
            Messaging.sendEmail(allmsg,false);
            return;
        } catch (Exception e) {
        System.debug(e.getMessage());
        }
    }
}
1
  • please also always add the error message you are getting, so it's easier to understand your problem and point out the error.
    – itsmebasti
    Commented Nov 12, 2017 at 11:33

1 Answer 1

0

You need to define the type of your Trigger.new:

public List<String> findClosedCases(List<Case> cases, String TempName ){
    ...
    for(Case c : cases){

and wherever you call it in your trigger, you use Trigger.new:

findClosedCases(Trigger.new, 'foo');
2
  • I replaced this with, public List<String> findClosedCases(List<Case> CaseList, String TempName ) but getting error: Invalid loop variable type expected SObject was Case
    – Deep
    Commented Nov 12, 2017 at 11:39
  • for(case c: trigger.new){ needs to be changed to for(Case c: caseList){
    – itsmebasti
    Commented Nov 12, 2017 at 11:41

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .