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
Alexander KrykunAlexander Krykun 

<apex:actionSupport> question

I  have simple Sobject in my bd, with 2 number fields: index1__c, index2__c:

on my visualforce page i'm going to create record of this object only when these filelds are equal.

if they aren't I want to render another field of this objcet which says why they aren't equal

Creating the record must be obtained by pressing enter key. 

On pressing enter nothing happens.

Is any restrictons where  <apex:actionSupport> tag must be placed in? 

VF page 

<apex:page id="HM6" controller="HM6Controller">
    <apex:form >
        <apex:pageBlock >
            <apex:pageBlockSection >
                <apex:inputField value="{!hm.index1__c}"/>
                <apex:inputField value="{!hm.index2__c}"/>
               
             
            </apex:pageBlockSection>
            
        </apex:pageBlock>
        
        <apex:outputpanel id="block">
        
         <apex:outputField value="{!hm.reason_not_equals__c}" rendered="{!NOT(check)}" />
          <apex:actionSupport event="onkeypress" action="{!save}" reRender="block" />
          
        </apex:outputpanel>/>
        <apex:pageMessages />
    </apex:form>
</apex:page>
 

controller

/**
 * Created by LanaPC on 22.06.2016.
 */

public with sharing class HM6Controller {
    public HM6__c hm;

    public HM6__c getHM(){
        return hm;

    }
    public boolean check;

    private String fillMessalge(Decimal index1, Decimal index2){
        if(index1>index2) {
            return 'index1>index2';
        }else return 'inde1<index2';
    }

    public HM6Controller(){
        hm=new HM6__C();

    }

    public Boolean getcheck(){
        if (hm.index1__c==hm.index2__c){
        check = true;
        return check;
        }
        else {
           hm.reason_not_equals__c= fillMessalge(hm.index1__c,hm.index2__c);
            check= false;
            return  check;
        }
    }

    public PageReference save(){
        if (getcheck()) insert hm;
        return null;
    }


}
Jerun JoseJerun Jose
Looking through the documents at
https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_compref_actionSupport.htm
The example seems to suggest that the actionsupport binds to the element it is wrapped in.

In your example that would be the outputpanel with id="block". However, as there are no input fields in this outputPanel, a user will never be able to register a keypress in this section and so the actionsupport is never invoked.

I believe you will have to add the actionsupport to the pageblocksection where the 2 input fields are displayed.

Also, note that you are calling the "save" method on EVERY keypress, you have not filtered for only the ENTER key. You will need some custom javascript to identify when the ENTER key was pressed.