We have a script to add a record (client login info) to a table (ComputerLogins). The script asks the user to select which client the new record is for, from a clients table, which is linked to the ComputerLogins table. They are then asked what single-select category (“Portal”) the entry should be assigned. It then creates the record.
Problem is that if a record already exists with the same category, for the same client, it creates a duplicate. We’d like to check the ComputerLogins table to see if (1) the client already has a record and (2) if they do, does it have the same category that the user wants to add?
If it exists, then the record should be updated, if not it should be created.
I’m new to this scripting but I can do the first part and the last part, but I’m having trouble with the comparison code. I’ve tried to figure out if it’s a records query with .includes or some && condition statement or some type of getAsString to do an if A===B then update statement. I think the issue I’m having is that the clients field in the ComputerLogin table is a link, so I’m not able to use a getCellValue(“Clients”) to due a comparison. I’ve tried using the client.id and comparing with a query of the Login table but no luck.
Here’s the current code to collect the input and create the record:
//Pick tables from the base
let logins = base.getTable('ComputerLogins');
let clientlist = base.getTable('Clients');
//Provide instructions
output.markdown('## Be sure Client exists before adding ');
// Select Client from the Clients Table
let selectedClient = await input.recordAsync('Choose Client',clientlist);
// Label an ID to match the client in the different tables
let clientId = selectedClient.id;
//Pull client name from the selected record and assign label "cname"
let cname = selectedClient.name;
// Prompt the user for the USERID info
output.markdown('## Enter Client UserID');
let portalID = await input.textAsync('UID');
// Prompt the user for the Password info
output.markdown('## Enter Client password');
let portalPass = await input.textAsync('Password is case sensitive');
//Create selections for the portal type (from the single-select option in the table)
let logintype = "PC1","PC2","Macbook1"];
//Request the user input based on the selections button
output.markdown('## Select which computer you are providing a login for:');
//Assign label to the selection
let portal = await input.buttonsAsync("Click to select", logintype);
//Add new record to the table
let loginRecord = await logins.createRecordAsync({
'Clients' : {id: clientId}],
'UserID' : portalID,
'Password' : portalPass,
'Portal' : {name: portal},
});
output.markdown(`## ${portal} login added for **${cname}**` );
Once I have the comparison, let’s call it “match”, I would do something like this:
if (match === null) {
let loginRecord = await logins.createRecordAsync({
//Add record to the table
'Clients' : b{id: clientId}],
'UserID' : portalID,
'Password' : portalPass,
'Portal' : {name: portal},
})} else {
let loginRecord = await logins.updateRecordAsync(clientId,
{
//update the client record in the table
'UserID' : portalID,
'Password' : portalPass,
'Portal' : {name: portal},
});
}
Suggestions on how to code finding a match?