parameter value null is redirected to url - salesforce

Hi i am writing a simple controller class with code
public class OMTime1 {
public String xx { get; set; }
public PageReference continue1(){
String url='/apex/OneMoreTime?id='+ApexPages.currentPage().getParameters().get('id')+'&process='+xx;
PageReference page=new PageReference(url);
page.setRedirect(true);
return page;
}
}
and a simple VF Page with code
<apex:page controller="OMTime1" >
<!-- Begin Default Content REMOVE THIS -->
<!-- End Default Content REMOVE THIS -->
<apex:form >
<apex:selectList id="chooseColor" value="{!xx}" size="1">
<apex:selectOption itemValue="1" itemLabel="Terminate Resource"/>
<apex:selectOption itemValue="2"
itemLabel="Change Resource Position at Same Location"/>
<apex:selectOption itemValue="3" itemLabel="Change Resource Position with in same Location "/>
<apex:selectOption itemValue="4" itemLabel="Change Resource Position to a Different Location"/>
</apex:selectList>
</apex:form>
<apex:form >
<apex:commandButton value="Continue" action="{!continue1}" id="theButton"/>
</apex:form>
</apex:page>
my task s when user click on button name as Continue page will redirect to it a url /apex/OneMoreTime?id=record_id&process=some_value
some_value=1,2,3, or 4
but when i click on button it redirect to /apex/OneMoreTime?id=record_id&process=null
why this null is coming in parameter i want to redirect a value related to label of select option.please suggest way how to resolve it

Short answer: because <apex:commandButton> is broken ;)
Recently I've posted an explanation of 2 most common workarounds on Salesforce-dedicated stackexchange site: https://salesforce.stackexchange.com/questions/4937/command-button-vs-command-link
Use an <apex:commandLink styleClass="btn"> and it will work while looking almost the same as button.

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>

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 :)

Passing values to nested apex page for editing

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;
}
}

Adding rows in visualforce page in PageBlocktable dynamically

i am creating a dummy visualforce page whose code is
<apex:page controller="sampleCon">
<apex:form>
<apex:pageMessages id="message" />
<apex:pageBlock>
<apex:pageBlockTable value="{!wrap1}" var="item" id="hello">
<apex:column headerValue="String" value="{! item.x}"></apex:column>
<apex:column headerValue="Action"><apex:commandButton value="{!if(item.i == size-1,'Add','Delete')}" onclick="AddOrDelete({! item.i});" rerender="message"></apex:commandButton></apex:column>
</apex:pageBlockTable>
</apex:pageBlock>
<apex:actionRegion >
<apex:actionFunction name="AddOrDelete" action="{!addElement}" reRender="message,hello">
<apex:param value="" name="fieldName" />
</apex:actionFunction>
</apex:actionRegion>
</apex:form>
</apex:page>
and the controller code is
public class sampleCon {
public List<Wrapper> wrap1{get;set;}
public Integer size{get;set;}
public sampleCon(){
wrap1=new List<Wrapper>();
wrap1.add(new wrapper(0));
size=wrap1.size();
}
public PageReference addElement(){
size=wrap1.size();
System.debug('Hello ji'+Apexpages.currentPage().getParameters().get('fieldName')+'size is'+size);
Integer i=Integer.valueOf(ApexPages.currentPage().getParameters().get('fieldName'));
if(i == size-1)
{
wrap1.add(new wrapper(size));
size++;
}
return null;
}
public class wrapper{
public integer i{get;set;}
public String x{get;set;}
public wrapper(Integer p){
i=p;
}}
}
what i am trying is the last row button label should be Add and other button label should be Delete when i press add button a new row is added and the label should be changed acordingly for this i am adding row but it is not working as expected
initial page screenshot is
when i press add button screen look like this.
when i again press the Add button screen is looking like first screenshot.i was expecting that one more row will be added when i click add button in above screenshot.can any one please tell why only one row is showing when i click add button in second screen shot and how to correct it that when i again click on add button on above screen one more row will be added instead of showing initial screen.
I would suggest you take a list at below post:
http://boards.developerforce.com/t5/Visualforce-Development/Add-Row-functionality-in-VisualForce/m-p/358921
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