Passing values to nested apex page for editing - salesforce

I have an apex page with custom extension. I am using pageblocktable to show all available records. Table has modify button, on click of button I am showing another nested apex page in a javascript dialog. Question is how to pass pageblocktable row values to nested page for editing purpose?

You can share same controller for both pages.
Page1:
<apex:page standardcontroller="Object__c" extensions="MyController">
<apex:column headerValue="Action">
<apex:commandLink action="{!goToPage2}" value="Edit" >
<apex:param name="rowId" assignTo="{!rowId}" value="{!item.Component.Id}" />
</apex:commandLink>
</apex:column>
</apex:page>
Page2:
<apex:page standardcontroller="Object__c" extensions="MyController">
{!objForPage2.Name}
</apex:page>
Controller:
public with sharing class MyController{
public string rowId {get;set;}
Object__c objForPage2;
public Pagereference goToPage2(){
for(Object__c obj : objectList){
if(obj.Id==rowId){
objForPage2= obj;
}
}
return Page.Page2;
}
}

Related

Refresh Record Page with Apex via a Visualforce Page

I want to create a button that effectively does the same thing as "ctrl r"
After my end user finishes updating info in the visualforce page. I want to give them to option to click this button and it will refresh the entire record page which the visualforce is embedded onto.
I honestly have no idea how to do this, my coding experience is limited. The only thing i've been able to do is get the page to reload within the visualforce page and that's not very helpful.
This is what I tried:
Visualforce code:
<apex:page standardController="Request__c" extensions="ActionRelatedList">
<apex:form>
<apex:pageBlock>
<apex:commandButton value="Reload" action="{!redirect}"/>
</apex:pageBlock>
</apex:form>
</apex:page>
Redirect method:
public PageReference redirect() {
String requestURL ='URL';
PageReference newRequestURL = new PageReference(requestURL);
newRequestURL.setRedirect(true);
return newRequestURL;
}
But this doesn't refresh my entire page, it just opens it within the visualforce page.
You can do this using code below:
<apex:page standardController="Account" >
<apex:form >
<apex:pageBlock>
<apex:commandButton value="Reload" onclick="window.top.location='/{!Account.id}'; "/>
</apex:pageBlock>
</apex:form>
</apex:page>

In Visualforce, Does "rendered" reexecutes after "reRender" refreshes a section?

I am trying to hide the section 'searchCriteria' when 'Edit' link is clicked. The hiding is done in Controller 'editSearchResult()' method using 'pageLoad' variable.
The 'pageLoad' is used in 'searchCriteria' section with rendered attribute. But, the section is not refreshing with updated values from Controller when Edit link is clicked.
But if I remove 'rendered' attribute on the section 'searchCriteria', system refreshes the section with updated values from Controller. Can anyone explain how 'rendered' handles display?
Visualforce Code:
<apex:page controller="RerenderDemoController">
<apex:form id="thisForm">
<apex:outputPanel id="searchCriteria" rendered="{!pageLoad}">
<apex:pageBlock>
<apex:pageBlockSection>
This is Search Criteria Section. Page Load: {!pageLoad},
User: <apex:outputText value="{!userName}">
</apex:outputText>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:outputPanel>
<apex:outputPanel id="searchResults">
<apex:pageBlock>
<apex:pageBlockSection>
This is Search Results Section. {!accts}
</apex:pageBlockSection>
</apex:pageBlock>
</apex:outputPanel>
<apex:outputPanel id="EditResult">
<apex:pageBlock>
<apex:pageBlockSection>
This is Edit Result Section.
<apex:commandLink action="{!editSearchResult}" reRender="searchCriteria">Edit</apex:commandLink>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:outputPanel>
</apex:form>
</apex:page>
Apex Controller:
public class RerenderDemoController {
public List<Account> accts {get; set;}
public Boolean pageLoad {get; set;}
public String userName {get; set;}
public RerenderDemoController() {
pageLoad = true;
userName = 'My First Name';
accts = [select id, name from Account limit 10];
}
public void editSearchResult() {
pageLoad = false;
accts = [select id, name from Account limit 20];
userName = 'My Last Name';
}
}
What you can do is, wrap your outputPanel with another outputPanel
<apex:outputPanel id="searchCriteria">
<apex:outputPanel rendered="{!pageLoad}">
<apex:pageBlock>
<apex:pageBlockSection>
This is Search Criteria Section. Page Load: {!pageLoad},
User: <apex:outputText value="{!userName}">
</apex:outputText>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:outputPanel>
</apex:outputPanel>
Hope, this will solve your issue.
You can use JQuery to Hide the content just use html class and use [jquery]:https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js
and use $('.classname').hide();
hope it helps you.

Visualforce Apex:pageMessages validation rule Issue

