+ Start a Discussion
Jyoti TrailheadJyoti Trailhead 

Sample code for option button on VF page and getting value in apex class

Hi
I need to built a page with say two or more fields – their values will be Yes or No – based on if it is selected or not.
Get these field values in class and based on field values set variable values and call the function e.g:
VF Page:
Text1: Yes(it is a radio button)
Text2: Yes(radio)
call saveList from action button on VF page.
Apex Class:
Public String Cat
Public String Req
public PageReference saveList()
{
if(Text1 == Yes):
Cat = ‘abc’
Req = ‘abc’
CallFunction(Cat,Req)
}
if(Text2 == ‘Yes’){
Cat = ‘def’
Req = ‘def’
CallFunction(Cat,Req)
}
}
public void CallFuntion(String Cat, String Req)
{
statements;
}
Best Answer chosen by Jyoti Trailhead
Magesh Mani YadavMagesh Mani Yadav
hi Jyothi,
The error is not in your saveList method rather its in the trigger IncidentTrigger may be there are more soql queries in the trigger invocation.
You need to check the trigger
but we can even change the saveList and CreateTicket method as follows: to return a list and then we make single DML
 
public PageReference saveList()
  {
    List<BMCServiceDesk__Incident__c> listOfIncident = new List<BMCServiceDesk__Incident__c>();
        system.debug('CCMS value:  '+ ccms);
        if(ccms == 'Yes'){
        string Category = 'CCMS';
        string ReqDef = 'CCMS Access';
        listOfIncident.add(CreateTicket(Category, ReqDef));
        }
        if(jira == 'Yes'){
        string Category = 'JIRA';
        string ReqDef = 'JIRA Account';
        listOfIncident(CreateTicket(Category, ReqDef));
        }
        if(!listOfIncident.isEmpty()){
          insert listOfIncident;
        }

   return null;
  }
     
public  BMCServiceDesk__Incident__c CreateTicket(String Category, String ReqDef)
{
    BMCServiceDesk__Incident__c myinci1=new BMCServiceDesk__Incident__c(); 
    myinci1.BMCServiceDesk__incidentDescription__c='Test ticket';
    User u=[select id,login__c from User where login__c = 'jyotiv' limit 1]; //If you are assigning single record then use limit 1 otherwise use List<User>
    if(u!=null){ // Always check if the instance u is null or not before using it
      myinci1.BMCServiceDesk__FKClient__c= u.id;
    }        
    BMCServiceDesk__Category__c cat=[select id,name from BMCServiceDesk__Category__c where name =: Category limit 1]; //If you are assigning single record then use limit 1
    if(cat != null){
    myinci1.BMCServiceDesk__FKCategory__c=cat.id;
    }
    myinci1.BMCServiceDesk__contactType__c = NULL;
    myinci1.BMCServiceDesk__queueName__c=NULL;
    myinci1.BMCRF_AutoAssign__c = true;
    myinci1.BMCRF_Update_Note__c ='test';
    myinci1.BMCServiceDesk__queueName__c=NULL;
    BMCServiceDesk__SRM_RequestDefinition__c ref=[select id,name from BMCServiceDesk__SRM_RequestDefinition__c where name =: ReqDef limit 1];    //If you are assigning single record then use limit 1            
    if(ref != null){
      myinci1.BMCServiceDesk__FKRequestDefinition__c=ref.id;       
    }  
    return myinci1;  
 }


 

All Answers

Magesh Mani YadavMagesh Mani Yadav
Hi Jyothi,
the below link has an example for the radio button. may be this helps you.
https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_compref_selectRadio.htm

 
Rupal KumarRupal Kumar
Hi Jyothi,
           Try this code-

      
Hi Jyothi,
           Try this code-
vf page*****

<apex:page controller="sampleCon">
<style>
body .bPageBlock .pbBody .labelCol{
     padding-top: 13px;
   
 }
