You need to sign in to do that
Don't have an account?
Issues passing value from Visualforce to Apex
Hello all,
I've got three objects, Lab Projects, Employees, and Lab Project Hours (junction between the previous two). New Lab Project Hours records are always created from a Lab Project record. Someone generously wrote the following code for me, which overrides the New button and automatically fills in the Employee value on new Lab Project Hours records.
However, since the New button was overwritten, the Lab Project value no longer is populated, as is the case natively. I'm thinking I need to pass the ID of the current Lab Project record and pass it into my Apex code, but I'm not sure. Any help would be greatly apprecaited.
VisualForce page (simply a redirect):
<apex:page standardController="Lab_Project_Hours__c" extensions="ControllerSetEmployeeName" action="{!SetEmployeeName}" > </apex:page>
Apex Code
public class ControllerSetEmployeeName{ private final Lab_Project_Hours__c objLabProjectHours; // Constructor method public ControllerSetEmployeeName(ApexPages.StandardController stdController){ this.objLabProjectHours = (Lab_Project_Hours__c) stdController.getRecord(); this.EmployeeName = ''; this.EmployeeId = ''; try{ User currentUser = [Select Id, Employee_Page_ID__c From User Where Id = : UserInfo.getUserId() Limit 1]; if(currentUser.Employee_Page_ID__c != null){ List<Employee__c> listEmployee = [Select Id, Name From Employee__c where Id = : currentUser.Employee_Page_ID__c Limit 1]; if(listEmployee.size() > 0){ this.EmployeeId = listEmployee.get(0).Id; this.EmployeeName = listEmployee.get(0).Name; } } } catch(Exception ex){ system.debug(ex.getMessage()); } } // public property public string EmployeeName{ get; set; } public string EmployeeId{ get; set; } // This method is used to set employee name and redirect to project hours new page public PageReference SetEmployeeName(){ PageReference pageRef = new PageReference('/a02/e?nooverride=1&retURL=/a02&CF00N40000001moi9_lkid=' + this.EmployeeId + '&CF00N40000001moi9=' + this.EmployeeName); // Note: Please change the field ids 'CF00N40000001moi9_lkid' and 'CF00N40000001moi9', while deploying this code to production pageRef.setRedirect(true); return pageRef; } }
Thought I'd give an update, I was able to get this code working and all is good now. I took my SOQL out of the constructor and moved it down to the SetEmployeeName() method. Then I captured the incoming URL via ApexPages.currentPage().getParameters() and then parsed out the fields for the Master object. Then it was a matter of appending those to the new URL and redirecting. Full code below, hope someone finds this helpful!
Visualforce page:
Apex Class:
All Answers
Thought I'd give an update, I was able to get this code working and all is good now. I took my SOQL out of the constructor and moved it down to the SetEmployeeName() method. Then I captured the incoming URL via ApexPages.currentPage().getParameters() and then parsed out the fields for the Master object. Then it was a matter of appending those to the new URL and redirecting. Full code below, hope someone finds this helpful!
Visualforce page:
Apex Class:
are different in sandbox from their values in prod. Whatever works in sandbox will fail when you deploy to PROD. Am I missing something?
eric