0

VF:

<apex:page controller="jsclass">
<apex:form >
<script>
  function getValue(){
    var fieldValue = document.getElementById('test').value;
    Visualforce.remoting.Manager.invokeAction('{!$RemoteAction.jsclass.addAccounts}',acc,function(result, event)
    {
        if (event.status)
        {
           document.getElementById("{!$Component.theBlock.pbs.pbs1.accId}")
                                  .innerHTML = result.Id;
        }
    },{escape:true});
  }
</script>
<apex:inputtext id="test"/>
<apex:pageBlock id="theBlock" >
<apex:pageBlockSection id="pbs" >
<apex:pageblocksectionitem id="pbs1">
<apex:outputtext id="accId">
</apex:outputtext>
</apex:pageblocksectionitem>
</apex:pageBlockSection>
</apex:pageBlock> 
</apex:form>
</apex:page>

class:


public class jsclass {
public string acc {get; set;}
public account a {get; set;}
@RemoteAction
    public static void addAccounts(string acc){
      String ac = acc.Substring(0,acc.length()-1);
       Account a =[Select Id from account where name like'%ac'];
       system.debug(a);
}
}
  • Can you please provide more details on what error you are receiving and what line it is occurring on? – techbusinessman Aug 2 '15 at 17:37
  • no errors i cant see the recordid corresponds to the name i enter – Vidhyasagaran Muralidharan Aug 2 '15 at 17:44
  • 1
    In your select statement you have where name like '%ac' when you should most likely have where name like :acc so that you are keying off the parameter and not the static string. Also in your javascript I don't see where you set the acc parameter you are passing in. – techbusinessman Aug 2 '15 at 17:46
  • To add to @techbusinessman's comment, String acLike = '%' + ac; and name like :acLike. But you are returning nothing either so its hard to see what you are trying to do. – Keith C Aug 2 '15 at 21:18
1

I could not find where you are calling the getValue() function also you are not returning anything from your addAccounts() method. Below is the working version of your code. I tried to update the code to meet your requirement. I am calling getValue() method form onkeyup() event on the input text. The addAccounts() is now returning Account object matching the name. You can update further based on your need. Hope this helps.

VF Page

<apex:page controller="jsclass">
  <apex:form >
    <script>
          function getValue(){
            var fieldValue = document.getElementById("{!$Component.test}").value;
            Visualforce.remoting.Manager.invokeAction('{!$RemoteAction.jsclass.addAccounts}',fieldValue,function(result, event){
                if (event.status){
                   document.getElementById("{!$Component.theBlock.pbs.pbs1.accId}").innerHTML = result.Id;
                }
            },{escape:true});
          }
    </script>   
    <apex:inputtext id="test" onkeyup="getValue();" />
    <apex:pageBlock id="theBlock" >
      <apex:pageBlockSection id="pbs">
        <apex:pageblocksectionitem id="pbs1">
          <apex:outputText id="accId" value="accId"/>
        </apex:pageblocksectionitem>
      </apex:pageBlockSection>
    </apex:pageBlock>       
    </apex:form>
</apex:page>

Controller

public class jsclass {
public string acc {get; set;}
public account a {get; set;}
@RemoteAction
    public static Account addAccounts(string acc){
       String ac = '%' + acc.Substring(0,acc.length()-1) +'%';
       Account a =[Select Id from account where name like :ac limit 1];
       system.debug(a);
       return a;
    }
}
  • @Vidhyasagaran Muralidhara If it was the answer can you please mark it as answer. Thanks. – Saroj Bera Aug 3 '15 at 7:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.