Skip to main content

Script to prefix duplicate numbers

 belt and braces fix approach:

https://docs.servicenow.com/bundle/xanadu-platform-administration/page/administer/field-administration/concept/c_EnforcingUniqueNumbering.html

but below is a fix script example to fix historic tickets with duplicate numbers


//--sys_ids for duplicate numbers selected based on the latest created of the duplicates var bUpdate = false; bUpdate = true; //--dupl INCIDENTS duplIncidents(bUpdate); //--dupl CHANGE: duplChgRequests(bUpdate); //--dupl ST CHANGE: duplStChgRequests(bUpdate); printFullList(); var bDelete = false; //bDelete=true; if (bDelete) { tidyUpSubProd(bDelete); } function duplIncidents(bUpdate) { var inc_sysids = 'd9f0e709db54a68074abffa9bf961xxx,8894b385db62e60074abffa9bf961xxx,cfbc2ccd4f51364057db0b318110cxxx'; var sysidsARR = inc_sysids.split(','); for (iC = 0; iC < sysidsARR.length; iC++) { renameDuplNumber(sysidsARR[iC].toString(), 'incident', 'd_', bUpdate); } //renameDuplNumber('f4e822d9db0f4f4074abffa9bf961xxx', 'incident', 'd_', bUpdate); } function duplChgRequests(bUpdate) { var chg_sysids = renameDuplNumber('27e1abb41b16c41c89070e9c5e4bcxxx', 'change_request', 'd_', bUpdate); } function duplStChgRequests(bUpdate) { //--dupl STDCHANGE: this record had 3 duplicates renameDuplNumber('202340cc47410200e90d87e8dee49xxx', 'std_change_proposal', 'd_', bUpdate); renameDuplNumber('c407515147810200e90d87e8dee49xxx', 'std_change_proposal', 'd2_', bUpdate); renameDuplNumber('af47d04047810200e90d87e8dee49xxx', 'std_change_proposal', 'd_', bUpdate); } function renameDuplNumber(ticketSYSID, tableName, duplPrefix, bUpdate) { var grDupls = new GlideRecord(tableName); grDupls.addQuery('sys_id', ticketSYSID); grDupls.query(); if (grDupls.next()) { grDupls.autoSysFields(false); //--leave last updated intact grDupls.setWorkflow(false); //--skip business rules and notifications grDupls.setEngines(false); //--skip data policy rules var sNum = grDupls.number; var sNewNum = duplPrefix + sNum; grDupls.number = sNewNum; if (bUpdate) { var sRes = grDupls.update(); gs.print(sRes + ' updated to ' + grDupls.number + ' [' + ticketSYSID + ']'); } else { gs.print('set update flag to true to update --' + ticketSYSID); } } } function printFullList() { var sysids = '227c57f81b52c41c89070e9c5e4bcxxx,27e1abb41b16c41c89070e9c5e4bcxxx,c22f9bc5db54a68074abffa9bf961xxx'; var sysidsARR = sysids.split(','); var tableName = 'task'; for (iC = 0; iC < sysidsARR.length; iC++) { var grDupls = new GlideRecord(tableName); grDupls.addQuery('sys_id', sysidsARR[iC]); grDupls.query(); if (grDupls.next()) { gs.print(grDupls.number); } } } function tidyUpSubProd(bDelete) { instURL = gs.getProperty('glide.servlet.uri'); if (instURL.indexOf('https://prod.service-now.com') > -1) { gs.print('Not to be run on prod!!'); } else { var sQuery = 'numberINCHG50003xx,INC00156zz,INC00250yy,d_INCHG50003xx,d_INC00156zz,d_INC00250yy'; var grDelRecords = new GlideRecord('task'); grDelRecords.addQuery(sQuery); if (!bDelete) { grDelRecords.query(); gs.print('RECORDS TO DELETE: ' + grDelRecords.getRowCount() + '-- target:6'); } else { grDelRecords.deleteMultiple(); } } }

Comments

Popular posts from this blog

ServiceNow check for null or nil or empty (or not)

Haven't tested these all recently within global/local scopes, so feel free to have a play! option 1 use an encoded query embedded in the GlideRecord , e.g.  var grProf = new GlideRecord ( 'x_cls_clear_skye_i_profile' ); grProf . addQuery ( 'status=1^ owner=NULL ' ); grProf . query (); even better use the glideRecord  addNotNullQuery or addNullQuery option 2 JSUtil.nil / notNil (this might be the most powerful. See this link ) example: if ( current . operation () == 'insert' && JSUtil . notNil ( current . parent ) && ! current . work_effort . nil ())  option 3 there might be times when you need to get inside the GlideRecord and perform the check there, for example if the code goes down 2 optional routes depending on null / not null can use gs.nil : var grAppr = new GlideRecord ( 'sysapproval_approver' ); var grUser = new GlideRecord ( 'sys_user' ); if ( grUser . get ( 'sys_id' , current . approver )){...

Code a pause/wait - gs.sleep or gs.wait alternative, pause script for specified seconds (timer)

Code a pause/wait - gs.sleep / gs.wait alternative, pause script for specified seconds (timer)  e.g. 10 seconds: do_sleep ( 10000 ); function do_sleep ( milliseconds ) { var start = new Date (). getTime (); for ( var i = 0 ; i < 1e7 ; i ++) { if (( new Date (). getTime () - start ) > milliseconds ){ gs . print ( 'waking up!' ); break ; } } }