• Bijay Sahu
  • NEWBIE
  • 0 Points
  • Member since 2018

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 2
    Questions
  • 2
    Replies
Hi All,
I have a dynamic table and reach row binding with inputCheckbox. onClick i am calling an Apex class methos but method is not calling. Could anyone please help and Thanking you in advance.

My Apex Page
<apex:page controller="DynamicCheckBox_Ctrl">
  <apex:form >
      <apex:pageBlock >
          <apex:pageBlockSection columns="1">
              <apex:inputText value="{!firstValue}" label="Enter Field Name"/>
              <apex:inputText value="{!secondValue}" label="Enter another Filed Name"/>
          </apex:pageBlockSection>
          <apex:pageBlockButtons location="top">
              <apex:commandButton value="Get Data" action="{!showValues}"/>
              <apex:commandButton value="Delete" onclick="if (!confirmDelete()) return false;" action="{!deleteAccount}"  />
          </apex:pageBlockButtons>
          <apex:outputPanel rendered="{!hideTable}">
              <apex:pageblockTable value="{!accList}" var="acct" >
                  <apex:column >
                    <apex:inputCheckbox onclick="storeAccoutIds({acct.id},selected)" />
                  </apex:column>
                  <apex:repeat value="{!lstFields}" var="filedName">
                      <apex:column value="{!acct[filedName]}"/>
                  </apex:repeat>
              </apex:pageblockTable>
          </apex:outputPanel>
      </apex:pageBlock>
  </apex:form>
  <script>
      function confirmDelete()
      {
        return confirm(‘Are you sure you want to delete?’);
      }
  </script>
</apex:page>

Apex Class

public class DynamicCheckBox_Ctrl {
    
    public string prepareQuery{get;set;}
    public string firstValue{get;set;}
    public string secondValue{get;set;}
    public boolean hideTable{get;set;}
    public boolean enableDelButton{get;set;}
    
    public list<Account> accList{get;set;}
    public List<String> lstFields{get;set;}
    
    //store account id, if checkbox is checkedselectedcheckbox
    public Map<String,Boolean> mapAccountId = new Map<String,Boolean>();

         
    public void storeAccoutIds(string accId, boolean isSelected)
    {
        if(isSelected)
        {
            mapAccountId.put(accId,isSelected);
            enableDelButton = true;
        }   
        
        system.debug(mapAccountId);
    }
    
       
    //Get records from datanase and send to apex page
    public void showValues()
    {
        try{

            //Add all the fileds into a list
            lstFields=new list<String>();
            lstFields.add('id');
            lstFields.add(firstValue);
            lstFields.add(secondValue);

            system.debug(lstFields);

            //Prepare dynamic query
            prepareQuery = 'SELECT id,'+firstValue+','+secondValue+' FROM Account';
            system.debug(prepareQuery);

            //Execute query
            accList = new List<Account>();
            accList = Database.query(prepareQuery);
            
            if(accList.size()>0)
            {
                hideTable=true;
            }
            system.debug(accList);

        }catch(Exception ex){
            ApexPages.Message msg = new ApexPages.Message(ApexPages.Severity.Error, ex.getMessage());
            ApexPages.addMessage(msg);         
        }
    }
Hi All,
I am new to salesforce Apex page and class. I have written a Apex class to prepare a dynamic query based on user input. In my apex page i have two inputbox where user can input filed name. After that by clicking on a command button i am getting those values and making a dynamic query and executing. after executing the data should populate in frontend.

If i am testing Apex class from  Anonymous window. Apex class is working fine where as if i try to bind the query returned values in Apex page it is throwing me an error. "Invalid field id, for SObject Account "

Could anyoine help me here.

Apex page
<apex:page controller="DynamicQuery_ctrl">
    <apex:form >
        <apex:pageBlock >
            <apex:pageMessages/>
            <apex:pageBlockSection columns="1">
                <apex:inputText value="{!firstValue}" label="Field Name"/>
                <apex:inputText value="{!secondValue}" label="Field Name"/>
            </apex:pageBlockSection>
            <apex:pageBlockButtons >
                <apex:commandButton value="Show" action="{!showValues}"/>
            </apex:pageBlockButtons>
            <apex:pageBlockTable value="{!accList}" var="acc">
                <apex:repeat value="{!lstFields}" var="fieldName">
                    <apex:column value="{!acc[fieldName]}"/>
                </apex:repeat>
            </apex:pageBlockTable>
        </apex:pageBlock>
    </apex:form>
</apex:page>

Apex Class
public class DynamicQuery_ctrl {
    
