Skip to main content

currentUserKey()

Last updated 10/05/2023

Example One

The currentUserKey() returns a key (GUID) associated with the currently logged-in user. By setting a global variable from the UserKey variable, the code can later reference this value to perform actions or retrieve data specific to the current user.


Get the key for the current logged in user and store in a global variable
function SetSearchISBN(five: Five, context: any, result: FiveError) : FiveError  {
five.setVariable('ISBN', five.field.ISBN);
five.setVariable('Title', `%${five.field.Title}%`);
five.setVariable('UserKey', five.currentUserKey());
five.refreshTable('Book');
five.refreshTable('iUser');
return five.success(result);
}

Example Two

The code executes a function that gets the iRoleKey from Five's iUser table using the currentUserKey() to return the name of the role and set it as a session variable.


Finding the iRoleKey associated to the current logged in user
function AssignRole(five: Five, context: any, result: FiveError) : FiveError {
const userKey: string = five.currentUserKey();
const userResults = five.executeQuery("SELECT iRoleKey FROM iUser WHERE iUserKey = ?", 1, userKey);
if (userResults.isOk() === false) {
return five.createError(userResults);
}

const roleKey: string = userResults.records[0].iRoleKey;

const sysdb = five.getDatabaseConnectionByID(five.System);
if (sysdb.isOk() === false) {
return five.createError(sysdb);
}

const tx = five.startTransaction(sysdb);
if (tx.isOk() === false) {
return five.createError(tx);
}

const roleResults = five.executeQuery(tx, "SELECT Name from iRole WHERE iRoleKey = ?", 1, roleKey);
if (roleResults.isOk() === false) {
return five.createError(roleResults);
}

const roleName = roleResults.records[0].Name;
five.setVariable("Role", roleName);
return five.success(result);
}

Example Three

The code executes a function that gets the current logged in user from Five's iUser table using the currentUserKey() to return the full name of the dispatch user to be included in the notification delivered to the client (Restaurant).


Get the full name of the current logged in user
function DispatchCompleteOrder(five: Five, context: any, result: FiveError) : FiveError {
if (!context.Transactions) {
return five.success(result);
}

const currentUserKey: string = five.currentUserKey();
const userResults = five.executeQuery("SELECT FullName FROM iUser WHERE iUserKey = ?", 1, currentUserKey);
if (userResults.isOk() === false) {
return five.createError(userResults);
}

const name: string = userResults.records[0].FullName;
const transactions: Array<FormRecord> = context.Transactions;
for (let i: number = 0; i < transactions.length; i++) {
const record = transactions[i];
if (record.ActionID !== 'Orders') {
continue;
}

if (record.Type !== 'UPDATE') {
continue;
}

let completed = record.Values.Completed;
if (completed === false) {
continue;
}

let restaurantKey: string = record.Values.RestaurantKey;
let orderNumber: number = record.Values.OrderNumber;
const userResults = five.executeQuery('SELECT FullName, iUserKey FROM iUser WHERE iUserRecordKey = ?', -1, restaurantKey);
if (userResults.isOk() === false) {
return five.createError(userResults);
}

for (let i: number = 0; i < userResults.recordCount(); i++) {
const userKey: string = userResults.records[i].iUserKey;
const clientName: string = userResults.records[i].FullName;
five.sendNotification(userKey, `Hi ${clientName} order number ${orderNumber} has been completed by ${name}`)
}
}

return five.success(result);
}