4

I am trying to update some user data via the admin SDK. I thought this would work

function directoryUpdate(userId, userDept, userLocation, userPhone, userTitle) {
  var update = {
    organizations: 
      {
        name: "Next Step Living",
        title: userTitle,
        primary: true,
        type: "work",
        department: userDept,
        location: userLocation
      },
    phones: 
      {
        value: userPhone,
        type: "work",
        primary: true,
      }
  };
  update = AdminDirectory.Users.update(update, userId);

  Logger.log('User %s updated with result %s.', userId, update)
  return true;
}

but it is not updating the organization or phone data on the record. It also does not throw any kind of error.

three questions, what is the proper syntax to do this update, I assume this works like the API update and behaves like an upsert, is that true, and what is the best practice for capturing any errors during the update. I would like to return a false when the update fails and capture that info. Thanks in advance for your help.

2 Answers 2

6

Thanks for your question! This "inspired" me to work out how the update API worked, as I had got as far as retrieving a User object, updating the properties but had not worked out how to persist the data back to Google.

So, here's my prototype code, which appears to work (the objective being to reset the user's password based on entries in a spreadsheet).

It doesn't seem the most elegant code to me, being that there are two round-trips to the Admin API, and we have to post the email address twice, but I guess that is a side-effect of the JSON API.

var emailAddress = userListSheet.getRange(row, 1).getValue();
var password = userListSheet.getRange(row, 2).getValue();
Logger.log('Email: %s, Password: %s', emailAddress, password);

// Reset user's password
var user = AdminDirectory.Users.get(emailAddress);
user.password = password;
if (changePasswordAtNextLogin == 'Yes') {
  user.changePasswordAtNextLogin = true;
}
AdminDirectory.Users.update(user, emailAddress);
0
0

Figured out the syntax issue. You do need a set of [] around the name value pairs under organization and phones. organizations:[{....}], phones:[{...}]}; and no, at the end of primary: true under phones. Also changed it from an update to a patch but not sure if that was really required;

update = AdminDirectory.Users.patch(update, userId);

And Yes, it did behave like an upsert and modified existing data and added new data just like the API.

Still need to figure out the best way to capture any errors though so if you have any suggestions please post them.

Looks like supplying an invalid email address is a fatal error that can not be caught and dealt with in code. What I did was get all the primary emails out of Google, store them in an array, and validate that the email I was using was in that list prior to running the update. Since everything else is just a string or logical replacement it should not throw any errors so I am confident that the script will not fail. Now all I have to worry about is the time limit.

Not the answer you're looking for? Browse other questions tagged or ask your own question.