I have a use case wherein I have to store the case Id, Opportunity Id, Opportunity owner email and Opportunity status in collection and then finding out if case status is already closed, if it is then leave it but send email notification to Opportunity owner if related case is closed. I am thinking about using maps but don't know how to do it.

Select Id, status, Opportunity__r.Owner.email, Opportunity__r.Id, Opportunity__r.IsClosed from Case Where Status = 'Closed Won' OR Status = 'Closed Lost'

up vote 0 down vote accepted
//Assuming the relation is setup between oppty and case 

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']);

//If you are using this in a trigger context 

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
        }
    }
}

//If you have a List of cases already in your code then use this.
for(case c: Listofcases){
    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
        }
    }
}

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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