Selecting picklist option on Visual force page upon load - salesforce

I have a visualforce page that has a picklist called Topic and sometimes I will need to select one of the picklist options upon page load (meaning the Topic will be passed on from another page and will need to be selected upon loading the page for the first time). I'm not sure how to do this? I'm posting part of the Visualforce page that handles topic selection and the Controller code that below. Any help would be appreciated.Thanks.
Visualforce page:
<!---------------------------------- Select Topic ----------------------------------------------------->
<apex:pageblockSection title="Select the Topic" >
<apex:selectList value="{!topic}" size="1">
<apex:outputlabel for="Topic" value="Pick a Topic :" ></apex:outputlabel>
<apex:selectOptions id="topic" value="{!Topics}"/>
<apex:actionSupport action="{!populateParameters}" reRender="parametersSection,querySection" event="onchange"/>
</apex:selectList>
</apex:pageblockSection>
<!---------------------------------- End of Select Topic ---------------------------->
<!---------------------------------- Parameters for Topic ----------------------------------------------------->
<apex:pageblockSection id="parametersSection" title="Input Parameters">
<apex:repeat value="{!topicpParamWrapperList}" var="params">
<apex:outputPanel >
<apex:outputlabel value="{!params.parameter.Name}" ></apex:outputlabel>
<apex:inputfield value="{!params.parameter.inputValue__c}" rendered="{!params.renderAsText}">
<apex:actionsupport action="{!placeValuesInQuery}" reRender="querySection,splunUrlLink" event="onchange"/>
</apex:inputfield>
<apex:inputfield value="{!params.parameter.DateTimeValueHolder__c}" rendered="{!params.renderAsDate}">
<apex:actionsupport action="{!placeValuesInQuery}" reRender="querySection,splunUrlLink" event="onchange"/>
</apex:inputfield>
</apex:outputPanel>
</apex:repeat>
</apex:pageblockSection>
<!---------------------------------- End of Parameters for Topic ----------------------------------------------------->
Apex Controller
public List < topicpParamWrapper > topicpParamWrapperList {
get;
set;
} {
topicpParamWrapperList = new List < topicpParamWrapper >();
}
public void populateParameters()
{
if(!topicpParamWrapperList.isEmpty())
{
topicpParamWrapperList.clear();
}
if(topic!='' && topic!=Null)
{
for(Query_Parameter__c qParam :[select id, Parameters__r.Variable_Name__c, Parameters__r.Type__c,Parameters__r.Name from Query_Parameter__c where Topics__c=:topic])
{
Parameters__c param = new Parameters__c();
param.Name =qParam.Parameters__r.Name ;
param.type__c = qParam.Parameters__r.type__c;
param.Variable_Name__c=qParam.Parameters__r.Variable_Name__c;
topicpParamWrapperList.add(new topicpParamWrapper(param));
}
getQueryToRun();
}
}
public void getqueryToRun(){
if(mapTopics.containsKey(topic))
{
this.queryToRun =mapTopics.get(topic).query__c;
this.queryMain=mapTopics.get(topic).query__c;
}
}
public List < topicpParamWrapper > paramList {
get;
set;
} {
paramList = new List <topicpParamWrapper>();
}

