Thursday, June 27, 2019

ServiceNow parseInt() is returning NaN all the times

This is a common issue and the resolution is to pass both the parameters to the parseInt() method. It accepts the second parameter, which is the base

var testString = "10101"
var number = parseInt(testString, 10) + 100


The second parameter indicates that this integer has to be generated in base10

-------------------------------------------------------------------------------------------------------------
Are you looking to implement custom solutions in ServiceNow?

You are at the right place. Reach us at

Conyx IT Solutions | ServiceNow experts
partners@conyxit.com

Monday, December 17, 2018

Parse the CSV file upload in ServiceNow Service Portal


Body HTML template


<div class="text-center m">
      <span class="file-upload-input">
        <input type="file" style="display: none" multiple="true" ng-file-select="attachmentHandler.onFileSelect($files); filePicked($files);" class="sp-attachments-input">
        <button title="Add attachment"
                ng-click="attachmentHandler.openSelector($event)"
                class="btn btn-primary sp-attachment-add"
                aria-label="Add attachment" role="button">${Upload the custim file}
        </button>
      </span>
</div>


Client Controller Code


$scope.filePicked = function (oEvent) {
docArr = [];

// Get The File From The Input
var oFile = oEvent[0];
var sFilename = oFile.name;

// Create A File Reader HTML5
var reader = new FileReader();

// Ready The Event For When A File Gets Selected
reader.onload = function(e) {
try {
setTimeout(function(){
$rootScope.$broadcast('file-attached',null);
},1000);

var data = e.target.result;
var cfb = XLS.CFB.read(data, {type: 'binary'});

var wb = XLS.parse_xlscfb(cfb);

// Loop Over Each Sheet
wb.SheetNames.forEach(function(sheetName) {
// Obtain The Current Row As CSV
var sCSV = XLS.utils.make_csv(wb.Sheets[sheetName]);
var oJS = XLS.utils.sheet_to_row_object_array(wb.Sheets[sheetName]);
var theanswer = sCSV.split(',');
docArr.push(theanswer);

console.log(theanswer.filter(String)[0]);
$scope.data.output = theanswer.filter(String);
$scope.data.doc_date = theanswer.filter(String)[0];
$scope.data.doc_date_val = theanswer.filter(String)[1];
$scope.data.post_date = theanswer.filter(String)[2];
$scope.data.post_date_val = theanswer.filter(String)[3];
}
}
}
}


Establish Communication between two widgets in ServiceNow - Service Portal

Use Case:

Let us say for example we created a widget to just upload a file and other widget to display the files for the current record. As and when the file is uploaded in the first widget, we need to refresh the attachment list in second widget

This can be done by sending a broadcast message from the client controller in first widget and capturing that signal in the second widget

First Widget

setTimeout(function(){
$rootScope.$broadcast('file-attached',null);
},1000);

Second Widget

$rootScope.$on('file-attached', function(event,data) {
console.log('YYYYYYYYYYYYY  event recevied');
$scope.refreshAttachments();
 })

$scope.refreshAttachments =function() {
$scope.attachmentHandler.setParams("u_custom_table", $scope.data.sys_id, 1024 * 1024 *         $scope.data.maxAttachmentSize);
$scope.attachmentHandler.getAttachmentList();
}




Sunday, December 9, 2018

Avoid duplicate attachments in ServiceNow

This can be achieved by creating a before business rule on the sys_attachment table.

If there is a file with same name, same content type and of same size in bytes then the attachment upload is aborted.

The code for the same is as below


(function executeRule(current, previous /*null when async*/) {
var attach = new GlideRecord('sys_attachment');
attach.addQuery('table_sys_id', current.table_sys_id);
attach.addQuery('table_name', current.table_name);
attach.addQuery('file_name', current.file_name);
attach.addQuery('content_type', current.content_type);
attach.addQuery('size_bytes', current.size_bytes);
attach.query();
if(attach.next()){
current.setAbortAction(true);
}
})(current, previous);




Thursday, October 25, 2018

Mark terminated LDAP users as inactive in ServiceNow


We Find inactive LDAP accounts using the last refresh time. In this method, we add a Last Refreshed field to the user record and set the value during the import process. We create a scheduled job that checks for users that have not been refreshed in 30 days, and deactivate them.

 Create a datetime field on the User [sys_user] table. example, u_last_refreshed.

Add the following code in the transform script

target.u_last_refreshed = gs.now();

Create a scheduled job to find and deactivate the user accounts that have not been refreshed in 30 days.


disable_users();

function disable_users() {
    /*
     * query for active users with ldap source and last updated more than 30 days ago
     * disable them
     */
    var gr = new GlideRecord("sys_user");
    gr.addQuery('u_last_refreshed', '<', gs.daysAgoStart(30));
    gr.addQuery('active', true);
    gr.addQuery('source', '!=', '');
    gr.query();
    while (gr.next()) {
        gr.active = false;
        gs.log("Disabled inactive user: " + gr.user_name + " - last updated: " + gr.u_last_refreshed);
        gr.update();
    }
    gs.log("Completed disabling inactive accounts");
}



Tuesday, October 16, 2018

Script include script is not working from Service Portal catalog client script GlideAjax

Change the privacy setting for a single client-callable script include by adding theisPublic() function.

The isPublic() setting takes precedence over the glide.script.ccsi.ispublic property. For example, if the property is set to false making all client-callable script-includes private, and a script sets isPublic() to true, the script is public.

To change the privacy for a single client-callable script include, add the following method to the script include - The highlighted method.

var RITMUtils = Class.create();
RITMUtils.prototype = Object.extendsObject(AbstractAjaxProcessor, {
isPublic: function () {
return true;
},
type: 'RITMUtils'
});

Tuesday, October 9, 2018

Call a servicenow workflow thorough business rule script

var vars = {};
var w = new Workflow();
var context = w.startFlow('sys_id_of_the_workflow', current, current.operation(), vars);

Sys id of the workflow can be obtained by looking at the wf_workflow table