create a child visual force page - salesforce

I have a custom Object called "Gift_c" off the Contact record. I would like to replace the standard "Gift_c" page with a visualforce page so that I can selectively hide certain fields.
All is farily straightforward.
<apex:page standardController="Gift__c" showHeader="true" >
<apex:form >
<apex:pageBlock title="" mode="Edit">
<apex:pageMessages />
<apex:pageBlockButtons >
<apex:commandButton value="Save" action="{!save}"/>
<apex:commandButton value="Cancel" action="{!cancel}"/>
</apex:pageBlockButtons>
<apex:pageBlockSection title="Basic Information" columns="1">
<apex:inputField value="{!Gift__c.Contact__c}"/>
<apex:inputField value="{!Gift__c.GiftAmount__c}"/>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:form>
The Challenge:
Normally when you are on a Contact and I click the "New Gift" button and the Contact_c field from the Gift_c object is populated to be the person I just came from. How can I ensure that still happens on the new Visual Force Page

Actually this functionality comes out of the box as long as you override the "New/Edit" button to point to new page.

There is what is known as the lkid hack. It is an undocumented feature that the values for the parent are passed as GET parameters from the New button on a related list. So you could extract the parameters that way, but it is widely accepted that this is a very flimsy solution.
Or there is a very neat solution posted here: hack-to-find-field-ids-allows-a-default-ui-new-page-to-be-pre-populated
This solution is screen scraping the standard edit page for all the lkid values in the html of the page. There are just as many reasons why this could one day just stop working, but it is probably more robust than reverse engineering the get parameters.

Related

Salesforce visualforce save button doesnt save

I am new to salesforce environment and I am trying to achieve simple thing. I want to create new Lead screen with custom lead questions and save that as a Lead.
I created apex form, overridden new lead button page, but when I press save button on that page, I dont get any error but it also doesnt save the Lead. Cancel button seems to be working.
Do I need to write custom code for save or apex:pageblockbuttons default action should work ?
I will just show short snippet of the code, as most of it is just setting the input fields
<apex:form id="mySection">
<apex:pageBlock title="New Lead: {!Lead.Name}">
<apex:pageBlockSection title="Lead information" columns="1" >
<apex:inputField value="{!Lead.FirstName}" tabOrderHint="1"/>
<apex:inputField value="{!Lead.LastName}" tabOrderHint="2"/>
<!-- Other fields are skipped -->
<apex:inputField value="{!Lead.Project_Value__c}" tabOrderHint="3"/>
</apex:pageBlockSection>
<apex:pageBlockButtons >
<apex:commandButton action="{!save}" value="Save"/>
<apex:commandButton action="{!cancel}" value="Cancel" immediate="true"/>
</apex:pageBlockButtons>
</apex:pageBlock>
</apex:form>
I found the solution, which was not related to the apex itself, but more to a one custom field which was mandatory but not on the page. I decided to leave the answer here, because while I was trying to find the solution to this issue, I discovered command which actually shows why the input form failed.
<apex:pageMessages />
Maybe that is a common knowledge to Visualforce, apex users, but for me it was a helpful tool and thats why I decided to leave it in an answer, if someone will have similar issue to remember to add that line.

4 columns on a custom visual force inline page

