How To Trigger SAP Server Event With Javascript

In order to trigger a server even with Javascript from a BSP, you need to generate a Javascript function with <bsp:event> tag in your .htm file, which you can then use inside a script. You can pass two parameters with it as well if you desire.

<bsp:htmlbEvent name = "fireServerEvent"
                Id   = "fireServerEvent"
                p1   = "p1"
                p2   = "p2" />

You can then use the generated function as below

<script language="javascript">
    fireServerEvent( p1 = "first_parameter"
                     p2 = "second_parameter" );
</script>

You need to create an event handler with the name serverEvent (case sensitive!) to process this event. You can then access the data inside the event handler as below

DATA: lr_event       TYPE REF TO cl_bsl_htmlb_event,
      lt_event_items TYPE tihyypnvp,
      ls_event_items TYPE ihttpnvp.

IF htmlb_event_ex IS BOUND.
       lr_event ?= htmlb_event_ex.
       IF  lr_event->name = 'fireServerEvent'.
*      your code here
*      access the parameters as lr_event->p1 and lr_event->p2
    ENDIF
ENDIF

You can use a code like below in your .htm file to trigger a server event periodically (in every 10 seconds for this example) using the setInterval() method of Javascript

<bsp:htmlbEvent name = "fireServerEvent"
                id   = "fireServerEvent" />

<script language="javascript">
    var oInterval = setInterval("fireServerEvent()", 10000);
</script>

 

ABOUT Furkan Sönmez

www.linkedin.com/profile/view?id=122521475

RELATED POSTS

COMMENTS

comments powered by Disqus