Verifying List View selection

General Add comments
by:

One of our customers recently had a requirement that I would like to share here.
The customer wanted three things:

  1. Create a new record directly from the list view;
  2. Relate the newly created record to another table;
  3. Redirect to the new record.

It’s not rocket science but I need to build in some user friendly logic.
The first one, the user has to check one checkbox in the list view.
The second, it’s not allowed to check multiple checkboxes.
The third, if a record has been related previously an error message must appear on the screen.


Note:
You can download the zipped XML file using the download button below.
[download id=”3″]

1. CREATE THE TABLE

The first step of this solution is to create a new table. For this example we create Incident Report with the extend base table TASK.

2. CREATE UI ACTION

Name: Create Report
Table: Incident
Action name: create_report
List Context Menu: True
Client: True
Onclick: listView()
Script:
[cc lang=”javascript”]
function listView() {

var getChecked = g_list.getChecked();//Get checked SYS_ID
split = getChecked.split(“,”);//Split SYS_ID in an array
var countChecked = split.length;//Count length of the array

if (getChecked == false) {
alert(“Please select an item”);

} else if (countChecked == 1) {
glideForm();

} else {
alert(‘You have selected more than one item’);
}
}

function glideForm() {

//GlideAjax Server Call
var ga = new GlideAjax(‘verifyRecord’);
ga.addParam(‘sysparm_name’, ‘verifyRecord’);
ga.addParam(‘sysparm_sys_id’, split);
ga.getXMLWait();

var answerAjax = ga.getAnswer();

if (answerAjax != ‘false’) {
//Javascript confirm box
var answer = confirm(“An Report already exists for this item.\nWould you like to see this item?”);

if (answer) {
//If record exist redirect to the record
window.location = ‘u_incident_report.do?sys_id=’ + answerAjax;
}

} else {
var dialog = new GlideDialogForm(‘Create report’, ‘u_incident_report’, onSubmit);
dialog.setSysID(‘-1’);
dialog.addParm(‘sysparm_view’, ‘default’);
dialog.addParm(‘sysparm_form_only’, ‘true’);

dialog.setLoadCallback(function(iframeDoc) {
var dialogFrame = ‘defaultView’ in iframeDoc ? iframeDoc.defaultView : iframeDoc.parentWindow;
dialogFrame.g_form.setValue(‘u_source_incident’, split);
dialogFrame = null;
});

dialog.render();
}
}

//onSubmit function and redirect to the newley created record
function onSubmit(action, sys_id, table, displayValue) {
window.location = ‘u_incident_report.do?sys_id=’ + sys_id;
}
[/cc]

3. CREATE SCRIPT INCLUDE

Name: verifyRecord
Client callable: True
Script:
[cc lang=”javascript”]
var verifyRecord = Class.create();
verifyRecord.prototype = Object.extendsObject(AbstractAjaxProcessor, {

verifyRecord : function() {
var grinc = new GlideRecord(‘u_incident_report’);
grinc.addQuery(‘u_source_incident’, this.getParameter(‘sysparm_sys_id’));
grinc.query();

if (grinc.next()) {
return grinc.sys_id;
}
return ‘false’;
}
});
[/cc]

If you have any questions you can reach me on email at jeffrey.meijer@2e2.nl

Leave a Reply