Stop duplicate records from being created

Mikolz
Kilo Contributor

Hi everyone, I have a UI action that is on the task table and when clicked creates a record on a user created table (u_m2m_time_card). When a record is created is it bringing over the task number and who created the record. The problem I am having is with duplicate entires. Users can hit this UI action multiple times and a new record will be inserted each time. I want to limit that and only one record with the same task number and same user can be created. The same task number can be there, just as long as there isnt one with the same user ID already there. I am struggling to make that happen. Should I do this in the current UI action code? or should I make a new business rule? Here is the code behind the UI action


var ID = current.sys_id.toString();
var User = gs.getUserID();


var card = new GlideRecord('u_m2m_time_card');
card.initialize();
card.u_task = ID;
card.u_user = User;
card.insert();
gs.addInfoMessage("This task has been added to your queue.");
action.setRedirectURL(current);

I tried a couple approaches without any success. Any help? Thanks!
5 REPLIES 5

derek_wada
Mega Contributor

I've modified your UI Action to include a check on the u_m2m_time_card table for any duplicate u_task values with the same user. I also changed your User variable to grab the Opened By value instead of the user who clicked the button.



createTask();
function createTask() {
var ID = current.sys_id.toString();
var User = current.opened_by;

var card = new GlideRecord('u_m2m_time_card');
card.addQuery('u_task',current.sys_id);
card.addQuery('u_user',User);
card.query();

if (!checkDuplicate(User,card)) {
card.initialize();
card.u_task = ID;
card.u_user = User;
card.insert();
gs.addInfoMessage("This task has been added to your queue.");
} else {
gs.addInfoMessage("Duplicate task already found.");
}
action.setRedirectURL(current);
}

function checkDuplicate(usr,gr) {
if (gr.next()) {
return true;
}
return false;
}


Mikolz
Kilo Contributor

Thanks a lot Derek, it works perfect!


Mikolz
Kilo Contributor

There is one more thing to ask you, when a user adds these tasks to their queue, they will sit in that table until the user creates a timecard for a particular week, from there they have an option to update their time card with all tasks they have added from this queue with the click of a buttom. Ideally I would like a category (u_category) field on the m2m table to be set. The problem is, that field has three options to potentially set to. Could I create a business rule that references the prefix of the number (ie, INC, GREQ, CHG, PRJ, etc) and based off of that, set the category to the correct one? The options for that field are Project, Support, and Other. I have a small business rule that is setting the category, but it is doing so incorrectly.



funcOther();

function funcOther()
{
var gr = new GlideRecord('u_m2m_time_card');
var ID = current.u_task.toString();
gr.addQuery('ID', 'STARTSWITH', "INC");
gr.query();

while(gr.next())
{
gr.u_category = 2; //value of support
gr.update();
}
action.setRedirectAction(current);
}



I think it is setting incorectly, becuase it is not referencing the number prefix for each record correctly. Ideally, this would be great so the user has less interaction with their tasks. I uploaded a couple screenshots to give you a visual.


Mikolz
Kilo Contributor

you know what I got it working. I just created a seperate business rule. Thanks for your time!