Duplication of Change/Change Tasks Numbers

Arindam Bose
Giga Contributor

Hi All,

We recently have an issue with Duplicate Change Task/ Change Numbers being created in the system. We had raised a HI ticket with Service Now for the same and they suggested for creating a business rule which would check for Duplicates. Can anyone please share the same if already implemented ?

Awaiting response.

Thanks in Advance.

1 ACCEPTED SOLUTION

Ashutosh Munot1
Kilo Patron
Kilo Patron

HI,

What i assume is that, whenever you are trying to create a new record duplicate number is getting inserted and hence you are getting issue. SO you want a script which should not allow a insertion of change task/request if the number already exists. IF so then use below script.

//For CHG Request (Before Insert BR)

var gr = new GlideRecord('change_request');

gr.addQuery('number',current.number);

gr.query();

if(gr.next()){

current.setAbortAction(true);

gs.addInfoMessage('Number is Duplicate');

}


Thanks,
Ashutosh

View solution in original post

3 REPLIES 3

Ankur Bawiskar
Tera Patron
Tera Patron

Hi Arindam,

Following script should help you detect the duplicates

create a fix script for this and put the duplicates in log

script below for change_task

gs.info('Duplicates are: ' + getDuplicates('change_task','number'));
function getDuplicates(tablename,val) {
try{
var dupRecords = [];
var gaDupCheck = new GlideAggregate(tablename);
gaDupCheck.addAggregate('COUNT',val);
gaDupCheck.addNotNullQuery(val);
gaDupCheck.groupBy(val);
gaDupCheck.addHaving('COUNT', '>', 1);
gaDupCheck.query();
while (gaDupCheck.next()) {
dupRecords.push(gaDupCheck[val].toString());
}
return dupRecords;
}
catch(ex){
gs.info('Exception is: ' + ex);
}
}

script below for change_request

gs.info('Duplicates are: ' + getDuplicates('change_request','number'));
function getDuplicates(tablename,val) {
try{
var dupRecords = [];
var gaDupCheck = new GlideAggregate(tablename);
gaDupCheck.addAggregate('COUNT',val);
gaDupCheck.addNotNullQuery(val);
gaDupCheck.groupBy(val);
gaDupCheck.addHaving('COUNT', '>', 1);
gaDupCheck.query();
while (gaDupCheck.next()) {
dupRecords.push(gaDupCheck[val].toString());
}
return dupRecords;
}
catch(ex){
gs.info('Exception is: ' + ex);
}
}

Mark Correct if this solves your issue and also mark Helpful if you find my response worthy based on the impact.
Thanks
Ankur

 

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

Thank you Ankur for your reply.

I could find the duplicate Change/Change Tasks which are there in the system. But i do not want that to happen. Could you please let me know how can  I stop that ?

 

Regards,

Arindam Bose.

Ashutosh Munot1
Kilo Patron
Kilo Patron

HI,

What i assume is that, whenever you are trying to create a new record duplicate number is getting inserted and hence you are getting issue. SO you want a script which should not allow a insertion of change task/request if the number already exists. IF so then use below script.

//For CHG Request (Before Insert BR)

var gr = new GlideRecord('change_request');

gr.addQuery('number',current.number);

gr.query();

if(gr.next()){

current.setAbortAction(true);

gs.addInfoMessage('Number is Duplicate');

}


Thanks,
Ashutosh