Skip to content

Validate a data field #1081

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# update a record in sys_user table

If we use the command below to update a record, it can lead to a problem.

grUser.get('grUser.get('62826bf03710200044e0bfc8bcbe5df9')');

If the record is not found in the table, the script will create a new one.

To make sure we are updating and not inserting, it is better to wrap up the get method with an If statement.
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
var grUser = new GlideRecord('sys_user');

if (grUser.get('62826bf03710200044e0bfc8bcbe5df9')) {
grUser.user_name = 'test.user';
grUser.first_name = 'test';
grUser.last_name = 'user';
grUser.email = 'test.user@servicenow';
grUser.update();
} else {
gs.info('Record not found.');
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
function onLoad() {

var taskId = getParameterValue("taskid");

if (taskId != "" && taskId != null && taskId != undefined) {
console.log('=== CAMACHO Task id: ' + taskId);

var gaGetTaskNumber = new GlideAjax('UtilsAjax');
gaGetTaskNumber.addParam('sysparm_name', 'getTaskNumber');
gaGetTaskNumber.addParam('sysparm_task_id', taskId);
gaGetTaskNumber.getXMLAnswer(setmyFormValue);
}
}

function setmyFormValue(answer) {

//console.log('=== CAMACHO Entered setmyFormValue');
if (answer) {
var obj = JSON.parse(answer);
var numero = obj.number.toString();
console.log(numero);

g_form.setValue('task_number', numero);

}
}

function getParameterValue(name) {
var url = top.location.href;
var value = new URLSearchParams(url).get(name);
if (value) {
return value;
} else {
return null;
}
}
37 changes: 37 additions & 0 deletions Catalog Client Script/Set fields from URL Parameter 2/KMXOUtils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
var KMXOUtils = Class.create();
KMXOUtils.prototype = {
initialize: function() {
},

/*
* Receives a um sys_id and returns a Task table field value
*
* @param {String} - taskId
* @return {Object}
*/
getTaskNumber: function(taskId)
{
if (taskId != "" && taskId != null && taskId != undefined) {

var grTask = new GlideRecord('x_770214_consultor_rwd_activity');

if (grTask.get(taskId)) {

var number = grTask.getValue('number');

var obj = {};
obj["number"] = number;

return obj;

} else {

return {};

}

}
},

type: 'KMXOUtils'
};
11 changes: 11 additions & 0 deletions Catalog Client Script/Set fields from URL Parameter 2/UtilsAjax.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
var UtilsAjax = Class.create();
UtilsAjax.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {

getTaskNumber: function() {
var taskId = this.getParameter('sysparm_task_id');
gs.debug('=== Camacho UtilsAjax = Received the sys_id ' + taskId);
return JSON.stringify(new KMXOUtils().getTaskNumber(taskId));

},
type: 'UtilsAjax'
});
27 changes: 27 additions & 0 deletions Catalog Client Script/Set fields from URL Parameter 2/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Set fields on a catalog item from URL parameters.

The mission was to get a sys_id from the URL, query a record and return a value to the front-end.

In the front-end we have a Record Producer (RP) with a String field.

On the onLoad event, we want to populate the field in order to show the value retrieved from the back-end.

The Use Case is defined so let's go to the step by step process.

1. Create a Util class in the back-end

Our Utils class will be a Script Include that receives a sys_id and returns a String.

[KMXOUtils](KMXOUtils.js)

2. Create a class to provide access for the Front-End

2.1. To provide access in this class, the parameter Client callable should be True (checked)

[UtilsAjax](UtilsAjax.js)

3. I'll suppose that you have a String field called task_number in your RP

3.1. Create a Catalog Client Script (Type: OnLoad) to get the URL parameter and call the back-end class:

[CatalogClientScript](CatalogClientScript.js)
40 changes: 40 additions & 0 deletions Service Portal/validate-data-field/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@

# Validating a data field in a Service Portal

Use Case: When submitting a Record Producer, a date field can’t be in the past. How to accomplish this?

We can create a Catalog Client Script.

1) What is the trigger?

The script will run when the date field value changed.

2) What do we need to confirm?

We need to check if the date provided is in the future.

3) What if the date is not in the future?

If the date is not in the future, then we show an error message to inform that the date cannot be in the past.

# Step by step process

1) Create a Record Producer (RP) and define the Catalog and Category so it will be visible in a Service Portal

2) Create a variable in your RP. In this example, the variable will have this configuration:

Type: Date
Question: Project Deadline
Name: project_deadline
Mandatory: Yes (checked)

3) Create a Catalog Client Script in your RP to check if the project_deadline value changed. If the date is in the past, show an error message to the user.

Your Catalog Client Script will look like this:

Name: Validate Project Deadline
Applies to: A Catalog Item
UI Type: Mobile / Service Portal
Type: onChange
Variable name: project_deadline

23 changes: 23 additions & 0 deletions Service Portal/validate-data-field/script.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
function onChange(control, oldValue, newValue, isLoading) {

if (isLoading || newValue == '') {
g_form.hideFieldMsg('project_deadline', true);
return;
}

//If the Project Deadline is in the past, show error message
var deadlineDate = new Date(getDateFromFormat(newValue, g_user_date_format));
var nowDate = new Date();

if (nowDate.getTime() >= deadlineDate.getTime()) {

g_form.setValue('project_deadline', '');
g_form.showErrorBox('project_deadline', 'Project deadline should be after today', true);

} else {

g_form.hideFieldMsg('project_deadline', true);

}

}
Loading