The Zurich release has arrived! Interested in new features and functionalities? Click here for more

Create a unique value for field

Lucy10
Tera Contributor

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

1 ACCEPTED SOLUTION

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);


View solution in original post

8 REPLIES 8

Lucy10
Tera Contributor

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


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);


Many thanks Chuck for your help.



The script works perfectly.


I'm glad you go it working. Thanks for participating in the community Lucy.