All you really have to do is to set the topic to some initial value in the constructor (the special function that has name identical to class' name). You set it to some value and then it'll be rendered properly in visualforce (assuming same value is one of the selectable options!).
You have omitted the constructor or <apex:page> tag so we don't know how you're navigating to that page. But probably easiest for you would be to pass the topic in the URL. So if you access the page like that:
/apex/MyPage?topic=Something, something
then in the constructor you could do this:
topic = ApexPages.currentPage().getParameters().get('topic');
(the name of the URL parameter doesn't have to be same as the variable name but it makes sense to have them at least similar)
You can read more about getParameters()
If there is risk that your topic will contain &, spaces etc you probably should URLENCODE it when building the link.

Related

How to show error message on Visualforce Page when create records based on Condition

Project Requirement:-create new record when candidate's PAN is not blacklisted, if it is blacklisted then show warning message on visualforce page and update new phone number in blacklisted candidate object.
Pre exquisite:- i have two object 1.Sudent_matster__c : Fields:- Name, Phone__c, PAN__c, email__c etc 2.Black_Listed_Candidate__c Fields:- Name, Phone__c, PAN__c etc
QUESTION When i enter blacklisted field PAN- FHSJF10387 then this code runs perfectly fine, but i enter another blacklisted field PAN- DKFIT8888S,CNYY78912Q, RYHJI997Q.etc. then code wont work. why? i think old value should be cleared from memory?
Visual force page IMAGE [This is visualforce page ][1] [1]: https://i.stack.imgur.com/aLzjo.png
VFP CODE
<apex:page Controller="Class_Practice">
<apex:form id="frm" html-autocomplete="off" style="border-style:solid;border-width:2px;border-color:black;background-color:lightyellow">
<H1 style="text-align:center;font-size:30px;">PAN DATA CHECK</H1>
<apex:pageBlock >
<apex:pageMessages></apex:pageMessages>
<apex:pageBlockSection title="My Content Section" columns="2">
<apex:inputField value="{!newStudent.Name}"/>
<apex:inputField value="{!newStudent.First_Name__c}"/>
<apex:inputField value="{!newStudent.Last_Name__c}"/>
<apex:inputField value="{!newStudent.Email__c}"/>
<apex:inputField value="{!newStudent.Phone__c}"/>
<apex:inputField value="{!newStudent.PAN__c}"/>
<Apex:commandButton value="Save" Action="{!MySave}"/>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:form>
</apex:page>
APEX CLASS Code
public class Class_Practice
{
public Student_Master__c newStudent {get;set;}
Map<Id, string> IdAndBlackListPhoneMap = New Map<Id, string>();
public static list< Black_Listed_Candidate__c> blackList =[SELECT Name,PAN__c,Phone__c FROM Black_Listed_Candidate__c];
public Class_Practice()
{
newStudent = new Student_Master__c();
}
public pageReference MySave()
{
for(Black_Listed_Candidate__c bp:blacklist)
{
if (bp.PAN__c==newStudent.PAN__c )
{
ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, 'PAN is Black Listed'));
bp.Phone__c = newStudent.Phone__c;
IdAndBlackListPhoneMap.put(bp.Id, bp.Phone__c);
Update blacklist;
return null;
}
else
{
insert newStudent;
PageReference pg = new PageReference('/' + newStudent.Id);
pg.setRedirect(true);
return pg;
}
}
return null;
}
}
THANK YOU

radio button value not visible in vfpage

im tring to display records in question__c object along with fields in object as radio button. but im not able to get radio button value. getting only radio button without name.
im new to salesforce.if this is very basic question pls excuse me.
<apex:page standardController="question__c" extensions="GetQuestionList" >
<apex:form >
<apex:repeat value="{!que}" var="a">
<apex:pageBlock >
<apex:pageBlockSection columns="1">
{!a.Quiz_question__c} {!a.id} <br/>
<apex:selectRadio value="{!selectedAns}" >
<apex:selectOption itemValue="{!opt3}" itemLabel="{!a.option_3__c}"/>
<apex:selectOption itemValue="{!opt4}" itemLabel="{!a.option_4__c}"/>
</apex:selectRadio>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:repeat>
</apex:form>
</apex:page>
____mycontroller is
public class GetQuestionList {
question__c q1;
public List<question__c> que{set;}
public List<question__c> getque(){
List<question__C> que= new List<question__c>();
for(question__C q:[select Quiz_question__c,id from question__c]){
que.add(q);
system.debug(que);
}
system.debug(que);
return que;
}
public String selectedAns{get;set;}
public String opt2{get;set;}
public String opt4{get;set;}
public String opt3{get;set;}
public GetQuestionList(ApexPages.StandardController controller) {
}
}
The reason you have no label is because the value field on the <apex:selectRadio> is a String variable, rather than a field. If you want to add a label, there is a label attribute on that visualforce component you can use.
If you want the label to be dynamically shown based on an actual field's label in the data model, you need to set the value field to that field. An example of that would be if you had an Answer__c field on the Question__c object.
<apex:selectRadio value="{!a.Question__r.Answer__c}">
<apex:selectOption itemValue="{!opt3}" itemLabel="{!a.option_3__c}"/>
<apex:selectOption itemValue="{!opt4}" itemLabel="{!a.option_4__c}"/>
</apex:selectRadio>
That value field would use the label assigned to that field in the object model by default.

How can I pass variables state from one page to another, if both pages use the same controller, but the second page uses a controller extension?

