0

I have implemented a table of Contacts with adjacent checkboxes using this Salesforce Wrapper Class. In the table, the contacts are just individual Contact users with their email ids. Command button sends an email to the selected Contacts.

Now, I got a requirement, where I need to show a Group/Folder of Contact records in that table. Whenever I select that group and click Send Email, I should be able to send the email to all of the contacts that comes under that group. How can I implement this functionality?

3
  • My work: I had a Picklist field on Contact records. Each value is a name of a group. I had 6 different values, thus 6 groups can be created. I have queried all the contacts. Used if( ) statement to categorize all the contacts based on the picklist values, saved those records in a List( ). I am not sure how I can take it from here.
    – Arnold Jr.
    Sep 2 '17 at 0:16
  • @ArnoldJr. Post your code what you have tried, that will help to modify and answer it. otherwise user have to answer from the scratch.
    – NITHESH K
    Sep 2 '17 at 8:03
  • @ArnoldJr. You didn't mention anything about the group . Its a custom object or just a fields in Contact ?
    – NITHESH K
    Sep 2 '17 at 8:04
1

Ok so what you need is a class to represent the groupings, and code to assemble everything. Don't bother with 6 if statements. Use a Map to group them together.

public class cContact {
    public Contact con {get; set;}
    public Boolean selected {get; set;}
    public cContact(Contact c) {
        con = c;
        selected = false;
    }
}

public class cGroup {
    public String name {get; set;}
    public List<cContact> cons {get; set;}
    public Boolean selected {get; set;}
    public cGroup(String gName){
        cons = new List<cContact>();
        name = gName;
    }
}

// assume dbCons is a List<Contact> and the group picklist is group__c
// note I haven't dealt with what happens if group__c is empty
Map<String, cGroup> mapGroups = new Map<String, cGroup>();
for (Contact c : dbCons){
    String groupName = c.group__c;
    cGroup cg = mapGroups.get(groupName);
    if (cg == null){
        cg = new cGroup(groupName);
        mapGroups.put(groupName, cg);
    }
    cg.cons.add(new cContact(c));
}
List<cGroup> listGroups = mapGroups.values();

So now you've sorted your contacts into cGroup wrappers that can be selected/deselected, and the constituent cContact wrappers can also be individually selected/deselected. You can add in some code that checks/unchecks the contacts in a group when the group is toggled.

If you need the groups to be sortable you could also look at implementing the Comparable interface (documented elsewhere).

1
  • Seems promising to me. I will try this out and get back to you.
    – Arnold Jr.
    Sep 2 '17 at 4:17
0

See Messaging and Messaging.SingleEmailMessage I'll post code to send to each contact individually, but you can combine them together with setToAddresses:

List<Messaging.SingleMailMessage> messages = new List<Messaging.SingleMailMessage>();
for(WrapperObj wrapper : wrapperObjs)
{
    if(wrapper.checked)
    {
        for(Contact cont : wrapper.contacts)
        {
            Messaging.SingleMailMessage message = new Messaging.SingleMailMessage();
            message.setTargetObjectID(cont.ID);
            //Set other message attributes
            messages.add(message);
        }
    }
}
Messaging.sendEmail(messages);
0

Following the example of wrapper class you cited, you can replace the checkbox field in the original example with a text field named group.

public class cContact {
    public Contact con {get; set;}
    public String group {get; set;}

    public cContact(Contact c) {
        con = c;
        // group is set by the controller
    }
}

In your VF page controller, you retrieve your contacts, build your wrapper list from the contacts and set the group variable in each wrapper item.

When the user selects the group(s) and submits the form, your controller loops through the wrapper list and picks out the contacts to send based on the selected groups.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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