cancel
Showing results for 
Search instead for 
Did you mean: 

calling a abap method in a javascript

Former Member
0 Kudos

Hi all,

I've been browsing a bit through this forum, but there has no answer been completely covering my problem...

I would like to call an abap method in the javascript in order to check some values...

This is what I have done:

<script language="JavaScript" defer="defer">

function do_checks(htmlbevent)

{

try

{

var doubles = "<%= controller->check_doubles( )%>";

if(doubles == true){

var Check = confirm("Test??");

if(Check == false){

htmlbevent.cancelSubmit = true;

}

}

}

catch(e)

{

}

}

</script>

The method has the following code:

mehod check_doubles. " returning parameter = rv_double of type flag

rv_double = abap_true.

endmethod.

the button is defined like this...

<htmlb:button

id = "Save"

onClick = "save"

onClientClick = "do_checks(htmlbevent);"

text = "<%= otr(crm_ic_appl/Save) %>"

/>

The funny thing is, that the controller->check_doubles( ). is already processed at load of the page (although there is no on_load used somewhere...

If I leave out the " if(doubles == true){ } ", the javascript reacts as foreseen..., but when I keep the " if(doubles == true){ } ", the javascript doesn't go through the method anymore...

Can anybody help me out here???

Thanks!!

Micha

Accepted Solutions (0)

Answers (3)

Answers (3)

guillaume-hrc
Active Contributor
0 Kudos

Hi Micha,

You must understand that the only way to run some ABAP at runtime is to call the Web Application server.

But, the Javascript works on the client side. In fact, your function <i>do_checks</i> is created when the server gives out the page to the user : that is why you see it running.

If you want to have ABAP processed at runtime, you have to make a new request to the ABAP server.

-> this can be done with some AJAX, check the weblogs in SDN about this

/people/sap.user72/blog/2005/08/15/ajax-and-htmlb--a-sample-bsp-application

and others...

To sum it up, if you want to have runtime check then

1) Do it all in Javascript

2) Use AJAX to make a request to the WAS and processed the XML returned

Hope this is clearer!

Best regards,

Guillaume

raja_thangamani
Active Contributor
0 Kudos

<i>If I leave out the " if(doubles == true){ } ", the javascript reacts as foreseen..., but when I keep the " if(doubles == true){ } ", the javascript doesn't go through the method anymore...</i>

YOu need to check the return value in "doubles".

Just add

alert(doubles);

and check-out what is the value of doubles and then you can use that value in IF condition. Because the values will be treated different in JavaScript & in ABAP.

Raja T

Former Member
0 Kudos

Nevermind,

I already found that the onclientclick could never check data in the system, because it is just working on the browser and not going to the server...

sorry answers where not satisfying...

guillaume-hrc
Active Contributor
0 Kudos

You're right... unless you make another call using Javascript to the Web Application server. This will keep the current page on, while giving you the opportunity to process some data on the server side, and get a result that you will exploit using Javascript once again.

<b>I am not saying it is an easy task, though !</b>

Best regards,

Guillaume

raja_thangamani
Active Contributor
0 Kudos

Hi,

You can call them FM inside the javascript something like below...

<script language="JavaScript">
function yourFM()
{
<%
   DATA: guid TYPE GUID_32.
   CALL FUNCTION 'GUID_CREATE' IMPORTING ev_guid_32 = guid.
%>
}

</script>

<b>To call the class & method:</b>

<script language="JavaScript">
    function yourFM()
    {
      <% CLASS cl_bsp_login_application DEFINITION LOAD. %>
      var EXIT_URL  = "<%= CL_BSP_LOGIN_APPLICATION=>GET_SESSIONEXIT_URL( page = page ) %>";
     
         }
  </script>

But i am not clear what do you want to achieve. I didnt understand what you are trying to do with you code. Could you give more detail?

<i>* Reward each useful answer</i>

Raja T

Message was edited by:

Raja Thangamani