From aaa726a7065a64e281ad1d23e2554501577f5536 Mon Sep 17 00:00:00 2001 From: kmxo Date: Wed, 25 Sep 2024 22:17:39 -0300 Subject: [PATCH 1/6] URL Parameter Use Case --- .../CatalogClientScript.js | 36 ++++++++++++++++++ .../KMXOUtils.js | 37 +++++++++++++++++++ .../UtilsAjax.js | 11 ++++++ .../Set fields from URL Parameter 2/readme.md | 27 ++++++++++++++ 4 files changed, 111 insertions(+) create mode 100644 Catalog Client Script/Set fields from URL Parameter 2/CatalogClientScript.js create mode 100644 Catalog Client Script/Set fields from URL Parameter 2/KMXOUtils.js create mode 100644 Catalog Client Script/Set fields from URL Parameter 2/UtilsAjax.js create mode 100644 Catalog Client Script/Set fields from URL Parameter 2/readme.md diff --git a/Catalog Client Script/Set fields from URL Parameter 2/CatalogClientScript.js b/Catalog Client Script/Set fields from URL Parameter 2/CatalogClientScript.js new file mode 100644 index 0000000000..d21d2756ca --- /dev/null +++ b/Catalog Client Script/Set fields from URL Parameter 2/CatalogClientScript.js @@ -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; + } + } \ No newline at end of file diff --git a/Catalog Client Script/Set fields from URL Parameter 2/KMXOUtils.js b/Catalog Client Script/Set fields from URL Parameter 2/KMXOUtils.js new file mode 100644 index 0000000000..7ecd6cec56 --- /dev/null +++ b/Catalog Client Script/Set fields from URL Parameter 2/KMXOUtils.js @@ -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' +}; \ No newline at end of file diff --git a/Catalog Client Script/Set fields from URL Parameter 2/UtilsAjax.js b/Catalog Client Script/Set fields from URL Parameter 2/UtilsAjax.js new file mode 100644 index 0000000000..abd7e8f408 --- /dev/null +++ b/Catalog Client Script/Set fields from URL Parameter 2/UtilsAjax.js @@ -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' +}); \ No newline at end of file diff --git a/Catalog Client Script/Set fields from URL Parameter 2/readme.md b/Catalog Client Script/Set fields from URL Parameter 2/readme.md new file mode 100644 index 0000000000..500af298e5 --- /dev/null +++ b/Catalog Client Script/Set fields from URL Parameter 2/readme.md @@ -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) From 23fabd47065c9ec1eee2e435c75293150f0a927a Mon Sep 17 00:00:00 2001 From: kmxo Date: Mon, 30 Sep 2024 21:59:43 -0300 Subject: [PATCH 2/6] update background scripts folder --- .../update a record in the sys_user table/readme.md | 9 +++++++++ .../update a record in the sys_user table/script.js | 11 +++++++++++ 2 files changed, 20 insertions(+) create mode 100644 Background Scripts/update a record in the sys_user table/readme.md create mode 100644 Background Scripts/update a record in the sys_user table/script.js diff --git a/Background Scripts/update a record in the sys_user table/readme.md b/Background Scripts/update a record in the sys_user table/readme.md new file mode 100644 index 0000000000..f2a9daa1af --- /dev/null +++ b/Background Scripts/update a record in the sys_user table/readme.md @@ -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. \ No newline at end of file diff --git a/Background Scripts/update a record in the sys_user table/script.js b/Background Scripts/update a record in the sys_user table/script.js new file mode 100644 index 0000000000..b112122f7b --- /dev/null +++ b/Background Scripts/update a record in the sys_user table/script.js @@ -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.'); +} \ No newline at end of file From ddcba51fa1685a7212107922af7bb79ed065c6c1 Mon Sep 17 00:00:00 2001 From: kmxo Date: Mon, 30 Sep 2024 22:17:14 -0300 Subject: [PATCH 3/6] fixing --- .../update a record in the sys_user table/readme.md | 9 --------- .../update a record in the sys_user table/script.js | 11 ----------- 2 files changed, 20 deletions(-) delete mode 100644 Background Scripts/update a record in the sys_user table/readme.md delete mode 100644 Background Scripts/update a record in the sys_user table/script.js diff --git a/Background Scripts/update a record in the sys_user table/readme.md b/Background Scripts/update a record in the sys_user table/readme.md deleted file mode 100644 index f2a9daa1af..0000000000 --- a/Background Scripts/update a record in the sys_user table/readme.md +++ /dev/null @@ -1,9 +0,0 @@ -# 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. \ No newline at end of file diff --git a/Background Scripts/update a record in the sys_user table/script.js b/Background Scripts/update a record in the sys_user table/script.js deleted file mode 100644 index b112122f7b..0000000000 --- a/Background Scripts/update a record in the sys_user table/script.js +++ /dev/null @@ -1,11 +0,0 @@ -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.'); -} \ No newline at end of file From 6000a9120d0441203a5ec0e00d515d15905c509b Mon Sep 17 00:00:00 2001 From: kmxo Date: Mon, 30 Sep 2024 22:23:05 -0300 Subject: [PATCH 4/6] background scripts --- .../Updating a record in the sys_user table/readme.md | 9 +++++++++ .../Updating a record in the sys_user table/script.js | 11 +++++++++++ 2 files changed, 20 insertions(+) create mode 100644 Background Scripts/Updating a record in the sys_user table/readme.md create mode 100644 Background Scripts/Updating a record in the sys_user table/script.js diff --git a/Background Scripts/Updating a record in the sys_user table/readme.md b/Background Scripts/Updating a record in the sys_user table/readme.md new file mode 100644 index 0000000000..f2a9daa1af --- /dev/null +++ b/Background Scripts/Updating a record in the sys_user table/readme.md @@ -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. \ No newline at end of file diff --git a/Background Scripts/Updating a record in the sys_user table/script.js b/Background Scripts/Updating a record in the sys_user table/script.js new file mode 100644 index 0000000000..e483846890 --- /dev/null +++ b/Background Scripts/Updating a record in the sys_user table/script.js @@ -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.'); +} From 3bf5729d23947568ba4d09f8748957718c9b49d2 Mon Sep 17 00:00:00 2001 From: kmxo Date: Tue, 1 Oct 2024 12:43:47 -0300 Subject: [PATCH 5/6] validate data field --- Service Portal/validate-data-field/readme.md | 40 ++++++++++++++++++++ Service Portal/validate-data-field/script.js | 23 +++++++++++ 2 files changed, 63 insertions(+) create mode 100644 Service Portal/validate-data-field/readme.md create mode 100644 Service Portal/validate-data-field/script.js diff --git a/Service Portal/validate-data-field/readme.md b/Service Portal/validate-data-field/readme.md new file mode 100644 index 0000000000..cd996a314e --- /dev/null +++ b/Service Portal/validate-data-field/readme.md @@ -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 + diff --git a/Service Portal/validate-data-field/script.js b/Service Portal/validate-data-field/script.js new file mode 100644 index 0000000000..be67b1e297 --- /dev/null +++ b/Service Portal/validate-data-field/script.js @@ -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); + + } + +} \ No newline at end of file From 30e2a0be02dc0157910c6728a3f99396e14f5c51 Mon Sep 17 00:00:00 2001 From: kmxo Date: Tue, 1 Oct 2024 12:51:48 -0300 Subject: [PATCH 6/6] related list --- Server Side/HideRelatedList/readme.md | 26 ++++++++++++++++++++++++++ Server Side/HideRelatedList/script.js | 9 +++++++++ 2 files changed, 35 insertions(+) create mode 100644 Server Side/HideRelatedList/readme.md create mode 100644 Server Side/HideRelatedList/script.js diff --git a/Server Side/HideRelatedList/readme.md b/Server Side/HideRelatedList/readme.md new file mode 100644 index 0000000000..587473230d --- /dev/null +++ b/Server Side/HideRelatedList/readme.md @@ -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. \ No newline at end of file diff --git a/Server Side/HideRelatedList/script.js b/Server Side/HideRelatedList/script.js new file mode 100644 index 0000000000..968a2203ed --- /dev/null +++ b/Server Side/HideRelatedList/script.js @@ -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'); + } + +} \ No newline at end of file