2

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, '[email protected]');

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 = '[email protected]';
  var schemaName = 'Employee_Information';
  var newSchemaFields = [
    {
      fieldName: 'Site_Code',
      fieldType: 'STRING',
      multiValued: false
    }
  ];

  createAndOverwriteUserSchema(userKey, schemaName, newSchemaFields);
}

Error:

ReferenceError: customSchema is not defined

1 Answer 1

-1

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 '[email protected]' 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.

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");
}

REFERENCES

Sign up to request clarification or add additional context in comments.

9 Comments

I didn't realize that it was looking for a customerId for the AdminDirectory.schemas.insert function, so thank you for pointing that out. Interestingly, I'm now getting the error API call to directory.schemas.insert failed with error: Entity Already Exists: resource.schemaName which seems to indicate it doesn't like that I've tried to use a schema created in the Google Admin console.
@KyleK. It seems to me that your initial question has already been addressed. Please only ask one question per post. Any followup questions must be posted as new questions instead.
@PatrickdC's statement is correct that updating the created custom schema is considered a new question. Although the title states, How to add data to a new Custom Attribute e.g. customSchema?, the question has two parts. The first is how to create a custom schema, while the second is how to update it.
To update a custom schema, you'll need to use Method: schemas.update instead of Method: users.update.
Understood. Looks like the schemas.update was the missing piece of the puzzle anyways. I'll see if I can figure out how that works in Apps Script. Thanks for the help @Saddles.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.