</style>
<apex:form >
<apex:pageBlock >
    <apex:pageBlockSection >
        <apex:pageBlockSectionItem >
           <apex:outputLabel value="Text" />
            <apex:selectRadio value="{!TextVal}" >
            <apex:selectOptions value="{!items}"/>
            </apex:selectRadio>
            
        </apex:pageBlockSectionItem>
        <apex:commandButton value="Test" action="{!test}" rerender="out" status="status"/>
    </apex:pageBlockSection>
    </apex:pageBlock>
</apex:form>
<apex:outputPanel id="out">
<apex:actionstatus id="status" startText="testing...">
<apex:facet name="stop">
<apex:outputPanel >
<p>You have selected:</p>
<apex:outputText value="{!TextVal}"/>
</apex:outputPanel>
</apex:facet>
</apex:actionstatus>
</apex:outputPanel>
</apex:page>


Controller*********


public class sampleCon {
String textValue = 'YES';

public PageReference test() {
return null;
}
public List<SelectOption> getItems() {
List<SelectOption> options = new List<SelectOption>();
options.add(new SelectOption('YES','Yes'));
options.add(new SelectOption('NO','No'));
 return options;
}

public String getTextVal() {
return textValue;
}
public void setTextVal(String textValue) { this.textValue = textValue; }
}

Thanks
Rupal Kumar
http://mirketa.com



 
Jyoti TrailheadJyoti Trailhead
Hi Rupal Thanks for your input – in my case – there are more than one Radio button. Can we create many radio buttons on VF page and call controller function like saveList – which is further calling another function for let’s say Case creation based on input Yes/No for radio button selected on VF page. It is giving me SOQL limit error right now. Regards Jyoti
Jyoti TrailheadJyoti Trailhead
Here is my code snippet: Please suggest what is wrong with this code - on selecting both radio button fields - tickets do not get created and SOQL error comes up.


VF Page:

<apex:page controller="NHM_SubmitTickets">
<title>{'Submit Tickets'}</title>
 <br></br>
Please choose your options : <br></br><br></br>
<apex:form >
   <apex:pageBlock >
      <apex:pageBlockSection >
        <apex:selectRadio label="CCMS Access" value="{!ccms}">
             <apex:selectOption itemValue="Yes"></apex:selectOption>
        </apex:selectRadio><p/>
        <apex:selectRadio label="JIRA Access" value="{!jira}">
             <apex:selectOption itemValue="Yes"></apex:selectOption>
        </apex:selectRadio><p/>
  <apex:commandButton value="Save" action="{!saveList}" rerender="out" status="status"/>
 <apex:outputPanel id="out">
<apex:actionstatus id="status" startText="testing...">
<apex:facet name="stop">
<apex:outputPanel >
<p>You have selected:</p> <apex:outputText value="{!ccms}"/> <apex:outputText value="{!jira}"/>
</apex:outputPanel>
</apex:facet>
</apex:actionstatus>
</apex:outputPanel>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:form>
</apex:page>


Controller:
public Class NHM_SubmitTickets
{
public string ccms{ get; set; }

public string jira{ get; set; }
public string tensilica{ get; set; }
public string Category = null;
public string ReqDef = null;

 public PageReference saveList()
  {
        system.debug('CCMS value:  '+ ccms);
        if(ccms == 'Yes'){
        Category = 'CCMS';
        ReqDef = 'CCMS Access';
        CreateTicket(Category, ReqDef);
        }
        if(jira == 'Yes'){
        Category = 'JIRA';
        ReqDef = 'JIRA Account';
        CreateTicket(Category, ReqDef);
        }
   return null;
  }
     
public static void CreateTicket(String Category, String ReqDef)
{
        BMCServiceDesk__Incident__c myinci1=new BMCServiceDesk__Incident__c(); 
        myinci1.BMCServiceDesk__incidentDescription__c='Test ticket';
        User u=[select id,login__c from User where login__c = 'jyotiv'];
        myinci1.BMCServiceDesk__FKClient__c= u.id;
        BMCServiceDesk__Category__c cat=[select id,name from BMCServiceDesk__Category__c where name =: Category];
        myinci1.BMCServiceDesk__FKCategory__c=cat.id;
        myinci1.BMCServiceDesk__contactType__c = NULL;
        myinci1.BMCServiceDesk__queueName__c=NULL;
        myinci1.BMCRF_AutoAssign__c = true;
        myinci1.BMCRF_Update_Note__c ='test';
        myinci1.BMCServiceDesk__queueName__c=NULL;
        BMCServiceDesk__SRM_RequestDefinition__c ref=[select id,name from BMCServiceDesk__SRM_RequestDefinition__c where name =: ReqDef];                
        myinci1.BMCServiceDesk__FKRequestDefinition__c=ref.id;         
        insert myinci1;  
 
 }
 }
 
