Execute ControlAddIn Javascript when from Subcribed Event

Hi,

I’m starting to look at developing custom extensions for my workplace.
I have a project where I need to play a sound when an error occurs in a 3rd party extension.

I’m able to create a controlAddIn and trigger the sound via Javascript by clicking on an navigation button using an action.

The problem I am facing is that I would like to trigger the sound after a certain event happens.

I subscribed to the correct event using the following code

codeunit 60102 "AudioFeedback"
{
    [EventSubscriber(ObjectType::Codeunit, Codeunit::"CodeUnitName", 'ActualEvent', '', true, true)]
    local procedure CallJavascript()
    begin
        Message('Running')
        CurrPage.AudioFeedback.playAudio('positive');
    end;
}

By moving my procedure into an Event, I no longer have access to the page variable called CurrPage.

I understand why this doesn’t work but I can’t seem to find a way around it.
Can I either pass the CurrPage object to my procedure or should my procedure dispatch a new event that is captured by my page and then executes the code.

My apologies if I’m not using the correct terms. I know many languages but AL is bran new to me.

Thank you in advance for any help.

Try to pass the ControlAdding to the Piblication/Suscription as var and you will manage to achieve it

It sounds like you’re on the right track with your understanding of events and procedures in AL. When you move your procedure into an event subscriber, you lose access to the CurrPage object because it is not available in the event context.

One solution would be to pass the CurrPage object as a parameter to your procedure. You can do this by adding a parameter to your procedure definition, like this:

local procedure CallJavascript(CurrPage: Page)

Then, when you subscribe to the event, you can pass the CurrPage object as a parameter, like this:

[EventSubscriber(ObjectType::Codeunit, Codeunit::“CodeUnitName”, ‘ActualEvent’, ‘’, true, true)]
local procedure CallJavascript(CurrPage: Page)
begin
Message(‘Running’);
CurrPage.AudioFeedback.playAudio(‘positive’);
end;

Alternatively, you could have your event subscriber dispatch a new event that is captured by your page and then executes the code. To do this, you would need to define a new event in your controlAddIn and subscribe to it in your page extension. When the event is fired in the event subscriber, it would raise the new event, which would then be captured by your page extension and execute the code to play the sound.

I hope this helps! Let us know if you have any further questions

Hi Jonathon,
Thank you very much for your feedback. I understand what you mean but I’m unable make it work.

This what I currently have

ControlAddIn

controladdin WmsAudioFeedback
{
    StartupScript = 'assets/js/script.js';
    HorizontalShrink = true;
    HorizontalStretch = true;
    MinimumHeight = 1;
    MinimumWidth = 1;
    RequestedHeight = 100;
    RequestedWidth = 100;
    VerticalShrink = true;
    VerticalStretch = true;

    procedure playAudio(_audio: Text);

    event ControlAddInReady();
}

And here’s my pageextention which adds it to the page and my codeunit to subscribe to the event from the 3rd party extension

pageextension 60101 "WmsAudioFeedbackPageExt" extends "3rd Party Extension"
{
    layout
    {
        addLast(content)
        {
            usercontrol(WmsAudioFeedback; WmsAudioFeedback)
            {
                ApplicationArea = All;
            }
        }
    }
    actions
    {
        addlast(processing)
        {

            action("Play Negative Audio")
            {
                ApplicationArea = All;
                Caption = 'Play Negative Audio';

                trigger OnAction()
                begin
                    CurrPage.WmsAudioFeedback.playAudio('positive');
                end;
            }
        }
    }
}


codeunit 60102 "WmsAudioFeedbackCodeUnit"
{
    [EventSubscriber(ObjectType::Codeunit, Codeunit::"3rd Party Event Publisher", 'OnAfterScan', '', true, true)]
    local procedure OnAfterScan(scan: Text; docType: Option; docNo: Code[50]; lp: Code[50]; item: Code[50]);
    begin
        //CurrPage.WmsAudioFeedback.playAudio('positive');
    end;
}

You suggested to add “CurrPage: Page” in my procedure but it gives me an error.

Trying to dispatch an event doesn’t seem to work since you can’t subscribe to an event in a pageextension, only a codeunit.

Would you be so kind as to point out what I am doing wrong here?
Thanks again!