I'm trying to use Google Apps Script to copy a value from a default Google field to a custom attribute that I created in the Google Admin console. As seen by the API, this is a customSchema.
I can't put data into that field, since it's empty by default. The field itself doesn't seem to exist to the API until you put data into it.
I get an error. The error says that it can't find the field. Because it doesn't exist.
I initially tried the following, not knowing the field doesn't exist until you put something in it.
var user = AdminDirectory.Users.get(userEmail,{projection: 'full'});
user.customSchemas.Employee_Information.Site_Code = user.locations[0].buildingId;
AdminDirectory.Users.update(user, userEmail);
Error:
API call to directory.users.update failed with error: Invalid Input: custom_schema
I've since been looking into ways to create the customSchema
within Apps Script, but I haven't found a working solution yet. I'll post the couple methods I've tried, as I'm not sure if I'm on the right track with small errors, or if I'm wildly off the mark.
Here are the thing's I've tried with the relevant errors:
Attempt A:
var schema = {
"schemaName":"Employee_Information",
"displayName":"Employee Information",
"fields":[
{
"fieldName":"Site_Code",
"displayName":"Site Code",
"fieldType":"STRING",
"readAccessType":"ALL_DOMAIN_USERS"
}
]
};
AdminDirectory.Schemas.insert(schema, 'user@email.org');
Error:
GoogleJsonResponseException: API call to directory.schemas.insert failed with error: Bad Request
Attempt B:
var user = AdminDirectory.Users.get(userEmail,{projection: 'full'});
AdminDirectory.Users.update({
'customSchemas': {
'User_Information': {
'Site_Code': [
{ value: 'bloop', type: 'work' },
]
}
}
},
userEmail
}
Error:
API call to directory.users.update failed with error: Invalid Input: custom_schema
Attempt C:
function createAndOverwriteUserSchema(userKey, schemaName, newSchemaFields) {
// Initialize Admin Directory service
var directory = AdminDirectory.newSchema();
// Step 1: Create a new schema object
var newSchema = {
schemaName: schemaName,
fields: newSchemaFields
};
// Step 2: Get the existing user custom schemas
var user = AdminDirectory.Users.get(userKey);
var customSchemas = user.customSchemas || {};
// Step 3: Overwrite the existing schema or add the new one
customSchema[schemaName] = newSchema;
// Step 4: Update the user's custom schemas
var userPatch = {
customSchemas: customSchema
};
AdminDirectory.Users.patch(userPatch, userKey);
Logger.log('Schema has been created and overwritten successfully.');
}
// Example usage:
function testCreateAndOverwriteUserSchema() {
var userKey = 'user@email.org';
var schemaName = 'Employee_Information';
var newSchemaFields = [
{
fieldName: 'Site_Code',
fieldType: 'STRING',
multiValued: false
}
];
createAndOverwriteUserSchema(userKey, schemaName, newSchemaFields);
}
Error:
ReferenceError: customSchema is not defined
You're encountering the error GoogleJsonResponseException: API call to directory.schemas.insert failed with error: Bad Request
in Attempt A
because the code uses an email address 'user@email.org'
which is invalid for the operation.
To resolve this, use the Customer ID
from the Admin console. You may get it by going to Account
> Account Settings
> Profile
or by going to this Admin console
link.
This is what the page should look like:
Since you've been looking into ways to create the customSchema within Apps Script
, here's a method that you may try:
const myFunction = () => {
AdminDirectory.Schemas.insert({
"displayName": "Employee Information",
"fields": [
{
"displayName": "Site Code",
"fieldName": "Site_Code",
"fieldType": "STRING"
}
],
"schemaName": "Employee_Information"
}, "customerId");
}