I have custom validation rules defined on my custom employee__c object in salesforce. I use a standard controller visualforce page with custom extension to show a UI to the user for data entry. My challenge is to show the validation rule error to the user in an easy to read manner. Here is the part of the code that I have.
Visualforce
<apex:page standardController="Employee__c" extensions="EmployeeExtension" sidebar="false">
<apex:sectionHeader ...
<apex:form id=fr>
<apex:pageMessages id="errMsg"/>
<apex:pageBlock title="Employee Edit" mode="edit" >
<apex:pageBlockButtons >
<apex:commandButton action="{!mySave}" value="Save" reRender="errMsg"/>
<apex:commandButton action="{!cancel}" value="Cancel"/>
....
</apex:form>
</apex:page>
Apex Controller
public class EmployeeExtension {
....
public PageReference mySave(){
....
try{
upsert empList;
} catch (DMLException ex) {
ApexPages.addMessages(ex);
}
}
}
This shows the errors at the page top which is the way I want, but it shows twice. Here is how it will display the error at page top.
error_message_from_custom_validation_comes_here
TriggerEmployee: Exception on row 0; first error: FIELD_CUSTOM_VALIDATION_EXCEPTION, error_message_from_custom_validation_comes_here
In my entire controller I do not have any other DML operations nor do I use ApexPages.addmessage any where else. The strange thing is if I remove
ApexPages.addMessages(ex);
while keeping the try catch block as is, then I see only
error_message_from_custom_validation_comes_here
I wonder why it shows the page messages in vf page even when I am not sending anything from controller. I appreciate all your responses, but I would like to see solutions not involving javascript or jquery.
You can use this:
ApexPages.addMessage(
new ApexPages.Message(
ApexPages.severity.ERROR,
ex.getMessage()
)
);
Alternatively you could also imitate the standardController:
public class EmployeeExtension {
....
public PageReference mySave(){
/* do your thing */
return this.standardController.save();
}
}
This way you return the page reference of the save which is the standard behaviour (if you want to imitate this) and if there is a validation error it will show up in the tag and only the nice formatted message will show up.
You don't need a reRender on the save:
<apex:page standardController="Employee__c" extensions="EmployeeExtension" sidebar="false">
<apex:sectionHeader ...
<apex:form id=fr>
<apex:pageMessages/>
<apex:pageBlock title="Employee Edit" mode="edit" >
<apex:pageBlockButtons >
<apex:commandButton action="{!mySave}" value="Save"/>
<apex:commandButton action="{!cancel}" value="Cancel"/>
....
</apex:form>
</apex:page>
Salesforce/Visualforce will do the rest for you :)

reRender and rendered attribute in visualforce

i write a visualforce page with source code
<apex:page controller="MyController1">
<apex:form>
<apex:pageBlock >
<apex:pageBlockSection id="search">
<apex:commandLink action="{!commandLinkAction}" value="Advance Search" reRender="thePanel" id="theCommandLink"/>
<apex:outputPanel id="thePanelWrapper">
<apex:outputPanel id="thePanel" rendered="{! rend}" layout="block">My div</apex:outputPanel>
</apex:outputPanel>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:form>
</apex:page>
and the MyController1 class is
public class MyController1{
public Boolean rend{get;set;}
public PageReference commandLinkAction(){
rend=true;
return null;
}
}
when i click on Advanced Search link nothing happens but i was expecting outputPanel with id "thePanel" should render.why it is not rendering please someone explain??
In moment that you click on the link the panel not on the page, SF not rendered it.
As #Shimshon said, when the HTML code is generated from Visualforce, the Visualforce components marked as rendered="false" are not displayed in the resulting HTML document.
In this case:
<apex:outputPanel id="thePanel" rendered="{! rend}" layout="block">
if you are going to rerender this panel, then you must ensure that the component will be displayed in the HTML code so the rerender action can find it. Since {! rend} is initially set to false in the controller's constructor "thePanel" is never rendered in the page, thus you try to rerender a component that does not exist.
#theGreatDanton 's solution will work because <apex:outputPanel id="thePanelWrapper"> is the container panel that is always rendered:
<apex:commandLink action="{!commandLinkAction}" value="Advance Search" reRender="thePanelWrapper" id="theCommandLink"/>
and if this panel is pointed by rerender attribute, then "thePanelWrapper" and their child nodes ("thePanel") will be updated.
try the below code.
<apex:page controller="MyController1">
<apex:form>
<apex:pageBlock >
<apex:pageBlockSection id="search">
<apex:commandLink action="{!commandLinkAction}" value="Advance Search" reRender="thePanelWrapper" id="theCommandLink"/>
<apex:outputPanel id="thePanelWrapper">
<apex:outputPanel id="thePanel" rendered="{! rend}" layout="block">My div</apex:outputPanel>
</apex:outputPanel>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:form>
</apex:page>
public class MyController1{
public Boolean rend{get;set;}
//setting the boolean to false in the constructor
public MyController1(){
rend = false;
}
public void commandLinkAction(){
rend=true;
// return null;
}
}
Hope this helps!!

display records based on selection of name from picklist in salesforce

I am trying to get my hands on learning Visual force.
I have an object inv_c which holds invoice records and another object item_c
I have in my VF page a picklist with the object names.
If user selects inv_c then all records of inv_c are displayed if user selects item__c all records of item are displayed
Is there any way where the list would be displayed on the completion of the selection or do we have to have button to get it.
how can i achieve this in VF? any small code snippet would be wonderful
Thanks
You can do this using a JavaScript onchange event with the help of the ActionSupport Visualforce Component. Here's an example.
<!-- Page: -->
<apex:page controller="exampleCon">
<apex:form>
<apex:outputpanel id="counter">
<apex:outputText value="Click Me!: {!count}"/>
<apex:actionSupport event="onclick"
action="{!incrementCounter}"
rerender="counter" status="counterStatus"/>
</apex:outputpanel>
<apex:actionStatus id="counterStatus"
startText=" (incrementing...)"
stopText=" (done)"/>
</apex:form>
</apex:page>
/*** Controller: ***/
public class exampleCon {
Integer count = 0;
public PageReference incrementCounter() {
count++;
return null;
}
public Integer getCount() {
return count;
}
}
In your case the actionSupport component would be a child of your selectRadio component i.e.
<apex:selectRadio value="{!selection}">
<apex:selectOptions value="{!items}"/>
<apex:actionSupport event="onchange" .... />
</apex:selectRadio>

Resources