+ Start a Discussion
Rakhesh TRakhesh T 

Not able to call "Remote Action method result" to Java Script function result.

Helloo....
I have created a VF page for a Controller with remote action method. I am not able to get the call back to javascript function and display the result in output text field of  Page block section.Can anyone please help me resolve the issue. Please find VF code and Apex code below:

Apex Code:

public class RemoteFunction1CNTRL{
@RemoteAction
public static Account call(string accname){
 Account acc = [select id,name,phone from Account where name=:accname];
 return acc;
}
}

VF code:

<apex:page controller="RemoteFunction1CNTRL">
<apex:form id="fm">
<script>
function myfun(){
alert('Script function called');
var name = document.getElementById("{!$Component.pb1.it}").value;
alert('Searching for:'+''+name);

RemoteFunction1CNTRL.call(name,handleResponse,{escape:true});

function handleResponse(result,event){
alert('sobject:'+ result);
document.getElementById("{!$Component.pb2.sid}").innerHTML=result.Id;
document.getElementById("{!$Component.pb2.sname}").innerHTML=result.Name;
document.getElementById("{!$Component.pb2.sphone}").innerHTML=result.Phone;
}
}
</script>
<apex:pageBlock id="pb1">
<apex:inputText id="it"/>
<apex:commandButton value="Click" onclick="myfun();"/>
</apex:pageBlock>

<apex:pageBlock id="pb2">
<apex:outputText id="sid"/><br/>
<apex:outputText id="sname"/><br/>
<apex:outputText id="sphone"/><br/>
</apex:pageBlock>

</apex:form>  
</apex:page>
Best Answer chosen by Rakhesh T
Krishna SambarajuKrishna Sambaraju
Change your code as follows
function myfun() {
        var name = document.getElementById('it').value;

        // This remoting call will use the page's timeout value
        Visualforce.remoting.Manager.invokeAction(
            '{!$RemoteAction.RemoteFunction1CNTRL.call}',
            name, 
            handleResponse,
			{escape:true}
        );
    }
For more details on javascript remoting check this document.
https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_js_remoting_configuring_request.htm

Hope this helps.

Regards,
Krishna.

All Answers

Krishna SambarajuKrishna Sambaraju
Change your code as follows
function myfun() {
        var name = document.getElementById('it').value;

        // This remoting call will use the page's timeout value
        Visualforce.remoting.Manager.invokeAction(
            '{!$RemoteAction.RemoteFunction1CNTRL.call}',
            name, 
            handleResponse,
			{escape:true}
        );
    }
For more details on javascript remoting check this document.
https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_js_remoting_configuring_request.htm

Hope this helps.

Regards,
Krishna.
This was selected as the best answer
Rakhesh TRakhesh T
Thank you Krishna, Now I am able capture the call back result but my code to put the result in output text in a page block:

document.getElementById("{!$Component.pb2.sid}").innerHTML=result.Id;
document.getElementById("{!$Component.pb2.sname}").innerHTML=result.Name;
document.getElementById("{!$Component.pb2.sphone}").innerHTML=result.Phone;


is not working, donno the reason. can you please check it as well?
Krishna SambarajuKrishna Sambaraju
Hi Rakhesh,

It may be because "{!$Component.pb2.sid}" is not working as you expect. Try getting the element by it's id as follows.
document.getElementById("sid").innerHTML = result.Id;
and check if that works.

Kind Regards,
Krishna.
Krishna SambarajuKrishna Sambaraju
If you want to use $component then you should start from the top most element, in your case you should start from your form as follows.
document.getElementById("{!$Component.fm.pb2.sid}").innerHTML=result.Id;

Hope this helps.