I have values that need to be setup like this:
Displayed like this, but with four columns:
I tried this way but this a mess... (The above picture is an example and would have USD $ 0.00 inline with the check total):
The code I'm using is:
<apex:page standardController="BookingEvent__c" extensions="EventInclusiveTotalPriceInlineExt" standardStylesheets="true" tabstyle="BookingEvent__c" docType="html-5.0">
<apex:form>
<apex:pageBlock mode="maindetail">
<apex:pageBlockSection columns="4">
<apex:outputfield value="{!be.ForecastRevenue1__c}"></apex:outputfield>
<apex:outputfield value="{!be.ForecastRevenue2__c}"></apex:outputfield>
<apex:outputfield value="{!be.ForecastRevenue3__c}"></apex:outputfield>
<apex:outputfield value="{!be.ForecastRevenue4__c}"></apex:outputfield>
<apex:outputfield value="{!be.ForecastRevenue5__c}"></apex:outputfield>
<apex:outputfield value="{!be.ForecastRevenue6__c}"></apex:outputfield>
<apex:outputfield value="{!be.ForecastRevenue7__c}"></apex:outputfield>
<apex:outputfield value="{!be.ForecastRevenue8__c}"></apex:outputfield>
<apex:outputfield value="{!be.ForecastRevenue9__c}"></apex:outputfield>
<apex:outputfield value="{!be.ForecastRevenue10__c}"></apex:outputfield>
<apex:outputfield value="{!be.ForecastRevenue11__c}"></apex:outputfield>
<apex:outputfield value="{!be.ForecastRevenue12__c}"></apex:outputfield>
<apex:outputfield value="{!be.ForecastRevenue13__c}"></apex:outputfield>
<apex:outputfield value="{!be.ForecastRevenue14__c}"></apex:outputfield>
<apex:outputfield value="{!be.ForecastRevenue15__c}"></apex:outputfield>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:form>
</apex:page>
I do not know how to style and it needs to look like it is displayed in the second pic.
Any help would be great, I'm new to VF and not great at styling yet and am having troubles finding exactly what i'm looking for.
For building pretty difficult layouts on Visuaforce page you can use power of HTML and CSS, pretty often it's more convenient than Visuaforce tags.
So, for implementation of table on your first screen I would prefer to use HTML table tag in cells of which apex:outputfield tags were included.

hide a field using controller extension in salesforce

I am new to salesforce development.
I have a requirement where not much customozation is needed but need to hide or show a couple of fields based on a value of other field.
Can I do this using standard controller and adding an extension controller? if yes How?
e.g I need to show the standard account page with list of accounts. when I click on a account , on the standard detail page there is a field " Rate". if the Rate is below 10% then I have to show "Revenue " field and if its >10% then I have to hide the previous field and show "Approx. Revenue" field.
Is this possible?
THnaks
I assume you're talking about Visualforce page and not the standard page layout? (It should be doable also with standard page layouts but would require some record type juggling, assigning of rec type depending on your conditions)...
It's possible to do what you want in pure Visualforce (just the standard controller, no extension). Do you want to display one account or multiple(in some table?)
Roughly speaking you should read about rendered attribute available on most visualforce tags. Here's a sample based on Opportunities but the principle is the same.
<apex:page standardController="Opportunity" recordSetVar="opps" readonly="true">
<apex:pageBlock>
<apex:pageBlockTable value="{!opps}" var="o">
<apex:column value="{!o.Name}" />
<apex:column value="{!o.Probability}" />
<apex:column headerValue="Amount or Stage">
<apex:outputField value="{!o.StageName}" rendered="{!o.Probability != 100}" />
<apex:outputField value="{!o.Amount}" rendered="{!o.Probability = 100}" />
</apex:column>
</apex:pageBlockTable>
</apex:pageBlock>
</apex:page>

Salesforce PageBlockTable Render an Item's Grandparent

