Skip to content
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
105 changes: 101 additions & 4 deletions Client-Side Components/UI Pages/Edit Last WorkNotes/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,103 @@
Edit Last Entered Work Notes/Additional comments
# ✏️ Edit Last Work Notes or Comments via Modal (Client UI Action)

This UI action is built specifically to edit the last entered work notes/ additional comments by the user in incident form or it can modified for any table which support this journal fields.
## 📁 Location
**Category:** `Client-Side Components`
**Subcategory:** `UI Actions`
**Snippet Folder:** `Edit Last Comments Modal`

There is some restriction around journal fields/ work notes as user cannot edit or adjust the work notes/additional comments that they entered. If they wish to edit it, I have introduced a new
UI action which calls the UI pages which will automatically populates the last entered work notes/comments and user can adjust and submit it.
---

## 📌 Description

This client-side function enables inline editing of the **most recent Work Notes or Additional Comments** on an Incident record via a modal dialog—without navigating away from the form.

When triggered from a **UI Action**, it opens a modal powered by the **UI Macro** `edit_worknotes_comments_inc`, passing the current record’s `sys_id` to enable dynamic data loading and editing.

---

## 🚀 Features

- 🪟 Opens a modal dialog using `GlideModal`
- ✏️ Allows quick editing of the latest Work Notes or Additional Comments
- 🔄 Passes the current Incident `sys_id` to the UI macro using `setPreference()`
- 🎨 Sets modal width and title for a better user experience
- 🧩 Seamlessly integrates into the form via a client-side UI Action

---

## 🧪 Example Use Case

Imagine a fulfiller needs to quickly revise their most recent comment on a ticket. Instead of scrolling through activity logs or editing the full record, they simply:

1. Click the **“Edit Last Comment”** button on the form.
2. A modal opens, pre-populated with the last comment.
3. They update and save — without navigating away from the current form.

---

## 📄 Script: `UIAction.js`

```javascript
/**
* Opens a modal to edit the last Work Notes or Additional Comments.
*
* Usage:
* - UI Action: Client = true
* - OnClick: openEditLastCommentModal()
* - Form Button: true
*/
function openEditLastCommentModal() {
var dialog = new GlideModal("edit_worknotes_comments_inc");
dialog.setTitle("Edit Last WorkNotes/Additional Comments");
dialog.setPreference("incid", g_form.getUniqueValue());
dialog.setWidth(550);
dialog.render();
}

🛠️ How to Use

1) Create a UI Action on the incident table with the following settings:
Client: true
Form Button: true
OnClick: openEditLastCommentModal()

2) Add the above function to a client script file or ensure it is loaded with the UI Action.
3) Ensure a corresponding UI Macro (edit_worknotes_comments_inc) exists that supports editing based on the provided sys_id.

📂 File Structure

Client-Side Components/
└── UI Actions/
└── Edit Last Comments Modal/
├── README.md
└── UIAction.js

📘 Dependencies

1) A working UI Macro named edit_worknotes_comments_inc that accepts the incid preference.
2) GlideModal API must be available on the form (standard in UI16+).
3) Script must run on a form context (g_form).

✅ Contribution Checklist Compliance

✔️ Code placed under correct category (Client-Side Components/UI Actions)
✔️ README.md provided with description and usage
✔️ Code snippet is self-contained and relevant to ServiceNow developers
✔️ No XML or server-specific exports included
✔️ Uses modern, maintainable JavaScript practices

👤 Author

Contributor: @Shweyy123
Script Name: UIAction.js — Open Modal for Editing Last Comments
PR Title: Added modal function to edit last Work Notes or Comments

📘 License

This script is shared for educational and developmental use. Test thoroughly in non-production environments before implementation.

🧩 Optional Enhancements

1) Extend to support both Work Notes and Additional Comments with tab-based UI
2) Add history tracking or audit log of modal-based edits
3) Support additional tables like change_request or problem
28 changes: 23 additions & 5 deletions Client-Side Components/UI Pages/Edit Last WorkNotes/UIaction.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,28 @@
//Enable client set to true
//Enter Onclick value as function name openEditLastCommentModal()
//Enter form button as true
/**
* Opens a modal to edit the last Work Notes or Additional Comments.
*
* @description
* This function is triggered by a form button (set to true),
* with client-side execution enabled. It opens a GlideModal
* using the UI macro "edit_worknotes_comments_inc" and sets
* the current incident sys_id as a preference for use within the modal.
*
* Usage:
* - Set Client = true
* - Set OnClick = openEditLastCommentModal()
* - Set Form Button = true
*/
function openEditLastCommentModal() {
// Create and configure the GlideModal
var dialog = new GlideModal("edit_worknotes_comments_inc");
dialog.setTitle('Edit Last WorkNotes/Additional Comments');
dialog.setPreference('incid', g_form.getUniqueValue());
dialog.setTitle("Edit Last WorkNotes/Additional Comments");

// Pass the current record's sys_id to the modal
dialog.setPreference("incid", g_form.getUniqueValue());

// Optional: Adjust modal width
dialog.setWidth(550);

// Render the modal
dialog.render();
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,31 @@
// Business Rule: 'After' Insert on 'sn_hr_core_case'
// Business Rule: After Insert on 'sn_hr_core_case'
(function executeRule(current, previous /*null when async*/) {

if (current.priority == "1" && current.subject_person.getValue('VIP') == 'true') {
var newTask = new GlideRecord('sn_hr_core_task');
newTask.initialize();
newTask.short_description = 'Priority VIP HR task for - ' + current.number;
newTask.assigned_to = current.assigned_to;
newTask.parent = current.sys_id;
newTask.insert();

gs.addInfoMessage('A related HR task has been created for this HR case.');
// Check if priority is high and subject person is a VIP
const isHighPriority = current.priority == '1';
const isVIP = current.subject_person.getValue('VIP') === 'true';

if (isHighPriority && isVIP) {
try {
const task = new GlideRecord('sn_hr_core_task');
task.initialize();

task.short_description = `Priority VIP HR task for - ${current.number}`;
task.assigned_to = current.assigned_to;
task.parent = current.sys_id;

const taskSysId = task.insert();

if (taskSysId) {
gs.addInfoMessage('✅ A related HR task has been successfully created for this VIP HR case.');
} else {
gs.addErrorMessage('⚠️ Failed to create HR task for VIP case.');
}

} catch (err) {
gs.error('Error while creating VIP HR Task: ' + err.message);
}
}

})(current, previous);

Loading