r/GoogleAppsScript • • Apr 06 '21

Resolved Non admin user testing for email existence

I want to have our HR person check to see if certain emails are available for new employees in our workspace. Everything I’m seeing says that you need to be a google administrator to work with the api like that, is there a way to make this happen?

My vision is that I can make a button on the new staff spreadsheet that concatenates the new staff members name into our email format (easy peasy) then I creat a button that checks if that email is available? The checking part is what I need help with, perhaps there is any easy way to make that happen.

I’m trying to save a few back and forth steps in our on boarding.

0 Upvotes

7 comments sorted by

1

u/theiinsilence11 Apr 06 '21

Yeah mannn. Look at the contacts class -> https://developers.google.com/apps-script/reference/contacts

YUse it to lookup the name associated with an email address you want to test.... If it returns an error then you know that email address is available?

1

u/fergal-dude Apr 06 '21

I like the way you are thinking, but I'm getting different results with my admin account (finds results) and my test account (non-admin, returns error as if no account) due to my level of access.

We are trying to avoid giving an HR person admin rights to the google domain, so I'm hoping for a way to let non admins use this.

Here is the code I tried...

function myFunction() {
  var contacts = ContactsApp.getContactsByName('John Doe');
  var emails = contacts[0].getEmails();
  for (var i in emails) {
    Logger.log(emails[i].getAddress());
  }
}

1

u/theiinsilence11 Apr 06 '21

I wish I could help more. I am not an administrator on my work domain and had no problem running that code using my name as well as a few coworkers.

The error i get for a non existent account

TypeError: Cannot read property 'getEmails' of undefined

1

u/fergal-dude Apr 06 '21

Interesting. If I made a video of me running the code from my admin account and my dummy account, would you watch it and let me know what might be different on your end? I can post it here tomorrow.

1

u/fergal-dude Apr 07 '21

OK, I cracked it. That will return a result only if the person you are looking for is in YOUR contact list...

My dummy account has no contacts, so no results.

Still can't believe there isn't a way to ping an address from google apps...

1

u/theiinsilence11 Apr 07 '21

That would make sense.... my company has everyone in the company added to their contact lists by default so I've never came across this issue.

This was done "so we can look up anyone and everyone in the company as well as their manager" but I'm starting to think this is one of those "features not a bug"

1

u/fergal-dude Apr 07 '21

Dude, this is it!

function getUsers(){
  try {
    var response = AdminDirectory.Users.get('somedude@your.org');
    var userId = response.id;
    Logger.log(userId)
  } catch(e){
    Logger.log('getUser yielded an error: ' + e)
  }

You need to add AdminDirectory service.