I'm trying to display the grandparent of a custom Salesforce Object in a pageblocktable in a Visualforce page.
I can display the parent as expected ie:
<apex:column headerValue="Related Item Parent">
<apex:outputField id="ItemParent" value="{!item.Parent__c}" />
</apex:column>
works great and gives me a nice table with a 'Related Item Parent' column that is linked to the Item parent exactly as I wanted.
but when I try to go up a further level, the Visualforce Page editor tells me
Could not resolve the entity from value binding
'{!item.Parent_c.Parent_c}'. can only be used
with SObject fields.
Is there anyway that this can be made to work? I'm a bit stuck!!
Below is the complete listing for my pageBlockTable , which works correctly if the Related Item Grandparent column is removed.
<apex:pageBlockTable id="RelatedItems" value="{!contact.RelatedItems__r}" var="item">
<apex:column headerValue="Related Item Name">
<apex:outputField id="ItemName" value="{!item.Name}" />
</apex:column>
<apex:column headerValue="Related Item Parent">
<apex:outputField id="ItemParent" value="{!item.Parent__c}" />
</apex:column>
<apex:column headerValue="Related Item Grandparent">
<apex:outputField id="ItemGrandParent" value="{!item.Parent__c.Parent__c}" />
</apex:column>
</apex:pageBlockTable>
Really appreciate any help that anybody can provide.
Best regards
Pete
When referencing fields of relationship objects, you will want to use the __r suffix. For example, you could use item.Parent__c to refer to the Id of the parent record, but accessing any of the fields on that record would require something like item.Parent__r.Name.
In this instance, you're looking for {!item.Parent__r.Parent__c} (which will give you the Id of the grandparent), or even {!item.Parent__r.Parent__r.Name} (if you wish to access fields on the grandparent record).

Visualforce apex:inlineEditSuport on Lookup/Reference Field

I have an object with a lookup field, and want to use InlineEditSupport on that field within a VisualForce page.
However the apex:outputField tag renders the lookup field as a hyperlink, which overrides the inlineEditSupport logic.
For example
<apex:page standardController="Contact">
<apex:outputField value="{!Contact.LastName}">
<apex:inlineEditSupport event="ondblclick"/>
</apex:outputField>
<apex:outputField value="{!Contact.AccountId}">
<apex:inlineEditSupport event="ondblclick"/>
</apex:outputField>
</apex:page>
In this example, the LastName field would be rendered by the apex:outputField tag, and convert to an editable field when double clicked.
However the 'Account' field renders as a hyperlink to the Account record itself, which takes action before the click event can fire.
I can make this work by using an alternate javascript event - i.e. mouseover, however that's not particularly user friendly. I need to maintain a consistent user experience and use a double click action.
Is there any way to prevent the Account field from rendering as a link, or a way of inserting an 'edit' icon next to it somehow?
I ran into the same problem with inline editing in a pageBlockTable format. We felt this was not user friendly.
I used JavaScript (jQuery) to convert the link into plain-text and make this behave like other inlineEditable fields:
/// Overwrite default functionality for lookup columns (links to object page)
/// Change to text-only of name to prevent navigation away
$('TABLE[id$=checklistTable] TBODY TR TD A[id^=lookup]').each(function() {
var text = $(this).html();
$(this).parent().html(text);
});
This will convert all SalesForce lookup fields within the matching jQuery selector. It is also dependent upon the resulting output HTML creating anchor links that have ID's starting with "lookup" (meaning SalesForce could change/break this with future updates).
This might not be the simplest way but what I've done before is to create a select list of the names of the records in the lookup. I think this is nicer for the user anyway than a salesforce lookup box (unless there are hundreds, I suppose). What you would need to do is create a list of select options in the controller, and assign them to some variable like theList, and then reference {!theList} where you want to put it (roughly; see documentation). The select options would need to have the record name as label and the record id as value. Then you can assign the Id directly to the lookup field.
Did you try with salesforce doc?
<apex:page standardController="Contact">
<apex:form >
<apex:pageBlock mode="inlineEdit">
<apex:pageBlockButtons >
<apex:commandButton action="{!edit}" id="editButton" value="Edit"/>
<apex:commandButton action="{!save}" id="saveButton" value="Save"/>
<apex:commandButton onclick="resetInlineEdit()" id="cancelButton" value="Cancel"/>
</apex:pageBlockButtons>
<apex:pageBlockSection >
<apex:outputField value="{!contact.lastname}">
<apex:inlineEditSupport showOnEdit="saveButton, cancelButton" hideOnEdit="editButton" event="ondblclick" changedStyleClass="myBoldClass" resetFunction="resetInlineEdit"/>
</apex:outputField>
<apex:outputField value="{!contact.accountId}"/>
<apex:outputField value="{!contact.phone}"/>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:form>

Resources