I am wanting to make a list of all users of the domain whose expiration date has been reached. The Expiration Date (FechaCaducidad) is a custom field that I added from our domain’s Google Workspace dashboard. The structure of the custom field is as follows:
"customSchemas": {
"Organizacion_Interna": {
"NombreFuncionario": "",
"FechaCaducidad": "2022-01-01",
"Departamento": "",
"TipoFuncionario": "",
"Cargo": "",
"Direccion": ""}
The code is the following:
//Controlar y eliminar cuenas caducadas
function VerifVencimiento(){
//Recorrer el directorio de FPUNA y buscar cuentas que han vencido.
//El atributo personalizado FechaCaducidad contiene la fecha de vencimiento de la cuenta.
//Se supone que el usuario ya ha sido avisado de la situación.
var users = [];
var options = {
domain: '',
viewType: 'admin_view',
orderBy: 'givenName',
maxResults: 5,
projection: 'full'
}
spreadsheetID = SpreadsheetApp.getActiveSpreadsheet().getId();
do {
var response = AdminDirectory.Users.list(options);
response.users.forEach(function (user) {
//externalIDs es un arreglo que contiene el employeeID (nro de cedula) en nuestro caso
//para evitar errores se consulta si es NULL, si tiene algún valor se devuelve el valor (CI)
if (user.externalIds) {
var extID = user.externalIds[0].value;
} else {
extID = null;
}
if (user.addresses) {
var direccion = user.addresses[0].formatted;
} else {
direccion = null;
};
//Obtiene el Nro de Telefono
if (user.phones){
var telefono = user.phones[0].value;
};
if(user.emails){
var correoAlt = user.emails[0].address;
}
//Verifica si el usuario está suspendido o activo
if(user.suspended){
estatus = "Suspendido"
}else{
estatus = "Activo"
};
//Verifica si su Fecha de vencimiento ha sido alcanzado
var fechahoy = new Date;
var vencido = "";
if(user.customSchemas.Organizacion_Interna.FechaCaducidad < fechahoy){
// console.log(fechahoy);
vencido = "Vencido";
}
//Formatea la fecha de creación a dia/mes/año
var fecha = Utilities.formatDate(new Date(user.creationTime), "GMT+1", "dd/MM/yyyy");
var lastloging = Utilities.formatDate(new Date(user.lastLoginTime), "GMT+1", "dd/MM/yyyy");
//Si la fecha devuelta es 01/01/1970 indica que nunca inicio sesión.
if (lastloging == "01/01/1970"){
lastloging = "";
};
//Agrega al array users los datos que nos interesan
//telefonoAlternativo = user.emails[0].recoveryPhone;
users.push([extID, user.name.givenName, user.name.familyName, user.primaryEmail,correoAlt, user.orgUnitPath, fecha, lastloging, estatus, telefono, user.recoveryPhone, user.recoveryEmail, direccion,vencido]);
});
// Verifica si aun existen mas dato y actualiza el Token para el siguiente grupo de registros
if (response.nextPageToken) {
options.pageToken = response.nextPageToken;
}
} while (response.nextPageToken);
//Agrega los datos a la hoja de calculo a partir de la Fila 2, columna 1
SpreadsheetApp.openById(spreadsheetID).getSheets()[0].getRange(2, 1, users.length, users[0].length).setValues(users);
}
The result should go to an active Google Sheets sheet.
What could be wrong? any suggestions?Muchas
Thank you so much
Control overdue accounts