I'm trying to add a value to a custom field within a customSchema using Google Apps Script. Whether I add the customSchema using the Google Admin Console, or through Apps Script, I run into the same problem: The customSchema can't be found with the Admin SDK with Apps Script until I put a value in manually using GAC.
function updateCustomSchemaData(userEmail, schemaName, fieldName, newValue) {
// userEmail = 'user@email.com'
// schemaName = 'User_Information'
// fieldName = 'Site_Code'
// newValue = 'test'
try {
var user = AdminDirectory.Users.get(userEmail);
var schemaData = user.customSchemas[schemaName];
schemaData[fieldName] = newValue;
AdminDirectory.Users.update(user, userEmail);
} catch (error) {
Logger.log("Error updating custom schema data: " + error);
}
}
I confirm through GAC that the field exists before running updateCustomSchemaData
.
When running the code, I get the following.
Error updating custom schema data: TypeError: Cannot read properties of undefined (reading 'User_Information')
If I check the output of user
, it doesn't contain any customSchema.
If I edit the field in GAC and add a value to that customSchema manually, var user = AdminDirectory.Users.get(userEmail);
will have the customSchema in it.
"customSchemas": {
"User_Information": {
"Site_Code": "This is a test"
}
},
I can confirm that the schema does exist, since I have a separate, working function to create the schema if it doesn't (using AdminDirectory.schemas.insert
), and as long as the schema is in GAC, even if it's not in user
, I get this:
Failed addSchema with error API call to directory.schemas.insert failed with error: Entity Already Exists: resource.schemaName
TL;DR At first glance, a newly created custom field in a customSchema can't be seen by AdminDirectory.Users.get
, and AdminDirectory.Users.update
can't a value to the field. I'm hoping this is incorrect, and I just don't know how.
Please help me find a way to update a value in a newly created, blank custom field in a customSchema.
UPDATE #1: Current code, doesn't error, but doesn't add the value to Site Code.
function updateUser() {
// update individual user, given email, to make Site Code match buildingId
const user1Email = "user1@email.org";
//const user2Email = 'user2@email.org';
try {
var user = getUser(user1Email);
var userId = user.customerId
const myFunction = () => {
AdminDirectory.Users.update({
"customSchemas": {
"User_Information": {
"Site_Code": "This is a test"
}
}
}, user1Email);
myFunction();
}
var user = getUser(user1Email);
} catch (err) {
console.log('Failed with error %s', err.message);
}
}
The goal is to add a value to a custom field within a customSchema using Google Apps Script
created by Method: schemas.insert
. The Error updating custom schema data: TypeError: Cannot read properties of undefined (reading 'User_Information')
is because Method: users.get
doesn't return customSchemas
if there's no value to it even if projection
is set to full
.
To resolve this, use Method: users.update
directly like this:
const myFunction = () => {
AdminDirectory.Users.update({
"customSchemas": {
"User_Information": {
"Site_Code": "This is a test"
}
}
}, "user@email.com");
}
Note: I noticed that
Method: users.get
only returns a value forcustomSchemas
ifprojection
is set tofull
and there's a value to it.