- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-05-2017 03:18 AM
I need some assistance on creating a unique value for a fields on the the cmdb table.
I have using a client script on submit to create the alpahnumeric value but I need some help with checking if the value is unique and if not create another value.
My script;
var tagID = g_form.getValue('u_tag');
var text = "";
var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
for (var i = 0; i < 5; i++)
text += possible.charAt(Math.floor(Math.random() * possible.length));
g_form.setValue(tagID, text);
}
Thanks,
Lucy
Solved! Go to Solution.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-05-2017 07:04 AM
Thanks for the update Lucy.
Your code is close... I suspect you go that from the top search of Stack Overflow.
I would put it in a before business rule that only runs on Insert (not update). Check Advanced and the script will look more like this. Granted - I haven't tested this yet.
(function executeRule(current, previous /*null when async*/) {
var text = "";
var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
var unique = false;
var tableName = current.getTableName();
while (!unique) {
for (var i = 0; i < 5; i++)
text += possible.charAt(Math.floor(Math.random() * possible.length));
// Check to see if any other record has this ID in the u_tag field (change field names below if it's not u_tag)
var rec = new GlideRecord(tableName);
if (!rec.get('u_tag', text)) {
unique = true;
}
}
current.u_tag = text;
})(current, previous);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-05-2017 06:58 AM
Hi Chuck,
Its doesn't have to be a client script.
the unique number doesn't have to be visible before submitting.
Is only for new records and doesn't need to be on an empty form.
Thanks,
Lucy

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-05-2017 07:04 AM
Thanks for the update Lucy.
Your code is close... I suspect you go that from the top search of Stack Overflow.
I would put it in a before business rule that only runs on Insert (not update). Check Advanced and the script will look more like this. Granted - I haven't tested this yet.
(function executeRule(current, previous /*null when async*/) {
var text = "";
var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
var unique = false;
var tableName = current.getTableName();
while (!unique) {
for (var i = 0; i < 5; i++)
text += possible.charAt(Math.floor(Math.random() * possible.length));
// Check to see if any other record has this ID in the u_tag field (change field names below if it's not u_tag)
var rec = new GlideRecord(tableName);
if (!rec.get('u_tag', text)) {
unique = true;
}
}
current.u_tag = text;
})(current, previous);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-05-2017 07:32 AM
Many thanks Chuck for your help.
The script works perfectly.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-05-2017 07:33 AM
I'm glad you go it working. Thanks for participating in the community Lucy.