I have a page that needs to pass data to another page. They both use the same controller, but the second page has its own controller extension. When the first page passes data to the second page, the second page will recall the constructor of the controller. If I remove the controller extension from the second page, data can be passed and the constructor is only called on the first page, not on the second.
How can I pass state from one page to another, if both pages use the same controller, but one page uses a controller extension?
For example, in the following code, if I enter data in the outputText for the variable t and then go to the next page, the state won't save, the constructor will be recalled, and the value for t on page 2 will be "construct". If the testExtension is removed from the second page, then everything works fine.
apex/test1
VisualForce
<apex:page controller="test">
<apex:outputText value="{!t}" />
<apex:commandButton action="/test2" value="Go to test2" />
</apex:page>
Controller
class test {
public String t {get; set;}
public test() {
t = 'construct';
}
}
apex/test2 VisaulForce
<apex:page controller="test" extension="testExtension">
<apex:outputLabel value="{!t}" />
</apex:page>
controller extension:
class testExtension {
public testExtension(test controller) {
}
}
public with sharing class Fake01_Controller {
public String fake02String {get;set;}
public PageReference fake03Submit() {
PageReference prToNavigate = null;
prToNavigate = Page.Fake_02;
return prToNavigate;
}
}
Fake 01 page:
<apex:form >
<apex:inputText value="{!fake02String}" />
<apex:commandButton action="{!fake03Submit}"
value="Navigate to Fake 02"
/>
</apex:form>
Fake 02 page:
<apex:form >
<apex:outputText value="{!fake02String}" />
</apex:form>
Please, read PageReference about setRedirect method:
If set to false, the redirect is a server-side forward that preserves
the view state if and only if the target page uses the same controller
and contains the proper subset of extensions used by the source page.
class test {
public String t {get; set;}
public test() {
t = 'construct';
}
public PageReference test2() {
return page.test2;
}
}
<apex:page controller="test">
<apex:outputText value="{!t}" />
<apex:commandButton action="{!test2)" value="Go to test2" />
</apex:page>

Visualforce Repeaters

