Skip to Content

SAP UI5 - How to call controller function from outside of UI5 framework

I am new in UI development and looking for a help on below issue. I have created a SAP UI5 project which has one button defined in view file and function defined in controller file which gets called on press event of button.

I have a requirement to click button B1 from outside of UI5 framework and also invoke controller function defined on press event of button.

Can someone please suggest how this can be done.

Thanks

Add comment
10|10000 characters needed characters exceeded

  • Get RSS Feed

3 Answers

  • Best Answer
    Feb 07 at 11:53 AM

    Hi Swati Garg,

    You can directly access the button and call firePress() method right?

    But you cannot directly access the button outside with the id you have given inside the view.. UI5 framework will generate a different ID based on the component name, view name and the button id. using that id you need to get the button instance.

    If the button id is "ButtonCustID" then the UI5 generated will be "__ComponentName---ViewName--ButtonCustID".

    sap.ui.getCore().byId("").firePress("__ComponentName---ViewName--ButtonCustID");

    BR,

    Mahesh

    Add comment
    10|10000 characters needed characters exceeded

  • Feb 07 at 02:44 PM

    what's your scenario?

    Add comment
    10|10000 characters needed characters exceeded

    • I need to capture user actions performed on sap ui5 web page and playback same after sometime. So if user entered a text and clicked on button then I want to playback same from outside. I am able to capture and playback input field but click of button doesn't call function defined on press event in controller file during playback. So i need to know how to invoke controller function from outside. Below code clicks the button.

      var targBtn = document.querySelector ("#" + target.id); //target.id is id of control element.

      var clickEvent = document.createEvent ('MouseEvents');

      clickEvent.initEvent ('click', true, true);

      targBtn.dispatchEvent (clickEvent);

      My code to capture and playback is written in .Js file and I have injected this file in index file of sapUI5 app.

  • Feb 07 at 04:07 PM
    document.getElementById('yourbuttonid').click()
    Add comment
    10|10000 characters needed characters exceeded