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
devloper sfdcdevloper sfdc 

Action Function Not working please help

public class BBH_Survey {
    public  String   Survey_res {get;set;}
	
	 Public BBH_Survey()
    {
        CasId='50023000001pb1w';
    }
	Public  static void survey_1(){
    
     List<case> lstCase=[select id,status ,Survey_fill__c from case where id =:CasId];
       
              BBH_Surveys__c bbh=new BBH_Surveys__c();
               bbh.CaseId__c=lstCase[0].id;
               bbh.Survey_Response__c= '1';
               insert bbh;
           
        
    }
	}
	
	
	<apex:page controller="BBH_Survey" sidebar="false" >
<apex:form >
<script>
    function bbh_survey_1()
    {
    survey_1();
    }


</script> 
 <apex:actionFunction name="survey_1" action="{!survey_1}"/>  
    <apex:commandButton value="1" onclick="bbh_survey_1();" styleClass="btn1" />
	</apex:form>
	<apex:page>

Hello All,

My Action function is not working please help me.

Regards
Sebastian 
Madhukar_HeptarcMadhukar_Heptarc
Hi Sebastian,

Can you please try below code 
Apex Class :
=========
public class BBH_Survey 
{
    public  String   Survey_res {get;set;}
    public  id  CasId  {get;set;}
    Public BBH_Survey()
    {
        CasId='50023000001pb1w';
    }
    Public void survey_1(){ // Here i have removed Static 
        List<case> lstCase=[select id,status ,Survey_fill__c from case where id =:CasId];
        BBH_Surveys__c bbh=new BBH_Surveys__c();
        bbh.CaseId__c=lstCase[0].id;
        bbh.Survey_Response__c= '1';
        insert bbh;
    }
}

Visual force Page :
=============
<apex:page controller="BBH_Survey" sidebar="false" >
    <apex:form >
        <script>
        function bbh_survey_1()
        {
            survey_1();
        }
        </script> 
        <apex:actionFunction name="survey_1" action="{!survey_1}"/>  
        <apex:commandButton value="Insert" onclick="bbh_survey_1();" styleClass="btn1" />
    </apex:form>
</apex:page>
(or)
If you want to try with Static method you need to intialize variable inside the static method. please have a look at below example 
Apex class :
=========
public class BBH_Survey 
{
    public  String   Survey_res {get;set;}
    public static id Casid = '50023000001pb1w'; // CasID
    Public BBH_Survey()
    {
	// I have moved CasID from constuctor to Static method (or) you can define on public access level 
    }
    Public static void survey_1(){
        // Id CasId='50023000001pb1w'; // Either you can define here (or) Above 
        List<case> lstCase=[select id,status ,Survey_fill__c from case where id =:CasId];
        BBH_Surveys__c bbh=new BBH_Surveys__c();
        bbh.CaseId__c=lstCase[0].id;
        bbh.Survey_Response__c= '1';
        insert bbh;
    }
}

Visual Force Pages:
===============
<apex:page controller="BBH_Survey" sidebar="false" >
    <apex:form >
        <script>
        function bbh_survey_1()
        {
            survey_1();
        }
        </script> 
        <apex:actionFunction name="survey_1" action="{!survey_1}"/>  
        <apex:commandButton value="Insert" onclick="bbh_survey_1();" styleClass="btn1" />
    </apex:form>
</apex:page>

Static method cannot find the casID

Reference link : https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_static.htm
Plese let me know if it is useful (Or) Any help required.

Thanks & Regrads
Madhukar