Question

I am trying to accomplish what I imagine would be a fairly simple automation in Google Apps, but I am new to Google Scripts and am having trouble figuring out how the triggers to execute the script work.

Here is some pseudo code describing what I think I need to do:

//When a new group is created execute the function below

function addNewSalesTeamGroupMember() {
  //Get the new group's id/email address
  //Add it as a new member of the Sales Team group (id=SalesTeam@mydomain.com)
}

Here is some background on the task at hand:

I have a Google Apps account that is using Groups as email forwarders. For example, there are 50 groups each with 1 member that serve to provide an email account on the domain for sales team members (e.g. salesguy@mydomain.com is a group whose only purpose is to forward to Sales Guy's third-party email address: salesguy@yahoo.com). There is also another group, SalesTeam@mydomain.com, that contains each of the sales team members, so a manager could make announcements to the whole Sales Team at once.

My task is to write a script that will automatically add any new sales members that are created to the SalesTeam group, as they are created. It thus needs to run when a new sales member is added (i.e. when a new group is made).

I've got the API for updating Groups, but I can't find any resources describing how to write the trigger to tell the script to execute when a new group is created, and to thus be able to retrive the new group's address. In other words, I'm looking to learn how to write this part of my pseudo code in Google Scripts:

//When a new group is created execute the function below
Was it helpful?

Solution

This is doable but you need to do some stuff here. As google apps script cant see if you have created a group you need to compare stuff. First execute this code to grab all your groups and put them in a database:

function startUp() {
  var groups = GroupsManager.getAllGroups();
  var db = ScriptDb.getMyDb();
  for (var a in groups){
    var ob = {group: groups[a].getId()};
    db.save(ob);
  }
}

So now we have a database full of our groups we are going to make a function to check if there are new groups added:

function control() {
  var db = ScriptDb.getMyDb();
  var results = db.query({});
  var groups = GroupsManager.getAllGroups();


  while (results.hasNext()) {
    var result = results.next();
    var group = GroupsManager.getGroup(result.group);
    for (var a in groups){
      if (groups[a].getId() == group.getId()){
        //things stay the same remove it from the groups array
        groups.splice(a,1);
        break;
      }
    }
  }
  //whats left in the array should be a new group
  for (var a in groups){
    //save it first
    var ob = {group: groups[a].getId()};
    db.save(ob);
    //And now tell your script to add this email to the group you want
    GroupsManager.getGroup("SalesTeam@mydomain.com").addMember(groups[a].getId());
  }
}

Now go to your script triggers and setup a time driven trigger on how often you want to execute this function.

With kind regards,

Thomas van Latum

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top