Generate a Unique Code based on User's Email

Hi Team :wave:

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 !

Like this:

I’m getting this error…

Ignore it.

Thanks this works!

1 Like

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.

3 Likes

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.

1 Like

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

1 Like

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:

image

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:

image

I hope it helps.

Saludos!

1 Like

Is there a reason you don’t use the rowID?

Thanks @ThinhDinh ! Final is using RowId :grinning:

how to get the last characters @gvalero ?

Sure!!

Just use this JS code to get the last 8 chars:

return p1.substr(p1.length-8);

Saludos!

2 Likes

You could use the “Slice text” computed column I think.

2 Likes

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.