Display fields based on Checkbox actions - salesforce

I have a VF page where I am using checkbox fiels, I want to display three other fiels if the checkbox is selected.
How can I go about doing that ?
Thanks !!

Use the rendered attribute on <apex:inputfield> or <apex:inputtext>. By setting this attribute, you can control whether or not a component appears on a Visualforce Page.
Include all fields that may or may not be shown on the Visualforce Page, but set rendered="{!Object.YourCheckboxField}" on the fields that you want to show/hide based on the checkbox field. Optionally, you can use a Visualforce function like IF or NOT.
For example, if you wanted to show or hide the Email field on Contacts based on whether or not the HasOptedOutOfEmail field is true or false, it would look like this.
<apex:page standardController="Contact">
<apex:sectionHeader title="Custom Contact Visualforce Page" />
<apex:form>
<apex:pageblock>
<apex:pageblocksection>
<apex:inputfield value="{!Contact.FirstName}" />
<apex:inputfield value="{!Contact.LastName}" />
<apex:inputfield value="{!Contact.Phone}" />
<apex:inputfield value="{!Contact.Email}" rendered="{!NOT(Contact.HasOptedOutOfEmail)}" />
</apex:pageblocksection>
</apex:pageblock>
</apex:form>
</apex:page>

Related

Newbie at visualforce

I am newbie at visualforce and i have a few questions. I was able to create a basic visualforce page:
<apex:page standardController="Campaign">
<apex:sectionHeader title="Campaign" subtitle="Campaign Edit"/>
<apex:form id="theForm">
<apex:pageBlock title="Campaign" mode="edit">
<apex:pageBlockButtons >
<apex:commandButton action="{!save}" value="Save"/>
<apex:commandButton action="{!cancel}" value="Cancel"/>
<apex:commandButton value="Save & New" action="{!'save & new'}"/>
</apex:pageBlockButtons>
<apex:pageBlockSection title="My Content Section" columns="2">
<apex:inputText value="{!Campaign.name}" id="name"/>
<apex:inputfield value="{!Campaign.Campaign_Type__c}">
<apex:actionSupport event="onchange" reRender="theForm" />
</apex:inputField>
<apex:inputtext value="{!Campaign.PPC__c}" rendered="{!Campaign.Campaign_Type__c == 'Campaign Type 1'}" />
</apex:pageBlockSection>
</apex:pageBlock>
</apex:form>
https://ibb.co/iWSVVS
The field "Campaign name" is a required field how can i add the red bar next to the field?
How can I "combine" several fields together and insert them as a value to the field Campaign name? for example i have a dropdown field "Campaign_Type__c=Type 1" and i also have a text box field "Campaign_textbox1__c=alex" I want to combine them as "Type 1 alex" and set this as a value for the field name "Campaign name".
Thank you
For the required just set the attribute to the tag:
<apex:inputText value="{!yourVaue}" id="yourid" required="true" />
Can you not do a formula field for the Campaign name?
One that concatenates the fields together. So create a formula field called Campaign name, then use my formula below:
Campaign Name = Campaign_Type__c & " " & Campaign_textbox1__c
The reason for the space is that otherwise it'll stick it together like "Type 1Alex"
With your other question, i'd just use HTML formatting to get this. Maybe use a pipe? | - one that's bold and red - but i'm lazy
Add the attribute required="true" to make it required i.e. red bar next to the field.
Therefore, you can achieve this by
<apex:inputText value="{!Campaign.name}" id="name" required="true"/>

Custom Field Labels - Field Sets VisualForce

I have a field set on a Visual Force Page. I want to customize the field labels displayed to the user.
Currently, my custom contact field is birthdate, but it'd be better Date of Birth.
Here is the code:
<apex:pageBlockSection title="Candidate Information">
<apex:repeat value="{!$ObjectType.Contact.FieldSets.contactForm}"
var="c">
<apex:inputField value="{!Contact[c]}" />
</apex:repeat>
</apex:pageBlockSection>
It outputs like:
Birthdate [ input field ]
Should be:
Date of Birth [ input field ]
Any idea how I can customize the output label?
Thanks.
You can do this:
<apex:inputField value="{!Contact[c]}" label="{!IF(c.label== 'Birthdate','Date of birth',c.label)}" />

How do I get a "New" button on my custom apex:pageBlockTable for Cases?

