function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
MLamb2005MLamb2005 

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

 

 

 

 

Best Answer chosen by Admin (Salesforce Developers) 
MLamb2005MLamb2005

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:page standardController="Lab_Project_Hours__c" extensions="ControllerSetLabProjectHourEmployeeName" action="{!SetLabProjectHourEmployeeName}" >

</apex:page>

 

Apex Class:

public class ControllerSetLabProjectHourEmployeeName{

private final Lab_Project_Hours__c objLabProject;

// Constructor method
public ControllerSetLabProjectHourEmployeeName(ApexPages.StandardController stdController){
this.objLabProject = (Lab_Project_Hours__c) stdController.getRecord();
}

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 SetLabProjectHourEmployeeName(){
this.EmployeeName = '';
this.EmployeeId = '';

Map<String, String> parentURL = ApexPages.currentPage().getParameters();

String ProjectId = parentURL.get('CF00N0000000715oD_lkid');
String ProjectRecordTypeId = parentURL.get('RecordType');
String ProjectName = parentURL.get('CF00N0000000715oD');

try{
User currentUser = [Select Id, Employee_Page__c From User Where Id = : UserInfo.getUserId() Limit 1];
if(currentUser.Employee_Page__c != null){
List<Employee__c> listEmployee = [Select Id, Name From Employee__c where Id = : currentUser.Employee_Page__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());
}

PageReference pageRef;

if(ProjectRecordTypeId != null){
pageRef = new PageReference('/a0G/e?nooverride=1&retURL=/' + ProjectId + '&CF00N00000008W0jP_lkid=' + this.EmployeeId + '&CF00N00000008W0jP=' + this.EmployeeName + '&CF00N0000000715oD_lkid=' + ProjectId + '&CF00N0000000715oD=' + ProjectName + '&RecordType=' + ProjectRecordTypeId);
}
else {
pageRef = new PageReference('/a0G/e?nooverride=1&retURL=/' + ProjectId + '&CF00N00000008W0jP_lkid=' + this.EmployeeId + '&CF00N00000008W0jP=' + this.EmployeeName + '&CF00N0000000715oD_lkid=' + ProjectId + '&CF00N0000000715oD=' + ProjectName);
}

pageRef.setRedirect(true);
return pageRef;
}
}

 

All Answers

MLamb2005MLamb2005

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:page standardController="Lab_Project_Hours__c" extensions="ControllerSetLabProjectHourEmployeeName" action="{!SetLabProjectHourEmployeeName}" >

</apex:page>

 

Apex Class:

public class ControllerSetLabProjectHourEmployeeName{

private final Lab_Project_Hours__c objLabProject;

// Constructor method
public ControllerSetLabProjectHourEmployeeName(ApexPages.StandardController stdController){
this.objLabProject = (Lab_Project_Hours__c) stdController.getRecord();
}

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 SetLabProjectHourEmployeeName(){
this.EmployeeName = '';
this.EmployeeId = '';

Map<String, String> parentURL = ApexPages.currentPage().getParameters();

String ProjectId = parentURL.get('CF00N0000000715oD_lkid');
String ProjectRecordTypeId = parentURL.get('RecordType');
String ProjectName = parentURL.get('CF00N0000000715oD');

try{
User currentUser = [Select Id, Employee_Page__c From User Where Id = : UserInfo.getUserId() Limit 1];
if(currentUser.Employee_Page__c != null){
List<Employee__c> listEmployee = [Select Id, Name From Employee__c where Id = : currentUser.Employee_Page__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());
}

PageReference pageRef;

if(ProjectRecordTypeId != null){
pageRef = new PageReference('/a0G/e?nooverride=1&retURL=/' + ProjectId + '&CF00N00000008W0jP_lkid=' + this.EmployeeId + '&CF00N00000008W0jP=' + this.EmployeeName + '&CF00N0000000715oD_lkid=' + ProjectId + '&CF00N0000000715oD=' + ProjectName + '&RecordType=' + ProjectRecordTypeId);
}
else {
pageRef = new PageReference('/a0G/e?nooverride=1&retURL=/' + ProjectId + '&CF00N00000008W0jP_lkid=' + this.EmployeeId + '&CF00N00000008W0jP=' + this.EmployeeName + '&CF00N0000000715oD_lkid=' + ProjectId + '&CF00N0000000715oD=' + ProjectName);
}

pageRef.setRedirect(true);
return pageRef;
}
}

 

This was selected as the best answer
crop1645crop1645
I'm puzzled how your solution works when you go from sandbox to production?  The SFDC field identifiers you use (e.g.
CF00N00000008W0jP

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