Hi Team
Is there a way to create a unique code (like 6 to 8 characters) based on user’s email?
Thanks,
Joey
Hi Team
Is there a way to create a unique code (like 6 to 8 characters) based on user’s email?
Thanks,
Joey
You can try this with a javascript column:
function generateUniqueCode(email) {
// Ensure email is lowercased and trimmed
const normalizedEmail = email.trim().toLowerCase();
// Create a basic unique code by converting each character's char code to a base-36 string
let code = '';
for (let i = 0; i < normalizedEmail.length; i++) {
code += normalizedEmail.charCodeAt(i).toString(36);
}
// Return the first 8 characters for a short unique code
return code.substring(0, 8);
}
const code = generateUniqueCode(p1);
return code;
Oh I haven’t done scripting in Glide before. How to use this script? Thanks by the way @Himaladin !
Ignore it.
Thanks this works!
The error was because you hadn’t filled in the p1
value:
You may also see that error if you have empty rows, but in that case it can be ignored.
Hi @Darren_Murphy , I noticed that it generates the code every time I open my Users table. Is it correct? So if I have 100k users, it will generate it every time?
Don’t worry, it’s just a computed action visible in the Glide table. The code stays consistent and doesn’t generate a new value each time the table is accessed.
Yeah I’m just worried about performance if it always generate the codes…
In your users table are you using row owners? What is the purpose of the code and how will you use it?
If row owners is applied to your users table then most likely the JS only runs on one row. It only appears to run on all of them in the data editor
I’m not using any row owners at the moment… I’m using the generated code to expose in the app instead of the user’s email address… And speaking of row owners, will check on security measures next…
Hola!
You might have problems using this procedure if your customer’s email has more than 6-8 characters. Some coincides can happen and create headaches.
See these 2 examples when you have a code based on 8 chars:
Of course, the @Himaladin’s code doesn’t have any blame of this, it’s its implementation.
Instead, I’d use a RowID to tag each email/row or at least the last 8 characters of the RowID to reduce the chance of having a match by accident. Something like this:
I hope it helps.
Saludos!
Is there a reason you don’t use the rowID?
Thanks @ThinhDinh ! Final is using RowId
how to get the last characters @gvalero ?
You could use the “Slice text” computed column I think.
This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.