I have a custom Cases apex page where I use a apex:pageBlockTable to display my list of Cases. Here is the code for it:
<apex:page standardController="Case" recordSetVar="Case" sidebar="true" showHeader="true">
<apex:form >
<apex:pageBlock title="Cases">
<apex:outputLabel value="View:"/>
<apex:selectList value="{!filterId}" size="1">
<apex:actionSupport event="onchange" rerender="cases_table"/>
<apex:selectOptions value="{!listviewoptions}"/>
</apex:selectList>
<apex:pageBlock >
<apex:pageBlockButtons >
</apex:pageBlockButtons>
<apex:pageBlockTable value="{!case}" var="c" rows="50" id="cases_table" >
<apex:column >
<a target="_parent" href="{!URLFOR($Action.Case.View, c.id)}">{!c.CaseNumber}</a>
<apex:facet name="header">Case Number</apex:facet>
</apex:column>
<apex:column value="{!c.ContactId}" />
<apex:column value="{!c.Subject}" />
<apex:column value="{!c.Status}" />
<apex:column value="{!c.Priority}" />
</apex:pageBlockTable>
</apex:pageBlock>
</apex:pageBlock>
</apex:form>
<base target="_parent"/>
</apex:page>
I would like to get a button inside my apex:pageBlockButtons like in the default Cases page. When the user clicks the button, I would like it to take the user to the new Cases page. I tried this:
<apex:commandButton action="{!new}" value="New"/>
but that gave me an error. How do I make a button that will take me to the new Cases page?
Try this:
<apex:commandButton onclick="window.location.href='{!URLFOR($Action.Case.NewCase)}'" value="New" immediate="true" rerender="blank"/>
Important to note that the rerender tag is needed otherwise the page will do a postback. By using a dummy rerender tag the redirect will work. Immediate is there to avoid running any validation rules on click.
I have a custom Cases apex page where I use a apex:pageBlockTable to display my list of Cases. Here is the code for it(custom object):
<apex:commandButton action="{!URLFOR($Action.Your_Custom_Object__c.New)}" ...>

How do I customize the columns in a apex:listView?

I have a apex:listView for my Cases:
<apex:ListViews type="Case">
</apex:ListViews>
When I display this page, I get the columns Action, Case Number, Contact Name, Subject, Status, Priority, Date/Time Opened, and Case Owner Alias.
How would I customize which columns show up and what order the columns are in?
Thanks.
This is what I ended up doing:
<apex:page standardController="Case" recordSetVar="Case" sidebar="true" showHeader="true">
<apex:form >
<apex:pageBlock title="Cases">
<apex:outputLabel value="View:"/>
<apex:selectList value="{!filterId}" size="1">
<apex:actionSupport event="onchange" rerender="cases_table"/>
<apex:selectOptions value="{!listviewoptions}"/>
</apex:selectList>
<apex:pageBlock >
<apex:pageBlockButtons >
</apex:pageBlockButtons>
<apex:pageBlockTable value="{!case}" var="c" rows="50" id="cases_table" >
<apex:column >
<a target="_parent" href="{!URLFOR($Action.Case.View, c.id)}">{!c.CaseNumber}</a>
<apex:facet name="header">Case Number</apex:facet>
</apex:column>
<apex:column value="{!c.ContactId}" />
<apex:column value="{!c.Subject}" />
<apex:column value="{!c.Status}" />
<apex:column value="{!c.Priority}" />
</apex:pageBlockTable>
</apex:pageBlock>
</apex:pageBlock>
</apex:form>
<base target="_parent"/>
</apex:page>
Apex List Views, similar to standard list views, contain a picklist at the top followed by an Edit button and a Create New View button. You can modify the view by clicking on the Edit button. There is no way, that I am aware of, to modify the columns programmatically. If you need more control, you could use another tag, such as <apex:pageblocktable>, as JCD mentioned in the question comments.

display fields based on selection on a picklist selection

I have a VF page where i am using a apex:pageblocktable to display bunch of records.
One of the columns is a picklist and i need to display/not display fields according to selection on the picklist.
<apex:pageBlockTable value="{!showRecord}" var="item">
<apex:column headerValue="Delivery">
<apex:inputField value="{!item.delivery__c}"/>
</apex:column>
<apex:column headerValue="Roadway">
<apex:inputField value="{!item.road__c}"/>
</apex:column>
<apex:column headerValue="Rail">
<apex:inputField value="{!item.rail__c}"/>
</apex:column>
</apex:pageBlockTable>
in the above code delivery_c is picklist with values roadways and railways. if the user selects roadways then i need to display road--c and if user selects railways then i need to display rail_c
How can i go about doing that?
Thanks
One way to do this would be to use partial-page refreshes in Visualforce.
Put both fields in the same column and use the "rendered" attribute to dynamically show/hide the field using an if-statement. Then you set up an AJAX onchange event handler for the delivery__c field using the actionSupport tag. This will basically listen for that field to change then refresh the table on the page. Each time this is refreshed, your if statements will be re-evaluated and result in showing one of the two fields in that column.
I didn't get a chance to try this but I think it should work.
<apex:pageBlockTable id="mytable" value="{!showRecord}" var="item">
<apex:column headerValue="Delivery">
<apex:actionRegion>
<apex:inputField value="{!item.delivery__c}">
<apex:actionSupport event="onchange" reRender="mytable">
</apex:inputField>
</apex:actionRegion>
</apex:column>
<apex:column headerValue="Delivery Type">
<apex:inputField rendered="{!item.delivery__c='Road'}" value="{!item.road__c}"/>
<apex:inputField rendered="{!item.delivery__c='Rail'}" value="{!item.rail__c}"/>
</apex:column>
</apex:pageBlockTable>

Resources