one of classic Report columns setted to be link and its javascript link which is javascript:$.event.trigger({type:'customEvent',id:'#ID#'});
Dynamic Action created with the following attributes:
When: custom
Custom Event: customEvent
Selection Type: JavaScript Expression
JavaScript Expression : document
the action is Execute javascript code:
console.log('event triggered')
var student_id = this.browserEvent.student_id
console.log(student_id)
this scenario working perfectly in Normal Page, but not in Modal Dialog Page. Why? Any suggestions?
2
Answers
The issue you’re experiencing where the custom event works in a normal page but not in a modal dialog page is likely related to the context in which the event is being triggered and how the modal dialog interacts with the DOM. Here are a few potential reasons and suggestions to troubleshoot:
1. Event Binding in Modal
When using a modal dialog, elements might not be in the DOM immediately upon page load, and certain event bindings may fail because the elements don’t exist when the script is initially executed.
Solution:
Ensure that event listeners are being bound correctly within the modal’s lifecycle. You may need to bind the event listeners after the modal has been fully rendered or ensure they are dynamically attached.
If you are using jQuery, try using event delegation by binding the event to a parent element that exists when the page loads and filtering for the target element:
2. Modal Dialog Scope
The modal dialog may create a new execution context, which could cause the
this.browserEvent
to behave differently within the modal. This happens becausethis
may refer to a different context when the event is triggered inside the modal.Solution:
student_id
is available within the modal’s context. Ifthis.browserEvent
is not accessible orthis
is not the correct context, try using other means to access thestudent_id
or ensure the correct context (this
) is set in the modal.3. JavaScript Expression Context in Modal
JavaScript expressions inside the modal might not execute in the same context as they do in a regular page due to different scope handling or JavaScript execution timing.
Solution:
4. JavaScript Execution Timing
The modal dialog might be dynamically injected into the DOM, meaning that the JavaScript code could run before the modal is fully available or rendered, causing issues when trying to access DOM elements or trigger events.
Solution:
Ensure that the modal’s elements are fully loaded before attempting to trigger the event. You could do this by adding a check or a delay before triggering the event, or by hooking into the modal’s open event to ensure it’s ready:
5. Custom Event Propagation in Modal
If you’re triggering custom events inside the modal, it’s important to check if the modal prevents event propagation, which could be stopping the event from being handled as expected.
Solution:
.stopPropagation()
and.preventDefault()
only when necessary.Debugging Steps:
this.browserEvent.student_id
is defined and accessible within the modal’s execution context.I tried your code in Chrome and for me it was working, also in Modal Page. So not sure what the issue is. However, what you can do is use the APEX abstraction:
See js doc apex.event.trigger