skip to Main Content

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


  1. 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:

      $(document).on('customEvent', '#ID#', function() {
          console.log('event triggered');
          var student_id = this.browserEvent.student_id;
          console.log(student_id);
      });
      

    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 because this may refer to a different context when the event is triggered inside the modal.

    Solution:

    • Make sure that the student_id is available within the modal’s context. If this.browserEvent is not accessible or this is not the correct context, try using other means to access the student_id or ensure the correct context (this) is set in the modal.
    console.log('event triggered');
    var student_id = this?.browserEvent?.student_id || 'default_id';
    console.log(student_id);
    

    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:

    • Verify that the modal’s JavaScript execution environment is set up correctly. If possible, add logging to the modal’s lifecycle to see when the event is being triggered and whether the required variables are accessible.

    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:

      $('#modal').on('shown.bs.modal', function () {
          // Trigger the event only after the modal is fully shown
          $.event.trigger({ type: 'customEvent', id: '#ID#' });
      });
      

    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:

    • Ensure that the custom event is allowed to propagate by setting proper event listeners with .stopPropagation() and .preventDefault() only when necessary.

    Debugging Steps:

    1. Add logging to verify that the custom event is being triggered inside the modal.
    2. Check if this.browserEvent.student_id is defined and accessible within the modal’s execution context.
    3. Test using alternative ways to select or trigger the event inside the modal (e.g., ensure the DOM elements are available before triggering events).
    4. Make sure that the modal’s JavaScript context does not interfere with the global event handling.
    Login or Signup to reply.
  2. 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:

    javascript:apex.event.trigger(apex.gPageContext$, 'customEvent', {student_id: '#ID#'});
    
    When: custom
    Custom Event: customEvent
    Selection Type:  JavaScript Expression
     JavaScript Expression : apex.gPageContext$
    
    console.log('event triggered')
    var student_id = this.data.student_id
    console.log(student_id)
    

    See js doc apex.event.trigger

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search