Related Attachment list in Workspace Navigation

TramaineM
Kilo Sage

Hi,

I've created a relationship / related list for attachments in the HR Agent Workspace. This way the HR Tasks can show a related attachments from the parent record without having to navigate to the parent. When clicking on the file name, it wasn't working until I found on another post an onload client script to openURL of the attachment and it downloads. 

 

However, I'm trying to prevent workspace from navigating to the attachment record when the file name is clicked. This record is useless for the agents and would create confusion for them. So the idea is to click on the filename and the file downloads in the browser while the page stays on the related list of attachments. 

 

Related list of Attachments on Parent

Screenshot 2024-10-15 104710.png

 

Attachment record that it navigates to 

Screenshot 2024-10-15 104736.png

The client script used to download the attachments when link is clicked

function onLoad() {
    openUrl("sys_attachment.do?sys_id=" + g_form.getUniqueValue());
    g_form.addInfoMessage('Attachment downloaded');

}

function openUrl(url) {

    this.open(url);
}

 

1 ACCEPTED SOLUTION

TramaineM
Kilo Sage

In case anyone needs it in the future, I solved this by adding 

g_aw.closeRecord(); after the info message in the client script. 

View solution in original post

3 REPLIES 3

sadif_raja
Tera Guru

Hello,

To prevent the **HR Agent Workspace** from navigating to the attachment record and instead directly download the attachment while staying on the related list, you can leverage a combination of client-side logic and the `openURL` functionality.

Here’s an approach you can try:

### Solution using `onClick` Event:
1. **Create an `onClick` event handler** in the related list or an **onLoad client script** to override the default behavior when clicking the attachment link. You can use `event.preventDefault()` to stop the navigation to the record and instead trigger the download.

2. **Modify the URL to download the file directly** using `window.open` so the file is opened in a new tab or downloaded.

Here’s a sample client script that you can use in your **onLoad Client Script**:

```javascript
function onLoad() {
// Attach click event listener to the attachment related list links
var attachmentLinks = document.querySelectorAll('a[data-type="attachment"]'); // Replace with actual selector if needed

attachmentLinks.forEach(function(link) {
link.addEventListener('click', function(event) {
event.preventDefault(); // Prevents navigation to the attachment record

var attachmentSysId = link.getAttribute('data-sys-id'); // Fetch the attachment sys_id from the link
var attachmentURL = '/sys_attachment.do?sys_id=' + attachmentSysId; // Build the direct download URL

// Open the attachment URL in a new tab or force download
window.open(attachmentURL, '_blank'); // Opens the file for download or viewing
});
});
}

 Explanation:
1. **Prevent Default Navigation**: The `event.preventDefault()` prevents the workspace from navigating to the attachment record when the user clicks on the attachment link.
2. **Download or Open in New Tab**: The script constructs the URL for the attachment download and uses `window.open` to open it in a new tab. This ensures the page stays on the related list.

### Additional Considerations:
- **Selector Adjustment**: You may need to adjust the selector (`a[data-type="attachment"]`) depending on how the attachment links are rendered in your specific instance. Inspect the HTML to get the right attributes.
- **Download Behavior**: The file will either open in a new tab (if the file type supports viewing in a browser, like PDFs) or it will trigger the browser’s download behavior.

This approach should keep the user in the **HR Agent Workspace** while enabling the attachment download seamlessly.

If this solution helps, I’d appreciate it if you could mark it as helpful!

Best regards,
Raja

TramaineM
Kilo Sage

In case anyone needs it in the future, I solved this by adding 

g_aw.closeRecord(); after the info message in the client script. 

Can you post the final implementation?