You need to sign in to do that
Don't have an account?
passing record id to visualforce page
To test a visualforce page, we pass the record id to the page, as in,
https://c.na9.visual.force.com/apex/pages/AccountRecord?id=a00E0000002RzBR
I want to dynamically pass the record id so I created a class, however it's giving an error, can someone please help me correct my code.
Visualforce Page
<apex:page standardController="Position__c" showHeader="false" > <apex:form> <apex:pageBlock > <apex:pageBlockSection > <apex:sectionHeader title="New Position Details" /> </apex:pageBlockSection > <br/> <b> <apex:OutputLabel value="Your approval has been requested for the following New Position."> </apex:OutputLabel> </b> <br/> <br/> Position Record: {!Position__c.Name} <br/><br/> Status: {!Position__c.Status__c} <br/><br/> Location: {!Position__c.Location__c}<br/><br/> Functional Area: {!Position__c.Functional_Area__c}<br/><br/> Job Level: {!Position__c.Job_Level__c}<br/><br/> Minimum Salary: {!Position__c.Min_Pay__c} Maximum Salary: {!Position__c.Max_Pay__c} <br/><br/><br/><br/><br/><br/> <apex:OutputLabel value="Please Click the appropriate button below to approve or reject the request."> </apex:OutputLabel> <br/><br/> <apex:commandButton action="{!saveAndReject}" styleClass="buttonStyle" style="vertical-align:left;width:80px;height:20px;background:green;" value="Approve" /> <apex:commandButton action="{!saveAndReject}" styleClass="buttonStyle" style="vertical-align:left;width:80px;height:20px;background:red;" value="Reject" /> <br/><br/> </apex:pageBlock> </apex:form> </apex:page>
extension controller
public class JobApp { public Id posId {get;set;} private final Position__c pos; public JobApp(ApexPages.StandardController controller) { posId = controller.getRecord().Id; } public PageReference saveAndReject() { PageReference requestPage = Page.AccountRecord; requestPage.getParameters().put(posId, Position__c.Id); requestPage.setRedirect(true); return requestPage; } }
The above class gives an error -
Error: Compile Error: Incompatible value type Schema.SObjectField for MAP<String,String> at line 15 column 29 |
@nagalakshmi: Thanks for you response.
Based on your suggestion I tried this, and it worked. Thanks!!
I don't want to pass the server name though, anyway i can get around it?
All Answers
It may be that you're providing an Id to a Map that is expecting a String:
requestPage.getParameters().put(posId, Position__c.Id);
Try casting the Id to a string first:
@doubleminus Thanks for correcting the code.
On the click of 'Approve or Reject button, I was hoping to be taken to the Position record.
Does that mean I have to replace return requestPage; with the full URL of the record?
Thanks !!
Just do this:
This would return you back to "xx.salesforce.com/axxOldRecordId".
Hi,
try this code
posId=apexpages().currentpage().getparameters().get('id');
public PageReference saveAndReject() {
PageReference requestPage = new pagereference('/apex/pagename?id='+posid);
return requestpage;
}
@sfdcfox, thanks for your response.
If I do as you suggested, I get this error, should I be passing the record id in view() ? I tried doing so, but got an error. I know I am doing something silly.
System.NullPointerException: Argument cannot be null
Class.JobApp.saveAndReject: line 14, column 1
@nagalakshmi: Thanks for you response.
Based on your suggestion I tried this, and it worked. Thanks!!
I don't want to pass the server name though, anyway i can get around it?
In your code, pos is null. That's why it didn't work. Change your constructor:
You're right in assuming it would be bad to hard-code the server URL.
Instead of hardcoding the instance / domain, we can use:
Which returns https://orgSpecific.instance.my.salesforce.com for example.