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
Ritesh__Ritesh__ 

Unknown function addElement. Check spelling apex salesforce

 i created a visualforce page in which there is a code segment

<apex:column ><apex:commandButton value="{!if(item.i == size-1,'Add','Delete')}" action="{!if(size == 1,addElement(),removeElement())}"></apex:commandButton></apex:column>

i created a custom controller containing function

public PageReference addElement(){

return null;

}
public PageReference removeElement(){

return null;

}

but when i try to save the visualforce page it is giving me error

Error: Unknown function addElement. Check spelling  

can any one please tell me how to use if structure in action attribute so that it will properly work

Best Answer chosen by Admin (Salesforce Developers) 
sfdcfoxsfdcfox

Apparently, you can't use if statements in action attributes (I tried this in my dev org).

 

Instead, you can either: (1) use two command buttons, and render only the option that's currently available via the rendered attribute, or (2) have the command button call a common function that in turn calls the appropriate action.

 

Ex 1:

<apex:commandButton value="Add" rendered="{!item.i==size-1}" action="{!addElement}!"/>
<apex:commandButton value="Delete" rendered="{!item.i!=size-1}" action="{!removeElement}"/>

Ex 2:

 

<apex:commandButton value="{!item.actionlabel}" action="{!item.actioncommand}" />

 
Where actioncommand is simply a function that calls one or the other function depending on the condition.