You need to sign in to do that
Don't have an account?
![Jyoti Trailhead Jyoti Trailhead](https://dfc-org-production.my.site.com/img/userprofile/default_profile_45_v2.png)
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;
}
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;
}
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
All Answers
the below link has an example for the radio button. may be this helps you.
Try this code-
Thanks
Rupal Kumar
http://mirketa.com
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;
}
}
Here is the Updated class and vf page. i have included comments for your understanding
I hope the issue is resolved.
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
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
good to know that your problem is solved.Can you also mark it solved.