Magesh Mani YadavMagesh Mani Yadav
Hi Jyoti
Here is the Updated class and vf page. i have included comments for your understanding
<apex:page controller="NHM_SubmitTickets">
<title>{'Submit Tickets'}</title>
 <br></br>
Please choose your options : <br></br><br></br>
<apex:form >
   <apex:pageBlock >
      <apex:pageBlockSection >
      <!--Use radio button if you have more than one options otherwise use checkbox-->
        <apex:selectRadio label="CCMS Access" value="{!ccms}">
            <apex:selectOption itemValue="Yes" itemLabel="Yes"/>
            <apex:selectOption itemValue="No" itemLabel="No"/>
        </apex:selectRadio><p/>
        <apex:selectRadio label="JIRA Access" value="{!jira}">
            <apex:selectOption itemValue="Yes" itemLabel="Yes"/>
            <apex:selectOption itemValue="No" itemLabel="No"/>             
        </apex:selectRadio><p/>
  <apex:commandButton value="Save" action="{!saveList}" rerender="out" status="status"/>
 <apex:outputPanel id="out">
<apex:actionstatus id="status" startText="testing...">
<apex:facet name="stop">
<apex:outputPanel >
<p>You have selected:</p> <apex:outputText value="{!ccms}"/> <apex:outputText value="{!jira}"/>
</apex:outputPanel>
</apex:facet>
</apex:actionstatus>
</apex:outputPanel>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:form>
</apex:page>
 
public Class NHM_SubmitTickets
{
public string ccms{ get; set; }

public string jira{ get; set; }
public string tensilica{ get; set; }
//public string Category = null; // dont use class variable if you want to assign it to both Jira and CCMS the value will be overwritten
//public string ReqDef = null;

 public PageReference saveList()
  {
        system.debug('CCMS value:  '+ ccms);
        if(ccms == 'Yes'){
        string Category = 'CCMS';
        string ReqDef = 'CCMS Access';
        CreateTicket(Category, ReqDef);
        }
        if(jira == 'Yes'){
        string Category = 'JIRA';
        string ReqDef = 'JIRA Account';
        CreateTicket(Category, ReqDef);
        }
   return null;
  }
     
public static void CreateTicket(String Category, String ReqDef)
{
    BMCServiceDesk__Incident__c myinci1=new BMCServiceDesk__Incident__c(); 
    myinci1.BMCServiceDesk__incidentDescription__c='Test ticket';
    User u=[select id,login__c from User where login__c = 'jyotiv' limit 1]; //If you are assigning single record then use limit 1 otherwise use List<User>
    if(u!=null){ // Always check if the instance u is null or not before using it
    	myinci1.BMCServiceDesk__FKClient__c= u.id;
    }        
    BMCServiceDesk__Category__c cat=[select id,name from BMCServiceDesk__Category__c where name =: Category limit 1]; //If you are assigning single record then use limit 1
    if(cat != null){
    myinci1.BMCServiceDesk__FKCategory__c=cat.id;
    }
    myinci1.BMCServiceDesk__contactType__c = NULL;
    myinci1.BMCServiceDesk__queueName__c=NULL;
    myinci1.BMCRF_AutoAssign__c = true;
    myinci1.BMCRF_Update_Note__c ='test';
    myinci1.BMCServiceDesk__queueName__c=NULL;
    BMCServiceDesk__SRM_RequestDefinition__c ref=[select id,name from BMCServiceDesk__SRM_RequestDefinition__c where name =: ReqDef limit 1];    //If you are assigning single record then use limit 1            
    if(ref != null){
    	myinci1.BMCServiceDesk__FKRequestDefinition__c=ref.id;       
    }  
    insert myinci1;  
 }

 }