Hi I am getting a headache with the visualforce repeater control:
<apex:repeat value="{!productDetails}" var="o">
<apex:pageBlockSectionItem >
<apex:outputText style="label" value="Name:"></apex:outputText>
<apex:outputLabel value="{!o.name}" />
</apex:pageBlockSectionItem>
<apex:pageBlockSectionItem >
<apex:outputText value="{!o.ProductCode}"/>
<apex:outputText value="{!o.Family}" />
</apex:pageblockSection>
<apex:pageBlockSection>
<apex:outputText style="label" value="Quantity:"> </apex:outputText>
<apex:inputText value="{!theList}"/>
</apex:pageblockSection>
</apex:repeat>
What I am trying to do, is for each record in the product details list, generate a text box. This text box is bound to another field (theList.quantity). But I am finding that when I change the value in the last text box it sets the quantity in all the text boxes(obviously as they are bound to the same field).
So my question is whats the best way to have each textbox that is generated in the repeater have its own parameter value?
Or am I using the repeater in the wrong way?
EDIT(clarification):
For each product detail record(What I am iterating through) I want to have a textbox where a user can enter the quantity of products. The value of this textbox is unrelated to the product detail records.
My question is how can I generate unique parameters for each iteration of the textbox? Hopefully that makes sense.
Cheers
You need to use the var in the <apex:repeat> for each element in the productDetails list. For example:
<apex:repeat value="{!productDetails}" var="productDetail">
<apex:pageBlockSection>
<apex:outputText style="label" value="Quantity:"> </apex:outputText>
<apex:inputText value="{!productDetail.quantity}"/>
</apex:pageblockSection>
</apex:repeat>
That will set the the quantity property of each productDetail.
If you're really iterating over a parent of the productDetail in this example, then you'll need to change your controller to create a parent for each and then iterate over that. I'll write the example code as if you're iterating over a list of potential orders.
In your controller, you'll need to create an order for each of the products. I'm not sure if the parent is an SObject or a custom class, but I'll write the example as if it was a custom class.
public class Order {
public Order(ProductDetail productDetail) {
this.productDetail = productDetail;
}
public ProductDetail productDetail;
public Integer quantity;
}
// I assume you've already implemented this getter or are setting it some other way.
public ProductDetail[] productDetails {
get {
if (productDetails == null) {
productDetails = ...;
}
return productDetails;
}
set;
}
public Order[] orders {
get {
if (orders == null) {
orders = new Order[]{};
for (ProductDetail productDetail: productDetails) {
orders.add(new Order(productDetail);
}
}
return orders;
}
set;
}
Now, in your VisualForce page you can iterate over the Order list and have the user set the quantity.
<apex:repeat value="{!orders}" var="order">
...
<apex:outputtext style="label" value="Name:"></apex:outputText>
<apex:outputLabel value="{!order.productDetail.name}" />
...
<apex:inputText value="{!order.quantity}"/>
....
</apex:repeat>
Back in your controller, only save the orders that have a quantity of more than zero (or whatever other criteria you have in mind).
The easiest way to do this is to add a custom field to the object you're repeating through. However, I understand there are cases where that's not desirable.
You probably could use a custom Apex object (not custom Salesforce object) for this. You can do this by adding a nested class within your Controller or Extension.
For example:
public class MyClass
{
public class MyProductDetail
{
public string Name {get;set;}
public string Family {get;set;}
public string ProductCode {get;set;}
public integer Quantity {get;set;} // theList
}
public List<MyProductDetail> MyProductDetails {get;set;}
}
You would need to loop through all of your Product detail records (returned from SOQL), and add them to the MyProductDetails list. Once you have that, though, you can use a Visualforce Repeater to display each of them them and save the inputted quantity data for each record.
<apex:repeat value="{!MyProductDetails}" var="o">
<apex:pageBlockSectionItem >
<apex:outputText style="label" value="Name:"></apex:outputText>
<apex:outputLabel value="{!o.Name}" />
</apex:pageBlockSectionItem>
<apex:pageBlockSectionItem >
<apex:outputText value="{!o.ProductCode}"/>
<apex:outputText value="{!o.Family}" />
</apex:pageblockSection>
<apex:pageBlockSection>
<apex:outputText style="label" value="Quantity:"> </apex:outputText>
<apex:inputText value="{!o.Quantity}"/>
</apex:pageblockSection>
</apex:repeat>
Hope that helps!

inputField databinding issue with VisualForce

I'm having strange results with a Visualforce page (yes, Salesforce.com is icky, I know). My issue is that I am trying to use the inputField to bind data on a custom sObject, but in my custom controller, it is not recognizing the user input data.
Here is the code snippet from the page:
<apex:pageBlockSection title="Enter New Fee" rendered="{!isRenderedFees}" >
<apex:inputField value="{!workingFee.Fee_Type__c}" required="True"/>
<apex:inputField value="{!workingFee.Fee__c}" required="True"/>
<apex:pageBlockSectionItem >
<apex:CommandButton value="Save Fee" action="{!saveFee}" immediate="true" />
<apex:CommandButton value="Cancel" action="{!cancelFee}" immediate="true" />
</apex:pageBlockSectionItem>
</apex:pageBlockSection>
and here is the code from the controller:
public Fee__c workingFee {get; set;}
....
public PageReference saveFee(){
this.workingFee.Trade_Group__c = tradeGroup.id;
try{
System.debug('~~~~#~~#~~workingFee: '+workingFee.Fee_Type__c +'='+workingFee.Fee__c);
upsert workingFee;
}catch (System.Dmlexception e){
ApexPages.addMessages(e);
return null;
}
System.debug('~~~~#~~#~~workingFee: '+workingFee.Fee_Type__c +'='+workingFee.Fee__c);
//savedFees.add(workingFee.clone());
//re-render the page
this.isRenderedFees = False;
return null;
}
I've made sure the workingFee property is not null. Whenever I hit the "Save Fee" button after entering the values, it reloads the page and gives me the message "Error: Required fields are missing: [Fee__c]" (note, Fee__c here is a currency field -- it's not that it expects this to be an sObject, is it?)
The debug statement in the saveFee() method shows that workingFee's important fields are null, when I would expect them to have been assigned the values input by the user.
I have had a whole heap of issues binding controls to a property exposed with the simple { get; set; } notation... The rest of your code will see the properties, but for some bizarre reason, your View won't (always) bind...
Try writing explicit get/set methods, like
private workingFee;
public Fee__c getWorkingFee() {
return workingFee;
}
public void setWorkingFee(Fee__c value) {
workingFee = value;
}
There is no logical reason why this should work any different to
public Fee__c workingFee { get; set; }
but in my experience, it sometimes does...
what did you say about it being icky? ;)
Immediate=false/true has bitten me quite a few times too. Good catch Ian.

Resources