+ Start a Discussion

Action function - Error : The name can only contain underscores and alphanumeric characters

Hi All - I am adding the action function in my visualforce page. I want to invoke an method from my controller on the onblur of the attribute. I get the following error, When I preview the visualforce page : The name can only contain underscores and alphanumeric characters. It must begin with a letter and be unique, and must not include spaces, end with an underscore, or contain two consecutive underscores.

Visualforce page code
<apex:page standardController="AccountLocation__c" extensions="AccountLocationEditController" recordSetVar="AccountLocation__c" >
      function javaScript(){
    <apex:actionFunction name="location" action="{!location}"  rerender="loc"/>
    <apex:param name="firstParam" assignTo="{!AccLoc.Name}" value="Bill To" />
    <apex:pageBlock title="Account Location Edit" mode="edit">
    <apex:pageBlockButtons >
    <apex:commandButton action="{!save}" value="Save"/>
    <apex:commandButton action="{!savenew}" value="Save & New"/>
    <apex:commandButton action="{!Cancel}" value="Cancel"/>
    <apex:pageBlockSection title="Information" columns="2">
    <apex:inputField  value="{!AccLoc.Type__c}" required="true" onblur="javaScrpt()" />
    <apex:inputField value="{!AccLoc.AccountId__c}" />
    <apex:inputField id = "loc" value="{!AccLoc.Name}"  />

Controller Code
public with sharing class AccountLocationEditController{
    Public AccountLocation__c AccLoc {get;set;}
    /*ApexPages.StandardController Controller = null;*/
 public AccountLocationEditController(ApexPages.StandardSetController stdController) {
       AccLoc = new AccountLocation__c ();
       Id strId= ApexPages.currentPage().getParameters().get('id');
      /*  if (strId!=null){
            AccLoc = [SELECT Id, Name,Type__c
                      FROM AccountLocation__c
                      WHERE Id = :strId];
          system.debug('loc name' +AccLoc.Name);
   Public PageReference location(){
       if (AccLoc.Type__c == 'Bill TO'){
                AccLoc.Name = 'Bill To Address';
          system.debug('loc name' +AccLoc.Name);
      }else {
          AccLoc.Name = 'Ship To Address';
        return null;
Please let me know if I had missed anything. Thanks you in Advance !
Best Answer chosen by SuAk
Issue was with this line

AccLoc = new AccountLocation__c ();

updated to AccountLocation__c AccLoc = new AccountLocation__c . Issue was resolved.

All Answers


I see a typo in the name of the function that needs to be called on the onblur event of <apex:inputField>. Update the below line of code
<apex:inputField  value="{!AccLoc.Type__c}" required="true" onblur="javaScrpt()" />

to the below code and this should address your issue.
<apex:inputField  value="{!AccLoc.Type__c}" required="true" onblur="javaScript();" />

One other thing to remember is to avoid usage of keywords as function names that are already reserved for Apex, Visualforce or Javascript within your code which could lead to issues. For e.g. rename the javaScript function to something more meaningful that defines the purpose of the function and is inuitive for e.g. findLocation() or getLocation().

Please mark the thread as SOLVED and answer as the BEST ANSWER if it helps address your issue.
Hi Shah - I updated the value but still I get the same error.

What is the name of your Visualforce Page? Perhaps there could be a problem with the naming convention of the page. Try changing the name of the Visualforce page.
Hi Shah - Name of the VF is AccountLocationEdit

Try removing the suffixed __c in your recordSetVar value of your <apex:page> tag. Change AccountLocation__c to accLoc.
I changed but I get the same error.

Can you share the generated Debug Log to get details around the exact point of failure?
Issue was with this line

AccLoc = new AccountLocation__c ();

updated to AccountLocation__c AccLoc = new AccountLocation__c . Issue was resolved.
This was selected as the best answer