cancel
Showing results for 
Search instead for 
Did you mean: 

Javascript variable to ABAP variable

Former Member
0 Kudos
807

I have a view that contains the following code:

<script defer language="Javascript" > 
function text_edit(defaultSelected,defaultValue,myForm){ 
if (selectedText == ""){ 
selectedText = defaultSelected; 
selectedValue = defaultValue; 
} 
customTextObject.cSelected = selectedText; 
customTextObject.cValue = selectedValue; 
textEditURL = "TextEdit.htm?option=" + selectedText + "&value=" + selectedValue; 
<b>customValue</b> = window.showModalDialog(textEditURL,customTextObject,customPageFeatures); 
var x = document.getElementById('customVal'); 
x.value = customValue; 
<script> 
..... 
<htmlb:inputField id = "customVal"   
visible = "FALSE"   
value = "<%=<b>lv_customvalue</b>%>"/> 
<htmlb:button text = "Edit"   
onClientClick = "text_edit('<%=lv_selection%>','<%=lv_selectionvalue%>',this.form)"/>

But is says that x is null, meaning document.getElementById('customVal') doesnt get any value. And so, I cannot put the value of the javascript variable to an ABAP variable.

How can I transfer the value of my javascript variable(<b>customValue</b>) to an ABAP variable(<b>lv_customvalue</b>)?

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

Is there another way to get the name of the form?

Also,

document.all.customVal

is null.

Former Member
0 Kudos

Hi Kathleen,

Sorry I gave you wrong syntex in my last post.

try this

<b>document.all("put element id here").value</b>

It should give you required value.

Regards,

Narinder Hartala

Former Member
0 Kudos

Narinder,

I am working with Kathleen on this issue.

document.all("customVal") still returns null.

Any other idea?

Joanna Shi

Former Member
0 Kudos

Hi,

If you are using sub-controllers the you have to concatenate controller's ID to your inputField's ID.

e.g. If your subcontroller's id is <b>mySubController</b>

and inputField's ID is 'customVal'

then you can get inputfield's value using code

document.all("mySubController_customVal").value;

Try it once also.

Regards,

Narinder Hartala

thomas_jung
Developer Advocate
Developer Advocate
0 Kudos

TIP: You don't have to hardcode the subcontroller ID. In the view coding you can always use controller->component_id to get the current, complete subcontroller ID. That way if your view is reused by one than one controller or a different levels your code doesn't break:

document.all("<%= controller->component_id %>_customVal").value;

Former Member
0 Kudos

Thanks Thomas! It's working. I have a new problem.

After setting the value for the hidden fields, I want to refresh the page and so call a BAPI to update the data in the table. Since this is a view, I will put the BAPI call to the DO_HANDLE_EVENT method. Is this correct? If so, my problem is how can I say that when the refresh is done, it will call the BAPI? I don't have something that would trigger the event.

athavanraja
Active Contributor
0 Kudos
<bsp:htmlbEvent id      = "myid"
                      onClick = "myonclick"
                      name    = "ValueChanged" />
        <script for="IP1" event=onchange type="text/javascript">

      ValueChanged();

      </SCRIPT>

in the above script for="IP1" is the name of the hidden form field.

any value change in that field will trigger serverside event.

Regards

Raja

Former Member
0 Kudos

Hi Raja!

This is my code:

            <htmlb:inputField id = "customVal"
                              type = "string"
                              visible = "false"
                              value="<%=lv_text%>"/>
            <bsp:htmlbEvent id      = "myid"
                          onClick = "update_custom_value"
                          name    = "ValueChanged" />
            <script for="customVal" 
                    event=onchange 
                    type="text/javascript">      
                    ValueChanged();      
            </SCRIPT>   

then a javascript call will place a value on the hidden field, like this:

    
document.all("<%= controller->component_id %>_customVal").value = customValue;

When the value is transferred to the hidden field. I want to call a BAPI. But nothing happens when the hidden field is filled-up with a value. Is there something wrong with my code? Note that this is a view and not a BSP page.

athavanraja
Active Contributor
0 Kudos

may be because its hidden, onchange event is not triggering.

when you set the value using javascript to the hidden field call the valuchanged() function as well.

setvalue();valuechanged();

this will work in view as well.

Regards

Raja

Former Member
0 Kudos

I tried calling the ValueChanged in my javascript code but it shows "page cannot be displayed". What I want is to refresh the page and call the BAPI on refresh. How can I do that?

I added this code in DO_HANDLE_EVENT:


  CASE htmlb_event->server_event.
   WHEN 'update_custom_value'.
         *do call bapi here...
  ENDCASE.

Is this correct?

athavanraja
Active Contributor
0 Kudos

just palce and http break point in your DO_HANDLE_EVENT method (in the starting point) and analyze the event object . as per your requirement the server event is generated with the help of the javascript now, within your method you have to handle.

Regards

Raja

Former Member
0 Kudos

I place a session breakpoint in the DO_HANDLE_EVENT but it doesnt stop there.

How can I do an http breakpoint?

athavanraja
Active Contributor
0 Kudos

to place a http breakpoint, first external debugging should be activated

se80->utilities->settings->abap editor->debugging

check the check box (actv) in external debuggin box

hit enter and come out. now in the do_handle_event method place the cursor where you want to the break point and click the red icon with letters STOP, this will throw a popup asking whether you want HTTP break point or session break point , click the button HTTP break point.

Regards

Raja

Former Member
0 Kudos

I already placed an HTTP breakpoint in the DO_HANDLE_EVENT but it doesnt stop there. It only stops in the DO_INIT and DO_REQUEST. After DO_REQUEST it goes to the page. What do you think is the problem?

Former Member
0 Kudos

Hi,

Just check out whether you have this in your method DO_REQUEST.

dispatch_input( ).

Regards,

Ravikiran.

Former Member
0 Kudos

Yup. dispatch_input( ) is present in DO_REQUEST. But still it doesnt go to DO_HANDLE_EVENT.

athavanraja
Active Contributor
0 Kudos

can you do a search in BSP forum with DO_HANDLE_EVENT. you will find lot of entries, where you could find a solution for your case.

Regards

Raja

Former Member
0 Kudos

Hi Raja!

Its working! I forgot to add an id for the form element that would trigger the event. After that, it worked! It enters the DO_HANDLE_EVENT.

Thanks a lot!

Kathleen

athavanraja
Active Contributor
0 Kudos

Glad to hear that.

Regards

Raja

Answers (5)

Answers (5)

Former Member
0 Kudos

But the problem is, it doesn't return the name of the form(this.form). That is why my variable myForm is null.

My view is a fragment only of a whole page. The <htmlb: form> tag is not included in the view. That is why I cannot access the name of the form. And so, I got a null value on line :

document.myForm.customVal

.

Former Member
0 Kudos

i am not sure but i think in case of view which is called inside a page the form is the form in parent page.

try giving the name of the form which you have defined in parent page inside whom this view lies.

rgrds,

Former Member
0 Kudos

Hi Kathleen,

Try to access value using <b>document.all.customVal</b> or if you are using sub controllers then use

<b>document.all.<sub-controller ID>_customVal</b> .

Regards,

Narinder Hartala

Former Member
0 Kudos

I have a view that contains the following code:

<script defer language="Javascript" > 
function text_edit(defaultSelected,defaultValue,myForm){ 
if (selectedText == ""){ 
selectedText = defaultSelected; 
selectedValue = defaultValue; 
} 
customTextObject.cSelected = selectedText; 
customTextObject.cValue = selectedValue; 
textEditURL = "TextEdit.htm?option=" + selectedText + "&value=" + selectedValue; 
customValue = window.showModalDialog(textEditURL,customTextObject,customPageFeatures); 
var x = document.getElementById('customVal'); 
x.value = customValue; 
<script> 
..... 
<htmlb:inputField id = "customVal"   
visible = "FALSE"   
value = "<%=lv_customvalue%>"/> 
<htmlb:button text = "Edit"   
onClientClick = "text_edit('<%=lv_selection%>','<%=lv_selectionvalue%>',this.form)"/>

As you can see, the button <b>Edit</b> calls the javascript function <b>text_edit</b>. This function opens a BSP page

named <b>TextEdit.htm</b>. Here are the contents of TextEdit.htm:

<script language="javascript">
function getValue(Cvalue){
    window.returnValue = Cvalue
    window.close();
}
</script>
...
<htmlb:form id="customTextForm">
<htmlb:textEdit id= "customTextArea" rows = "20" cols = "50" text=" "/>
<htmlb:button id ="create_text" text ="Create Text" width = "50" onClientClick="getValue(customTextForm.customTextArea.value)"/>
</htmlb:form>

My problem is how can I put the text entered by user to an ABAP variable.

My options are:

a) Process the data(through BAPI call) before the pop-up window is closed.

or

b)Since the data is passed back to my view through the javascript variable

