Skip to content

Hide a related list #1082

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 8 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)
26 changes: 26 additions & 0 deletions Server Side/HideRelatedList/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@

# How to hide a Related List

Do you know about the Related List that displays people who can approve or reject a document?

Recently a scenario came up where the developer needed to prevent the employee who created a record from approving that document.
It wasn't common because usually the operational level creates the document while a manager approves it.
Leaving this question of personas aside and focusing on the problem, what would be a possible solution?

The first workaround that came to my mind was to hide the Approval list if the user who registered was viewing his own record.
A workaround is not the ultimate solution. Just a palliative fix while we find time to think about the ideal one.

The action plan

1) Every record has a field called sys_created_by;
2) On the front-end (client side) we have access to an API called Glide User that has some information about the logged in user. Among them is the name in the userName property.

What if we create a Client Script of type onLoad* that compares these two fields and, if they are the same, simply hides the Approvers related list?
To see the related list name created for your table, you can access the platform and go to System UI > Related lists.


Now that you know the related list name, your script would look like script.js in this folder.

To get the value of the sys_created_by field it must be in the form. You can leave it read-only or even hidden but it must exist on the form.

A Client Script of type onLoad is executed whenever the form is opened.
9 changes: 9 additions & 0 deletions Server Side/HideRelatedList/script.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
function onLoad() {

var createdBy = g_form.getValue('sys_created_by');
var loggedUser = g_user.userName;
if (createdBy == loggedUser) {
g_form.hideRelatedList('put your related list name here');
}

}
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