function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
saisaisaisai 

Remoting method not getting called.

Here is my VF Page and below is my code.

When i try to run this only alert1 & 2 & 3 r getting called but the method in the controller is not getting invoked.

Please help what i m missing.

 

<apex:page controller="RemoteController" id="thePage">
<apex:form id="theForm" >
<apex:pageBlock id="theBlock" >
<apex:pageblockSection columns="1" id="searchSection">
<apex:pageBlockSectionItem id="item" >
<apex:inputText id="searchBox" />
<input type="button" value="Search" onclick="getRemoteAccount()"/>
</apex:pageBlockSectionItem>
</apex:pageblockSection>
</apex:pageBlock>

</apex:form>
<script type="text/javascript">
function getRemoteAccount() {

var accountName = document.getElementById('thePage:theForm:theBlock:searchSection:item:searchBox').value;
alert('1')
Visualforce.remoting.Manager.invokeAction( '{!$RemoteAction.RemoteController.getAccount}', accountName,
function(result, event){

var account = result[0];
alert('2')
if(account==null)
alert('3')
alert(account.Name);
if (event.status) {
// document.getElementById('acctId').innerHTML = result.Id
// document.getElementById('acctName').innerHTML = result.Name;
}
else if (event.type === 'exception') {
//document.getElementById(“responseErrors").innerHTML = event.message;
alert(event.message);
}
else {
// document.getElementById(“responseErrors").innerHTML = event.message;
alert(event.message);
}
},
{escape: true}
);
}
</script>

</apex:page>

 

****

 

public class RemoteController {
public RemoteController(){
}

@RemoteAction
public static List<Account> getAccount(String accName) {
System.Debug('Hello');
List<Account> lstAcc = [select id,name from Account where name =:accName];
if(!lstAcc.isEmpty()){
return lstAcc;
}
return new List<Account>();
}
}

 

Thanks

Sai

 

 

Best Answer chosen by Admin (Salesforce Developers) 
SFDC_VikashSFDC_Vikash

Hi,

 

Your code seems write which you have posted here.

 

Also you can compare with this code as follows :

 

public class AccountSearchRemote {
  
  public AccountSearchRemote (){
  }

  @RemoteAction
  public static List<Account> getAccount(String accName) {
    System.Debug('Hello');
    List<Account> lstAcc = [select id,name from Account where name =:accName];
    if(!lstAcc.isEmpty()){
      return lstAcc;
    }
   return new List<Account>();
  }
}

 

<apex:page id="thePage" Controller="AccountSearchRemote">
<script>
  function getRemoteAccount(){
    var accountName = document.getElementById('thePage:theForm:theBlock:searchSection:item:searchBox').value;    
    Visualforce.remoting.Manager.invokeAction( '{!$RemoteAction.AccountSearchRemote.getAccount}', accountName,
    function(result, event){
        var account = result[0];
        if(account == null){
          alert('No account found.');
        }
        else{
          alert(account.Name);        
        }
    },
    {escape: true}
    );
  }
</script>
  <apex:form id="theForm" >
    <apex:pageBlock id="theBlock" >
        <apex:pageblockSection columns="1" id="searchSection">
            <apex:pageBlockSectionItem id="item" >
                <apex:inputText id="searchBox" />
                <input type="button" value="Search" onclick="getRemoteAccount();"/>
            </apex:pageBlockSectionItem>
        </apex:pageblockSection>        
    </apex:pageBlock>
  </apex:form>
</apex:page>

 According to above code i am able to get alert with account name if there is an account with matched name else get no match found alert.

 

So might be possible your code is not working due to any other part of code. I can help you if you post the exact code.

 

Please mark it as solution if it works for you.

 

Thanks

Vikash Goyal

 

All Answers

Bhawani SharmaBhawani Sharma
Did you try with debugging?
SFDC_VikashSFDC_Vikash

Hi,

 

Your code seems write which you have posted here.

 

Also you can compare with this code as follows :

 

public class AccountSearchRemote {
  
  public AccountSearchRemote (){
  }

  @RemoteAction
  public static List<Account> getAccount(String accName) {
    System.Debug('Hello');
    List<Account> lstAcc = [select id,name from Account where name =:accName];
    if(!lstAcc.isEmpty()){
      return lstAcc;
    }
   return new List<Account>();
  }
}

 

<apex:page id="thePage" Controller="AccountSearchRemote">
<script>
  function getRemoteAccount(){
    var accountName = document.getElementById('thePage:theForm:theBlock:searchSection:item:searchBox').value;    
    Visualforce.remoting.Manager.invokeAction( '{!$RemoteAction.AccountSearchRemote.getAccount}', accountName,
    function(result, event){
        var account = result[0];
        if(account == null){
          alert('No account found.');
        }
        else{
          alert(account.Name);        
        }
    },
    {escape: true}
    );
  }
</script>
  <apex:form id="theForm" >
    <apex:pageBlock id="theBlock" >
        <apex:pageblockSection columns="1" id="searchSection">
            <apex:pageBlockSectionItem id="item" >
                <apex:inputText id="searchBox" />
                <input type="button" value="Search" onclick="getRemoteAccount();"/>
            </apex:pageBlockSectionItem>
        </apex:pageblockSection>        
    </apex:pageBlock>
  </apex:form>
</apex:page>

 According to above code i am able to get alert with account name if there is an account with matched name else get no match found alert.

 

So might be possible your code is not working due to any other part of code. I can help you if you post the exact code.

 

Please mark it as solution if it works for you.

 

Thanks

Vikash Goyal

 

This was selected as the best answer