    public string prepareQuery{get;set;}
    public string firstValue{get;set;}
    public string secondValue{get;set;}
            
    public list<Account> accList{get;set;}
    public List<String> lstFields{get;set;}
      
    public void showValues()
    {
        try{
            //Add all the fileds into a list
            lstFields=new list<String>();
            lstFields.add('id'+',');
            lstFields.add(firstValue+',');
            lstFields.add(secondValue);
            system.debug(lstFields);
                    
            //Prepare dynamic query
            prepareQuery = 'SELECT id,'+firstValue+','+secondValue+' FROM Account';
            system.debug(prepareQuery);
            
            //Execute query
            accList = new List<Account>();
            accList = Database.query(prepareQuery);
            
            system.debug(accList);
                       
        }catch(Exception ex){
         ApexPages.Message msg = new ApexPages.Message(ApexPages.Severity.Error, ex.getMessage());
         ApexPages.addMessage(msg);          
      }
        
    }
}
Hi All,
I am new to salesforce Apex page and class. I have written a Apex class to prepare a dynamic query based on user input. In my apex page i have two inputbox where user can input filed name. After that by clicking on a command button i am getting those values and making a dynamic query and executing. after executing the data should populate in frontend.

If i am testing Apex class from  Anonymous window. Apex class is working fine where as if i try to bind the query returned values in Apex page it is throwing me an error. "Invalid field id, for SObject Account "

Could anyoine help me here.

Apex page
<apex:page controller="DynamicQuery_ctrl">
    <apex:form >
        <apex:pageBlock >
            <apex:pageMessages/>
            <apex:pageBlockSection columns="1">
                <apex:inputText value="{!firstValue}" label="Field Name"/>
                <apex:inputText value="{!secondValue}" label="Field Name"/>
            </apex:pageBlockSection>
            <apex:pageBlockButtons >
                <apex:commandButton value="Show" action="{!showValues}"/>
            </apex:pageBlockButtons>
            <apex:pageBlockTable value="{!accList}" var="acc">
                <apex:repeat value="{!lstFields}" var="fieldName">
                    <apex:column value="{!acc[fieldName]}"/>
                </apex:repeat>
            </apex:pageBlockTable>
        </apex:pageBlock>
    </apex:form>
</apex:page>

Apex Class
public class DynamicQuery_ctrl {
    
    public string prepareQuery{get;set;}
    public string firstValue{get;set;}
    public string secondValue{get;set;}
            
    public list<Account> accList{get;set;}
    public List<String> lstFields{get;set;}
      
    public void showValues()
    {
        try{
            //Add all the fileds into a list
            lstFields=new list<String>();
            lstFields.add('id'+',');
            lstFields.add(firstValue+',');
            lstFields.add(secondValue);
            system.debug(lstFields);
                    
            //Prepare dynamic query
            prepareQuery = 'SELECT id,'+firstValue+','+secondValue+' FROM Account';
            system.debug(prepareQuery);
            
            //Execute query
            accList = new List<Account>();
            accList = Database.query(prepareQuery);
            
            system.debug(accList);
                       
        }catch(Exception ex){
         ApexPages.Message msg = new ApexPages.Message(ApexPages.Severity.Error, ex.getMessage());
         ApexPages.addMessage(msg);          
      }
        
    }
}