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
SabrentSabrent 

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>  &nbsp;&nbsp;
         <apex:OutputLabel value="Your approval has been requested for the following New Position."> 
         </apex:OutputLabel>
    </b>
      
   <br/> <br/>
    &nbsp;&nbsp;
    Position Record: {!Position__c.Name} <br/><br/>
   
    &nbsp;&nbsp;
    Status: {!Position__c.Status__c} <br/><br/>
   
    &nbsp;&nbsp;
    Location: {!Position__c.Location__c}<br/><br/>
    
    &nbsp;&nbsp;
    Functional Area: {!Position__c.Functional_Area__c}<br/><br/>
    
    &nbsp;&nbsp;
    Job Level: {!Position__c.Job_Level__c}<br/><br/>
    
    &nbsp;&nbsp;
    Minimum Salary: {!Position__c.Min_Pay__c}
    Maximum Salary: {!Position__c.Max_Pay__c}
    
    <br/><br/><br/><br/><br/><br/>
    
    &nbsp;&nbsp;&nbsp;
    
    <apex:OutputLabel value="Please Click the appropriate button below to approve or reject the request."> 
    </apex:OutputLabel> <br/><br/>
    
    &nbsp;&nbsp;&nbsp;
           
    <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
Best Answer chosen by Admin (Salesforce Developers) 
SabrentSabrent

@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?

 

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 = new pagereference('https://na9.salesforce.com/'+posid);
requestPage.setRedirect(true);
return requestPage;
}
}

 

 

 

 

 

All Answers

doubleminusdoubleminus

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:

PageReference requestPage = Page.AccountRecord;
String idString = Position__c.Id + '';
requestPage.getParameters().put(posId, idString);
requestPage.setRedirect(true);
return requestPage;

 

SabrentSabrent

@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 !!

 

 

sfdcfoxsfdcfox

Just do this:

 

return new ApexPages.StandardController(pos).view();

This would return you back to "xx.salesforce.com/axxOldRecordId".

nagalakshminagalakshmi

Hi,

 

try this code

 

posId=apexpages().currentpage().getparameters().get('id');
public PageReference saveAndReject() {

PageReference requestPage = new pagereference('/apex/pagename?id='+posid);
return requestpage;

}


SabrentSabrent

@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.

 

Visualforce Error

System.NullPointerException: Argument cannot be null
Class.JobApp.saveAndReject: line 14, column 1

 

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 = new ApexPages.StandardController(pos).view();
requestPage.setRedirect(true);
return requestPage;
}
}

 

 

SabrentSabrent

@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?

 

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 = new pagereference('https://na9.salesforce.com/'+posid);
requestPage.setRedirect(true);
return requestPage;
}
}

 

 

 

 

 

This was selected as the best answer
sfdcfoxsfdcfox

In your code, pos is null. That's why it didn't work. Change your constructor:

 

{
 pos = controller.getRecord();
 posId = pos.Id;
}

You're right in assuming it would be bad to hard-code the server URL.

Jamie 5mithJamie 5mith
OP: "I don't want to pass the server name though, anyway i can get around it?"

Instead of hardcoding the instance / domain, we can use:
 
URL.getSalesforceBaseUrl().toExternalForm();

Which returns https://orgSpecific.instance.my.salesforce.com​ for example.