Can emails be sent to all members of a Team?

We have situations in our application where it would be handy to be able to send emails to all members of a Team.

We don’t see how to do this. Is there a way?

Thanks.

Hi,

This can be solved by making use of the below class. The class uses sendEmail method to send email to all the users of the team by specifying the team id in sql query.

public class sendToTeam
{
public void testMail(Parameters p) throws Exception
{
try{
String attachmentIdList = “”;
String attachmentTemplateIdList = “”;

  String oid=p.get("object_id");
  String rid=p.get("id");
String sql="SELECT us.email FROM user_team AS ut INNER JOIN USER AS us ON ut.user_id=us.id where ut.team_id='e649219790b44038a433856dabd6defb'";
Result ResReq1=Functions.execSQL(sql);
  int nr1=ResReq1.getCode();

  ParametersIterator it=ResReq1.getIterator();
        
  while(it.hasNext())
  {
   Parameters SLAParams = it.next();
   String email=SLAParams.get("email");
           if(email!="" && email!=null){
     Result sendEmailResult = Functions.sendEmail(oid, rid, email,"", "Some subject", 
      "Hello, This email was sent from the Java API", attachmentTemplateIdList, attachmentIdList);
                    }
}

}
catch(Exception e){ }
}
}

(OR) You can make use of the below class also

public class DevUtil {

public void show(String msg) throws Exception { Functions.showMessage(msg+""); }
public void log(String msg) throws Exception { Logger.info(msg, “DevUtil”); }
public void debug(String msg) throws Exception { show(msg); log(msg); }

/**
* Send a notification message to members of My Team with role Agent.
*/
public void notifyAgents(Parameters p) throws Exception
{
String team_id = “1”; // My Team (get ID from Team definition, in the UI)
try {
//Record incoming parameters in the log
//log( “Method params:\n”+ p.toString().replace(",","\n") );

     //String objectID = p.get("object_id");
     //String recordID = p.get("id");  

     // Define the parameters for some operation
     Parameters params = Functions.getParametersInstance();
     params.add("key", "value");

     // Send messages to all active team members
     Result r;
     r = Functions.searchRecords ("USER", "*", "team_id='"+team_id+"' AND active='1'");
     if(r.getCode() < 0) { Functions.throwError( r.getMessage() ); }
     if(r.getCode() == 0) { Functions.throwError( "No users found" ); }
     ParametersIterator users = r.getIterator();
     while (users.hasNext()) {
        // Send a message to the team member, passing on the record so its
        // fields can supply parameters for the email template
        Parameters userParams = users.next();
        //log("Sending message to "+userParams.get("email"));
        sendAgentAnnouncement(p, userParams.get("email")); 
        //log("Message sent");
     }
  } catch (Exception e) {
     String msg = e.getMessage() + "\n methodName(): "+e.getClass().getName();
     log(msg);  
     Functions.throwError(msg);
  }

}

public void sendAgentAnnouncement(Parameters p, String toAddress) throws Exception
{
String emailTemplate = “f03c3324e1f4447dbcbb0cdf61f07c32”; // Agent Announcement template
// Get by modifying the view of templates to include Record IDs
try {
Result r = Functions.sendEmailUsingTemplate(
“cases”, // Object
p.get(“id”), // Case record
toAddress, // To
“”, // cc
“”, // Subject (empty. Use the value in the template
emailTemplate, // “Agent Announcement” template
“”, // List of attachment template IDs
“”); // List of attachment file IDs
if (r.getCode() != 0) { Functions.throwError( r.getMessage() ); }
} catch (Exception e) {
String msg = e.getMessage() + "\n sendMessage(): "+e.getClass().getName();
Functions.throwError(msg);
}

}

}

Thank you very much! Much appreciated.