Home > Forum > Rules, JS, SQL > Open first attachment from all atachment tab

Open first attachment from all atachment tab
0

MVP

Hi Marcin,

you can use the below code inside a form rule which is set to JavaScript mode. The form rule in turn is executed OnPage load / behavior tab of the form.

What the script does:
1. Show all attachments tab, which triggers loading the related attachment.
2. Once the loading is triggered showFirstAttachment is executed 10 times. This will test whether an attachment is available and "click" on it.
I shamelessly copied the simulateClick function from:
https://stackoverflow.com/questions/40091000/simulate-click-event-on-react-element

Best regards,
Daniel


window.ccls = window.ccls || {};
ccls.showAllAttachments = {};
ccls.showAllAttachments.Timeout = 0;
ccls.showAllAttachments.TimeoutMax = 4;
ccls.showFirstAttachment= {};
ccls.showFirstAttachment.Timeout = 0;
ccls.showFirstAttachment.TimeoutMax = 10;

ccls.showAllAttachments.execute = function (){
// Start debugger, if debug parameter is set and dev tools are started.
if (new URLSearchParams(document.location.search).get("debug") == 1) {
debugger;
}

var items = document.getElementsByClassName("all-attachments-link");
// verify that attachments are avialable
if (items == null || items.length != 1 ){
if (ccls.showAllAttachments.Timeout<= ccls.showAllAttachments.TimeoutMax){
ccls.showAllAttachments.Timeout ++;
setTimeout(function (){ccls.showAllAttachments.execute();},333)
}
return;
}
items[0].click();
ccls.showFirstAttachment.execute();
}


ccls.showFirstAttachment.execute = function (){
// Start debugger, if debug parameter is set and dev tools are started.
if (new URLSearchParams(document.location.search).get("debug") == 1) {
debugger;
}

var items = $(".attachment-name",$("#all-attachments"))
if (items == null || items.length != 1 ){
if (ccls.showFirstAttachment.Timeout<= ccls.showFirstAttachment.TimeoutMax){
ccls.showFirstAttachment.Timeout ++;
setTimeout(function (){ccls.showFirstAttachment.execute();},333)
}
return;
}
simulateMouseClick(items [0]);
}

const mouseClickEvents = ['mousedown', 'click', 'mouseup'];
function simulateMouseClick(element){
debugger;
mouseClickEvents.forEach(mouseEventType =>
element.dispatchEvent(
new MouseEvent(mouseEventType, {
view: window,
bubbles: true,
cancelable: true,
buttons: 1
})
)
);
}

ccls.showAllAttachments.execute();

In reply to: Daniel Krüger (Cosmo Consult)

Hi Marcin,

you can use the below code inside a form rule which is set to JavaScript mode. The form rule in turn is executed OnPage load / behavior tab of the form.

What the script does:
1. Show all attachments tab, which triggers loading the related attachment.
2. Once the loading is triggered showFirstAttachment is executed 10 times. This will test whether an attachment is available and "click" on it.
I shamelessly copied the simulateClick function from:
https://stackoverflow.com/questions/40091000/simulate-click-event-on-react-element

Best regards,
Daniel


window.ccls = window.ccls || {};
ccls.showAllAttachments = {};
ccls.showAllAttachments.Timeout = 0;
ccls.showAllAttachments.TimeoutMax = 4;
ccls.showFirstAttachment= {};
ccls.showFirstAttachment.Timeout = 0;
ccls.showFirstAttachment.TimeoutMax = 10;

ccls.showAllAttachments.execute = function (){
// Start debugger, if debug parameter is set and dev tools are started.
if (new URLSearchParams(document.location.search).get("debug") == 1) {
debugger;
}

var items = document.getElementsByClassName("all-attachments-link");
// verify that attachments are avialable
if (items == null || items.length != 1 ){
if (ccls.showAllAttachments.Timeout<= ccls.showAllAttachments.TimeoutMax){
ccls.showAllAttachments.Timeout ++;
setTimeout(function (){ccls.showAllAttachments.execute();},333)
}
return;
}
items[0].click();
ccls.showFirstAttachment.execute();
}


ccls.showFirstAttachment.execute = function (){
// Start debugger, if debug parameter is set and dev tools are started.
if (new URLSearchParams(document.location.search).get("debug") == 1) {
debugger;
}

var items = $(".attachment-name",$("#all-attachments"))
if (items == null || items.length != 1 ){
if (ccls.showFirstAttachment.Timeout<= ccls.showFirstAttachment.TimeoutMax){
ccls.showFirstAttachment.Timeout ++;
setTimeout(function (){ccls.showFirstAttachment.execute();},333)
}
return;
}
simulateMouseClick(items [0]);
}

const mouseClickEvents = ['mousedown', 'click', 'mouseup'];
function simulateMouseClick(element){
debugger;
mouseClickEvents.forEach(mouseEventType =>
element.dispatchEvent(
new MouseEvent(mouseEventType, {
view: window,
bubbles: true,
cancelable: true,
buttons: 1
})
)
);
}

ccls.showAllAttachments.execute();

Thank you Daniel.

Privacy overview

This website uses cookies so that we can provide you with the best user experience possible. Cookie information is stored in your browser and performs functions such as recognizing you when you return to our website and helping our team to understand which sections of the website you find most interesting and useful.


To see a full list of the cookies we use and learn more about their purposes, visit our Privacy Policy.