How to send an Email to Particular Team Users in AgileApps using Java Classes?

Problem Statement

How to send an email to any users who belongs to particular team using java class in agile apps?

Approach

You have to get all the users who belong to particular team by using the team id and you have to loop over the users and invoke the method to get the email id for users and form one string where the emails will be comma seprated for example abc@gmail.com,def@gmail.com and this string needs to pass to Functions.sendEmailUsingTemplate method will accept object id, record id, and email template id and help to send an email.

Sample Code

Below is the sample java class code for the same:

 import com.platform.api.*;
public class EmailtoTeam
{
	public void sendEmailToTeam(Parameters inParams) {
      try {
      	String recordId = inParams.get("id");
         //Team ID Whom you want to send an email
      	String TeamId = "028739811dc849c4bacf6cff47e2edce";
        //ID for the object
      	String objectId = "46cf2b0f3d4044339bdfb8a6e3c33697";
        //Id for the email template you want to use
      	String emailTemplateId = "a2eb6c8e5f474922b923c6ea656149fa";
        
      	String to = "";
        //Get all user ids from team id
      	String sql = "Select user_id from user_team where team_id='"+TeamId+"'";
      	Result result = Functions.execSQL(sql);
      	int resultCode = result.getCode();
      	if(resultCode <= 0) {        	  
        } else {
    		ParametersIterator userTeamIterator = result.getIterator();
          	while(userTeamIterator.hasNext()) {
         		Parameters userTeamParams = userTeamIterator.next();
              	String userId = userTeamParams.get("user_id");
                  //Call Method to get Email from user id
              	String userEmail = getEmailFromUserId(userId);
              	if(!userEmail.isEmpty()) {
                	to = userEmail+","+to;
           		}
         	}
         	int toLength = to.length();
         	to = to.substring(0, toLength-1);
                  //Send Email to the users string which is to in this case and pass it to sendEmailUsingTemplate method
         	Result emailResult = Functions.sendEmailUsingTemplate(objectId, recordId, to, "", null, emailTemplateId, null, null);
        	
  		}
      } catch(Exception e) {
      Logger.error(e, "com.platform.Orders.servicesendEmailToQMTeam:sendEmailToTeam -- Exception");
      }
  	}
          // Method to get email ids from user ids
	private String getEmailFromUserId(String userId) throws Exception {
  		String userEmail = "";
      	String sql = "Select email from USER where id='"+userId+"'";
      	Result result = Functions.execSQL(sql);
 		int resultCode = result.getCode();
      	if(resultCode == 1) {
      		ParametersIterator userIterator = result.getIterator();
          	while(userIterator.hasNext()) {
          		Parameters userParams = userIterator.next();
              	userEmail = userParams.get("email");
            }
        }
      	return userEmail;
    } 
  
}

Useful links | Relevant resources

https://agileappscloud.info/aawiki/SendEmailUsingTemplate
https://agileappscloud.info/aawiki/Java_API:Email

Thank you.

2 Likes