customValue(the one returened by the modal dialog),

I can do the processing(through BAPI call) in the view, after the pop-up modal window is closed.

Former Member
0 Kudos

Define a hidden input field on your view.

set its value to to the value that is returned by page(model page).

like

-


<script defer language="Javascript" >

function text_edit(defaultSelected,defaultValue,myForm){

if (selectedText == ""){

selectedText = defaultSelected;

selectedValue = defaultValue;

}

customTextObject.cSelected = selectedText;

customTextObject.cValue = selectedValue;

textEditURL = "TextEdit.htm?option=" + selectedText + "&value=" + selectedValue;

customValue = window.showModalDialog(textEditURL,customTextObject,customPageFeatures);

document.myForm.if01.value = customValue;

//this will forcibly submit the form

document.myForm.submit();

<script>

-


hidden field on page layout---

<htmlb:inputField id = "if01"

type = "string"

visible = "false"

size = "20" />

-


now at server acces the value of inputfield which contains the text entered by user.

if you do not submit the form then you can access and set your abap variable whenever there is a round trip to server.

rgrds,

Former Member
0 Kudos

Hi Kathleen,

Visit thread <a href="https://www.sdn.sap.com/irj/sdn/thread?forumID=49&threadID=48959&messageID=499935">Assign a Javascript variable value to a ABAP variable</a>.

This may help you.

Regards,

Narinder Hartala

former_member183993
Participant
0 Kudos

hi Hemendra,

I wanted to ask you, on your post you state: "now at server acces the value of inputfield which contains the text entered by user.

if you do not submit the form then you can access and set your abap variable whenever there is a round trip to server." can you please indicate how this is achieved? Pardon me, I am new to ABAB programming.

Thanks,

Chris

Former Member
0 Kudos

But the problem is my BSP page is a modal pop-up window, when an event is triggered, a new explorer window will be opened. How will I handle this?

Former Member
0 Kudos

can you tell me exact requirement or give more light to your problem.

rgrds

Former Member
0 Kudos

Thanks!

Actually, this view calls a BSP page, I used a javascript to open it in pop-up window (window.showModalDialog). My first option is to put the processing in the view but then based from your reply, I was thinking of processing it in the BSP page(and not in the view which is the calling page). So after submitting the BSP page, I want to call a BAPI, how can I do that? Will I put my call to BAPI in the OnDestroy method?

Former Member
0 Kudos

if your bapi is not returning anything then you can call it on ondestroy.

else call it on oninputprocessing. and get the returned variables.

rgrds

Former Member
0 Kudos

its not possible to assign js variable value to abap var.

however viceversa is possible.

you have to go for server round trip.

do this.

create an hidden input field assign js var value to it go to server round trip and retreive value.

check this sample code for this:

<SCRIPT language="JavaScript">

function func(){var txt;txt = "Hello";

document.formBody.if01.value = txt;

document.formBody.submit();

}

</SCRIPT>

<htmlb:form id="formBody" >

<htmlb:inputField id = "if01"

type = "string"

visible = "false"

size = "20" />

here we r assigning value of js var to inputfield and at server event handlling

get the value and assign it to abap var.

regards,