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
DeepVDeepV 

SOQL on account fields

Hi,

I'm trying to get the custom field (Hold_ID__c) on to Visualforce page. I am not getting the value in the page.

Passing account id in the url for the page

Here is my VF page:

<apex:page controller="Questionnaire" sidebar="false" showHeader="false">
    <apex:form >
        <apex:pageblock >
           <apex:pageBlockSection columns="1">
            <apex:inputText value="{!account}" label="Hold Name"/>
            <apex:inputText value="{!holdId}" label="Hold ID"/>
            <apex:inputText id="Survey" value="{!txtSurvey}"/>
            <apex:commandButton action="{!save}" value="save"/>
           </apex:pageBlockSection>
        </apex:pageblock>
    </apex:form>
</apex:page>

Apex code:

public class Questionnaire{

    public String holdId { get; set; }

    public String txtSurvey { get; set; }

    public String account { get; set; }
    
    public Questionnaire() {
    account =  [SELECT Id, Name,Account.Hold_ID__c FROM Account WHERE Id = :ApexPages.currentPage().getParameters().get('id')].Name;
    holdId = [SELECT Id, Name, Hold_ID__c FROM Account WHERE Id = :ApexPages.currentPage().getParameters().get('id')].Hold_ID__c;
    }
    
      public PageReference save() {
         Audit_Questionnaire__c aq = new Audit_Questionnaire__c();
            aq.Name = txtSurvey;
            aq.account__c = apexpages.currentpage().getparameters().get('id');
            insert aq;
            return null;
        }
}
 
Best Answer chosen by DeepV
Ajay K DubediAjay K Dubedi
Hi DeepV,
Custom field (Hold_ID__c) is hold the value and show in visualforce page.

Output is shown below :-

User-added image

Passing account id in the url for the page.
Try below code : 

Apex code :
  public class Questionnaire{
   public String holdId { get; set; }
   public  string holdIds { get; set; }
   public String txtSurvey { get; set; }
   public string accId { get; set; }
   public String account { get; set; }
  public Questionnaire(){
       accId = ApexPages.currentPage().getParameters().get('id');
       system.debug(':::::::::accId='+accId);
       account =  [SELECT Id, Name,Account.Hold_ID__c FROM Account WHERE Id = :accId].Name;
       system.debug('::::::::::account='+account);
       holdIds = ApexPages.currentPage().getParameters().get('id');
       system.debug(':::::::::::holdIds='+holdIds);
       holdId = [SELECT Id, Name, Hold_ID__c FROM Account WHERE Id = :holdIds].Hold_ID__c;
       system.debug(':::::::::::holdId='+holdId);
   }
  public PageReference save(){
       Audit_Questionnaire__c aq = new Audit_Questionnaire__c();
       aq.Name = txtSurvey;
       aq.account__c = apexpages.currentpage().getparameters().get('id');
       insert aq;
       system.debug('::::::::::aq='+aq);
       return null;
   }
}
Visualforce Page :
<apex:page controller="Questionnaire" sidebar="false" showHeader="false">
   <apex:form >
       <apex:pageblock >
          <apex:pageBlockSection columns="1">
           <apex:inputText value="{!account}" label="Hold Name"/>
           <apex:inputText value="{!holdId}" label="Hold ID"/>
           <apex:inputText id="Survey" value="{!txtSurvey}"/>
           <apex:commandButton action="{!save}" value="save"/>
          </apex:pageBlockSection>
       </apex:pageblock>
   </apex:form>
</apex:page>
I hope it will help you.

Regards,
Ajay

All Answers

LBKLBK
hi DeepV,

Try this code.

APEX
public class Questionnaire{

    public String holdId { get; set; }

    public String txtSurvey { get; set; }

    public Account account { get; set; }
    
    public Questionnaire() {
    account =  [SELECT Id, Name,Account.Hold_ID__c FROM Account WHERE Id = :ApexPages.currentPage().getParameters().get('id')];
    
    }
    
      public PageReference save() {
         Audit_Questionnaire__c aq = new Audit_Questionnaire__c();
            aq.Name = txtSurvey;
            aq.account__c = account.Id;
            insert aq;
            return null;
        }
}
VF Page
<apex:page controller="Questionnaire" sidebar="false" showHeader="false">
    <apex:form >
        <apex:pageblock >
           <apex:pageBlockSection columns="1">
            <apex:inputText value="{!account.Name}" label="Hold Name"/>
            <apex:inputText value="{!account.Hold_ID__c}" label="Hold ID"/>
            <apex:inputText id="Survey" value="{!txtSurvey}"/>
            <apex:commandButton action="{!save}" value="save"/>
           </apex:pageBlockSection>
        </apex:pageblock>
    </apex:form>
</apex:page>
Let me know if this helps.

 
LBKLBK
Minor change in the APEX. Here is the code.
public class Questionnaire{

    public String holdId { get; set; }

    public String txtSurvey { get; set; }

    public Account account { get; set; }
    
    public Questionnaire() {
    account =  [SELECT Id, Name, Hold_ID__c FROM Account WHERE Id = :ApexPages.currentPage().getParameters().get('id')];
    
    }
    
      public PageReference save() {
         Audit_Questionnaire__c aq = new Audit_Questionnaire__c();
            aq.Name = txtSurvey;
            aq.account__c = account.Id;
            insert aq;
            return null;
        }
}

 
Ajay K DubediAjay K Dubedi
Hi DeepV,
Custom field (Hold_ID__c) is hold the value and show in visualforce page.

Output is shown below :-

User-added image

Passing account id in the url for the page.
Try below code : 

Apex code :
  public class Questionnaire{
   public String holdId { get; set; }
   public  string holdIds { get; set; }
   public String txtSurvey { get; set; }
   public string accId { get; set; }
   public String account { get; set; }
  public Questionnaire(){
       accId = ApexPages.currentPage().getParameters().get('id');
       system.debug(':::::::::accId='+accId);
       account =  [SELECT Id, Name,Account.Hold_ID__c FROM Account WHERE Id = :accId].Name;
       system.debug('::::::::::account='+account);
       holdIds = ApexPages.currentPage().getParameters().get('id');
       system.debug(':::::::::::holdIds='+holdIds);
       holdId = [SELECT Id, Name, Hold_ID__c FROM Account WHERE Id = :holdIds].Hold_ID__c;
       system.debug(':::::::::::holdId='+holdId);
   }
  public PageReference save(){
       Audit_Questionnaire__c aq = new Audit_Questionnaire__c();
       aq.Name = txtSurvey;
       aq.account__c = apexpages.currentpage().getparameters().get('id');
       insert aq;
       system.debug('::::::::::aq='+aq);
       return null;
   }
}
Visualforce Page :
<apex:page controller="Questionnaire" sidebar="false" showHeader="false">
   <apex:form >
       <apex:pageblock >
          <apex:pageBlockSection columns="1">
           <apex:inputText value="{!account}" label="Hold Name"/>
           <apex:inputText value="{!holdId}" label="Hold ID"/>
           <apex:inputText id="Survey" value="{!txtSurvey}"/>
           <apex:commandButton action="{!save}" value="save"/>
          </apex:pageBlockSection>
       </apex:pageblock>
   </apex:form>
</apex:page>
I hope it will help you.

Regards,
Ajay
This was selected as the best answer
DeepVDeepV
ThankQ every one.