Home > OS >  Can't use variable inside custom schema (Google Apps Script, Google Workspace)
Can't use variable inside custom schema (Google Apps Script, Google Workspace)

Time:11-30

I am creating a few functions related to creating and updating custom schema values for both the domain and its users. I have set a variable for the custom schema that needs to be updated, but, for a specific function, I can't seem to use it. It works for the Logger.log part, but not for the update request. It looks like I must use a hard-coded vale.

This is my function (the variable in question is "schemaSafeName"): `

function updateUserSchemaNew() {
  var myEmailAdress = Session.getActiveUser().getEmail();
  var schemaSafeName = 'Test_Schema_Group';

try{
 AdminDirectory.Users.update({
   'customSchemas': {
     schemaSafeName: {
       'Test_field1':false,
       'Test_field2':true
       }
       }
       },
       myEmailAdress);

  Logger.log(AdminDirectory.Users.get(myEmailAdress,{fields: 'customSchemas',projection: 'Custom',customFieldMask: schemaSafeName}));

  } catch(error){
    const {code, message} = error.details;
    if(code === 400 || code === 404 || code === 409 || code === 412){
      console.log("Error 400 or 404 or 409 or 412");
    } else {
      console.log(`${code} - ${message}`);
    }
  }
}

`

Note that everything works if I keep it harcoded (without using a variable), as shown below, but that defies the purpose of what I'm trying to achieve (a spreadsheet to manage bulk actions on Google Workspace, without having to edit the script every time I need to update a diferent schema). So, this works, but it's static: `

function updateUserSchema() {

 var myEmailAdress = Session.getActiveUser().getEmail();

try{
 AdminDirectory.Users.update({
   'customSchemas': {
     'Test_Schema_Group': {
       'Test_field2':false,
       'Test_field2':true
       }
       }
       },
       myEmailAdress);

  Logger.log(AdminDirectory.Users.get(myEmailAdress,{fields: 'customSchemas',projection: 'Custom',customFieldMask: 'Test_Schema_Group'}));

  } catch(error){
    const {code, message} = error.details;
    if(code === 400 || code === 404 || code === 409 || code === 412){
      console.log("Error 400 or 404 or 409 or 412");
    } else {
      console.log(`${code} - ${message}`);
    }
  }
}

`

Is there a way to do this, or do you think that this is a limitation of the schema update process for users? Or of the way that the values and sub-values are structured?

Thanks in advance for any help.

CodePudding user response:

In your script, how about the following modification?

From:

AdminDirectory.Users.update({
  'customSchemas': {
    schemaSafeName: {
      'Test_field1':false,
      'Test_field2':true
      }
      }
      },
      myEmailAdress);

To:

AdminDirectory.Users.update({
  'customSchemas': {
    [schemaSafeName]: { // <--- Modified
      'Test_field1':false,
      'Test_field2':true
      }
      }
      },
      myEmailAdress);
  • In this modification, schemaSafeName is modified to [schemaSafeName]. By this, var schemaSafeName = 'Test_Schema_Group'; is used as the key.

Reference:

  • Related