Skip to main content

IMPROVED Sys Admin useful Service Portal widget to check for duplicates in tables

> improved from an earlier post...

(see )

Widget defined in global scope; note can set cross scope privileges for access to scoped tables
[- should generate entries in sys_restricted_caller_access which can then be set to permitted]


<div> <h1>Check ServiceNow Table for duplicates <title>Check ServiceNow Table for duplicates</title> </h1> <div class='container'> <form class="form-horizontal"> <div class="form-group"> <label class="col-sm-2 control-label">Enter table name here</label> <div class="col-sm-10"> <input class="form-control" value ='sys_user' id='tbl_name'> </div> </div> <div class="form-group"> <label class="col-sm-2 control-label">Enter field name here</label> <div class="col-sm-10"> <input class="form-control" value='email' id='dupl_field'> </div> </div> <div class="form-group"> <label class="col-sm-2 control-label">Custom query string</label> <div class="col-sm-10"> <input class="form-control" value='active=true' id='custom_query'> </div> </div> <div class="form-group"> <button ng-click="runtheoperation()" class="btn btn-info">Click to check dupl</button> </div> </form> <h2>Results:</h2> <div class="well"> <textarea id='results_q' rows="10" style="width:100%;">{{c.duplrecordsList}}</textarea> </div> <h2>Total duplicates rows: {{c.duplrecordsSize}}</h2> </div> </div>


.control-label { text-align: left; }



function($scope) { /* widget controller */ var c = this; c.duplrecordsSize=0; c.duplrecordsList=''; $scope.runtheoperation = function() { //console.log("message", 'run the op'); c.server.get({ action: "get_dupls", msg : "Running glideaggregate to check for duplicates...", sn_table : angular.element($('#tbl_name')).val(), duplfield : angular.element($('#dupl_field')).val(), customQuery : angular.element($('#custom_query')).val() }).then(function(r){; var; c.duplrecordsList=duplData.join('\n'); }); }; }


(function() { /* populate the 'data' object */ /* e.g., data.table = $sp.getValue('table'); */ data.duplrecordsList=[]; data.duplrecordsSize=0; if (input && input.action === "get_dupls"){ data.duplrecordsList=(checkDuplUsers(input.customQuery, input.duplfield,input.sn_table)); data.duplrecordsSize=data.duplrecordsList.length; } function checkDuplUsers(customQuery, fieldParam, table){ var ga= new GlideAggregate(table); ga.addAggregate('COUNT', fieldParam); if (customQuery){ ga.addQuery(customQuery); } ga.addHaving('COUNT', '>', 1); ga.query(); var dupls=[]; dupls.push("The following " + ga.getRowCount() + " records are duplicate on " + fieldParam); while ( { var DuplLine=ga.getAggregate('COUNT', fieldParam) + " => " + ga.getElement(fieldParam); dupls.push(DuplLine); } //gs.log(dupls,'duplWidget_arr'); return dupls; } })();


Popular posts from this blog

URL link in addInfoMessage

var ga=new GlideAjax('gld_HR_ajax'); ga.addParam('sysparm_name', 'checkEmployeeNumber_hrProfile'); ga.addParam('sysparm_hrprofilenumber', g_form.getValue('number')); ga.addParam('sysparm_employeenumber', newValue); ga.getXMLAnswer(function(answer) { if (answer!='undefined' && answer!=''){ var navURL="<a style='text-decoration:underline;color:blue'" + answer + ">" + answer + "</a><img width='3' src='images/s.gif'/>"; var sMsg='The employee number entered already exists on another HR Profile ' + navURL; //alert(sMsg); g_form.showErrorBox('employee_number', 'error - please check'); g_form.addInfoMessage(sMsg); } });

GlideRecord setValue

setValue(String name, Object value) Sets the specified field to the specified value. Normally a script would do a direct assignment, for example,  gr.category = value . However, if in a script the element name is a variable, then  gr.setValue(elementName, value)  can be used. When setting a value, ensure the data type of the field matches the data type of the value you enter. This method cannot be used on journal fields. If the value parameter is null, the record is not updated, and an error is not thrown!/api_doc?v=madrid&id=r_GlideRecord-setValue_String_Object