Salty Teapot
Salty Teapot

Reputation: 61

GroupsApp.getGroupbyEmail is returning no group found

I am trying to use google apps script to get a group by its email address. There is only one line of code that really matters here.

The line in question is:

var WSGroup = GroupsApp.getGroupByEmail("Group_Email_Address@domain.com");

I know the email address is correct. I know its spelled correctly. I opened a ticket in my admin console with the wonderful Google helpdesk people and confirmed that the group in question has the same settings as another group that I know is working. My question is this. Why can't the code find this group when the same code will find any other groups that have the same permissions settings. Any help would be appreciated before the repeated banging of my head against a wall causes permanent damage.

function getGroups(){
  //gets the current users email address then checks it against all the groups listed below. 
  //When the user is found in that group the proper excel sheet gets their time stamp and their doc is moved to the right folder.
  var check = checkTimes();
  //if(check == 1) {
  //  return 0;
  //}
  //else {
    var user = Session.getActiveUser().getEmail();
    try {
      //the groups
      var CACGroup = GroupsApp.getGroupByEmail("group1@domain.com");
      var HSGroup = GroupsApp.getGroupByEmail("group2@domain.com");
      var JHGroup = GroupsApp.getGroupByEmail("group3@domain.com");
      var JSGroup = GroupsApp.getGroupByEmail("group4@domain.com");
      var CSGroup = GroupsApp.getGroupByEmail("group5@domain.com");
      var WSGroup = GroupsApp.getGroupByEmail("group6@domain.com"); \\var WSGroup using group6@domain is the line in question.
    }
    catch(e){
      DocumentApp.getUi().prompt("There was an error with your group memberships! Report this to tech! Error: " + e);
    }

    var doc = DocumentApp.getActiveDocument();
    var docID = doc.getId();
    //If user is in CAC group timestamp the CAC excel sheet then move the document to the CAC signed docs folder.
    try{
      if(CACGroup.hasUser(user)){
        var googleSheet = SpreadsheetApp.openById('ID of spreadsheet 1');
        setStatus(user, googleSheet, doc);
      }
      if(HSGroup.hasUser(user)){
        var googleSheet = SpreadsheetApp.openById('ID of spreadsheet 2');
        setStatus(user, googleSheet, doc);
      }
      if(JHGroup.hasUser(user)){
        var googleSheet = SpreadsheetApp.openById('ID of spreadsheet 3');
        setStatus(user, googleSheet, doc);
      }
      if(JSGroup.hasUser(user)){
        var googleSheet = SpreadsheetApp.openById('ID of spreadsheet 4');
        setStatus(user, googleSheet, doc);
      }
      if(CSGroup.hasUser(user)){
        var googleSheet = SpreadsheetApp.openById('ID of spreadsheet 5');
        setStatus(user, googleSheet, doc);
      }
      if(WSGroup.hasUser(user)){
        var googleSheet = SpreadsheetApp.openById('ID of spreadsheet 6');
        setStatus(user, googleSheet, doc);
      }
    }
    catch(e){
      var prompt = 'There was an issue accessing the excel sheet for your building!' ;
      DocumentApp.getUi().alert(prompt);
    }
    var prompt = 'Document sent to your main office!';
    DocumentApp.getUi().alert(prompt);
  //}
}

Any user in the domain should be able to find the group and view its members. I have already confirmed these settings in admin.google.com and groups.google.com. The same code can find other groups so the necessary API's are already allowed and turned on.

Upvotes: 2

Views: 1989

Answers (2)

D. Lasa
D. Lasa

Reputation: 11

To follow up on Calre D.'s response, Google Apps Script Documentation says:

getGroupByEmail(email) Retrieves the group having the specified email address. Throws an exception if the group does not exist or if you do not have permission to see it.

https://developers.google.com/apps-script/reference/groups/groups-app#detailed-documentation

Upvotes: 1

Clare D.
Clare D.

Reputation: 21

I was faced with this issue recently and here is what we did:

The account running the script must either be a member/manager/owner of the group you are checking. In your case "group1@domain.com","group2@domain.com" etc...

I am not sure if Google updated the permissions but this solved the problem for us by creating a generic account for handling scripts, adding it to the groups and using that account to run the scripts.

Upvotes: 2

Related Questions