Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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

`

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

>Solution :

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:

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading