The Now Platform® Washington DC release is live. Watch now!

Help
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Kalaiarasan Pus
Giga Sage

This blog is about a share item (HERE) that I uploaded sometime back and a continuation of the previous blog. After uploading the first release of the library, I received some good feedback and the important one was from @Javier Arroyo, who showed various areas of improvement. The main theme being the boilerplate code present in the library itself. He was also gracious enough to provide me some samples snippets as part of the feedback to help me better understand them.

 

So for the latest release, I have tried to address some of the obvious issues with the code. While this is not the final update, as I still have few ideas to improve the code, the basic structure of the functions and the interfaces are going to remain the same.

 

The main change for the person making use of this library is the format of the function parameters. Since all of the functions had two or three parameters already, I have decided to adopt object as parameters so that any future changes are easy to incorporate and does not break existing functionality. 

 

 

So without wasting time, below are the various functions included in the script currently. I have also included examples of how you can use them on server or client side so that people reading or using this can follow along easily.

 

hasRecord: Utility function that checks if the record exists or not. This can be used both client and server side. 

 

//Server side

var options = {
    table: "incident",
    query: "active=true",
};
var answer = JSON.parse(new CommonLibraryFunctions().hasRecord(options));
gs.print("hasRecord:"+JSON.stringify(answer));

//Client side

var options = {
    table: "incident",
    query: "active=true",
};

var ga = new GlideAjax("CommonLibraryFunctions");
ga.addParam("sysparm_name", "hasRecord");
ga.addParam("sysparm_options", JSON.stringify(options));
ga.getXML(function(response) {
  var answer = JSON.parse(response.responseXML.documentElement.getAttribute("answer"));
  alert("hasRecord:"+JSON.stringify(answer)); 
});

 

getRecord: Utility function that returns the gliderecord object. This can be used on server side.

 

var options = {
    table: "incident",
    query: "active=true",
};
var answer = new CommonLibraryFunctions().getRecord(options);
gs.print("getRecord:"+answer.getRowCount());

 

getField: Utility function that returns a field value of a record. This can be used both client and server side.

 

//Server side

var options = {
    table: "sys_user",
    query: "sys_id=" + gs.getUserID(),
    field: "manager",
};
var answer = JSON.parse(new CommonLibraryFunctions().getField(options));
gs.print("getField:" + JSON.stringify(answer));

//Client side

var options = {
    table: "sys_user",
    query: "sys_id=" + g_user.userID,
    field: "manager",
};

var ga = new GlideAjax('CommonLibraryFunctions');
ga.addParam('sysparm_name', 'getField');
ga.addParam('sysparm_options', JSON.stringify(options));
ga.getXML(function (response) {
    var answer = JSON.parse(response.responseXML.documentElement.getAttribute("answer"));
    alert("getField:" + JSON.stringify(answer));
});

  

getRecordsField: Utility function that returns a field value of multiple records. Return data also includes duplicate. This can be used both client and server side.

 

//Server side

var options = {
    table: "incident",
    query: "active=true",
    field: "number",
};
var answer = JSON.parse(new CommonLibraryFunctions().getRecordsField(options));
gs.print("getRecordsField:" + JSON.stringify(answer));

//Client side

var options = {
    table: "incident",
    query: "active=true",
    field: "number",
};

var ga = new GlideAjax('CommonLibraryFunctions');
ga.addParam('sysparm_name', 'getRecordsField');
ga.addParam('sysparm_options', JSON.stringify(options));
ga.getXML(function (response) {
    var answer = JSON.parse(response.responseXML.documentElement.getAttribute("answer"));
    alert("getRecordsField:" + JSON.stringify(answer));
});

 

getUnique: Utility function that returns list of unique values of records. This can be used both client and server side.

 

//Server side

var options = {
    table: "incident",
    query: "active=true",
    field: "category",
};
var answer = JSON.parse(new CommonLibraryFunctions().getUnique(options));
gs.print("getUnique:" + JSON.stringify(answer));

//Client side

var options = {
    table: "incident",
    query: "active=true",
    field: "category",
};

var ga = new GlideAjax('CommonLibraryFunctions');
ga.addParam('sysparm_name', 'getUnique');
ga.addParam('sysparm_options', JSON.stringify(options));
ga.getXML(function (response) {
    var answer = JSON.parse(response.responseXML.documentElement.getAttribute("answer"));
    alert("getUnique:" + JSON.stringify(answer));
});

  

getAggregate: Utility function that returns the glideaggregate object. This can be used on server side.

 

var options = {
    table: "incident",
    query: "active=true",
};
var answer = new CommonLibraryFunctions().getAggregate(options);
gs.print("getAggregate:"+answer.getRowCount());

 

getCount: Utility function that returns count of records. This can be used on both client and server side.

 

//Server side

var options = {
    table: "incident",
    query: "active=true",
};
var answer = JSON.parse(new CommonLibraryFunctions().getCount(options));
gs.print("getCount:" + JSON.stringify(answer));

//Client side

var options = {
    table: "incident",
    query: "active=true",
};


var ga = new GlideAjax('CommonLibraryFunctions');
ga.addParam('sysparm_name', 'getCount');
ga.addParam('sysparm_options', JSON.stringify(options));
ga.getXML(function (response) {
    var answer = JSON.parse(response.responseXML.documentElement.getAttribute("answer"));
    alert("getCount:" + JSON.stringify(answer));
});

 

executeJob: Utility function to execute a scheduled job. This can be used both client and server side.

log: Utility function used to log error or info message. This can be used both client and server side. 

 

//Server side

new CommonLibraryFunctions().log({message:"Log Message"});

//Client side

var options = {
    message: "Log Message"
};
var ga = new GlideAjax("CommonLibraryFunctions");
ga.addParam("sysparm_name", "log");
ga.addParam("sysparm_options", JSON.stringify(options));
ga.getXML();

 

As always, all kinds of feedback are welcome 🙂

 

NOTE: If you are interested in extending the project, feel free to contribute using Github repo.

If you liked the content, please share, click helpful, bookmark or leave your valuable comments.

1 Comment