I hope the issue is resolved.
Jyoti TrailheadJyoti Trailhead
Hi Mani

Thanks for your great answer - but I am still getting error:



BMCServiceDesk:Too many SOQL queries: 101
Error is in expression '{!saveList}' in component <apex:commandButton> in page nhm_test_tickets: Class.BMCServiceDesk.DriverAccessNOSharing.selectObjects: line 133, column 1
Class.BMCServiceDesk.DriverAccess.selectObjects: line 29, column 1
Class.BMCServiceDesk.STCriteriaEvaluator.getRelatedSTs: line 370, column 1
Class.BMCServiceDesk.STCriteriaEvaluator.getSTEvaluationResult: line 20, column 1
Class.BMCServiceDesk.IncidentTriggerHandler.STEvalForIncident: line 568, column 1
Class.BMCServiceDesk.IncidentTriggerHandler.ExecuteTrigger: line 80, column 1
Trigger.BMCServiceDesk.IncidentTrigger: line 2, column 1

Can we do any code optimazation here - I need to create tickes for all options for which we select value as YES
Magesh Mani YadavMagesh Mani Yadav
hi Jyothi,
The error is not in your saveList method rather its in the trigger IncidentTrigger may be there are more soql queries in the trigger invocation.
You need to check the trigger
but we can even change the saveList and CreateTicket method as follows: to return a list and then we make single DML
 
public PageReference saveList()
  {
    List<BMCServiceDesk__Incident__c> listOfIncident = new List<BMCServiceDesk__Incident__c>();
        system.debug('CCMS value:  '+ ccms);
        if(ccms == 'Yes'){
        string Category = 'CCMS';
        string ReqDef = 'CCMS Access';
        listOfIncident.add(CreateTicket(Category, ReqDef));
        }
        if(jira == 'Yes'){
        string Category = 'JIRA';
        string ReqDef = 'JIRA Account';
        listOfIncident(CreateTicket(Category, ReqDef));
        }
        if(!listOfIncident.isEmpty()){
          insert listOfIncident;
        }

   return null;
  }
     
public  BMCServiceDesk__Incident__c CreateTicket(String Category, String ReqDef)
{
    BMCServiceDesk__Incident__c myinci1=new BMCServiceDesk__Incident__c(); 
    myinci1.BMCServiceDesk__incidentDescription__c='Test ticket';
    User u=[select id,login__c from User where login__c = 'jyotiv' limit 1]; //If you are assigning single record then use limit 1 otherwise use List<User>
    if(u!=null){ // Always check if the instance u is null or not before using it
      myinci1.BMCServiceDesk__FKClient__c= u.id;
    }        
    BMCServiceDesk__Category__c cat=[select id,name from BMCServiceDesk__Category__c where name =: Category limit 1]; //If you are assigning single record then use limit 1
    if(cat != null){
    myinci1.BMCServiceDesk__FKCategory__c=cat.id;
    }
    myinci1.BMCServiceDesk__contactType__c = NULL;
    myinci1.BMCServiceDesk__queueName__c=NULL;
    myinci1.BMCRF_AutoAssign__c = true;
    myinci1.BMCRF_Update_Note__c ='test';
    myinci1.BMCServiceDesk__queueName__c=NULL;
    BMCServiceDesk__SRM_RequestDefinition__c ref=[select id,name from BMCServiceDesk__SRM_RequestDefinition__c where name =: ReqDef limit 1];    //If you are assigning single record then use limit 1            
    if(ref != null){
      myinci1.BMCServiceDesk__FKRequestDefinition__c=ref.id;       
    }  
    return myinci1;  
 }


 
This was selected as the best answer
Jyoti TrailheadJyoti Trailhead
Thanks a lot Mani – it worked ☺. Thanks for your great help.
Magesh Mani YadavMagesh Mani Yadav
Hi Jyoti,
good to know that your problem is solved.Can you also mark it solved.