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
Roshan TamrakarRoshan Tamrakar 

Page Redirect Issue

Hi Experts,

 

I have 3 custom objects:

      Person__c

      Vehicle__c

      Person_Vehicle__c

 

The Person_Vehicle__c is child of Person and it has lookup relationship with Vehicle. 

 

I created a VisualForce page and placed it in person tab. The VisualForce page lists all child records for the person whose data is being displayed. The display in person page is something like:

 

Person Name             Vehicle

Tirtha                           Maruti 800

Tirtha                           Zen

 

The ‘Vehicle’ items are commandLink so that if I click ‘Maruti 800’, the page redirects to ‘Maruti 800’ detail page.

 

The following is my page and controller code:

 

Code:
public class VehicleListControllerClass{

 public String getPersonName(){
  ID pId = System.currentPageReference().getParameters().get('id');
  String name = [Select Name from Person__c where id=:pId].Name;
  return name;
 }

 public List<Person_Vehicle__c> getVehicleList(){
  ID pId = System.currentPageReference().getParameters().get('id');
  List<Person_Vehicle__c> data = new List<Person_Vehicle__c>(
   [Select p.Person__r.Name, p.Vehicle__c, p.Vehicle__r.Name from Person_Vehicle__c p where p.Person__c=:pId]);
   return data;
 }

 public PageReference OpenVehicle(){
  PageReference page = System.CurrentPageReference();
  ID vId = page.getParameters().get('vehicleId');
  return new PageReference('/'+vId);
 }
}

<apex:page controller="VehicleListControllerClass">
 <apex:form>
  <apex:pageBlock title="Vehicle List for {!personname}">
   <apex:pageBlockList value="{!vehicleList}" var="v">
    <apex:column value="{!v.Person__r.Name}"/>
    <apex:column>
     <apex:commandLink value="{!v.Vehicle__r.Name}" action="{!OpenVehicle}">
      <apex:param name="vehicleId" value="{!v.Vehicle__c}"/>
     </apex:commandLink>
    </apex:column>
   </apex:pageBlockList>
  </apex:pageBlock>
 </apex:form>
</apex:page>


 

The first issue:

When I click on any vehicle item, it works nicely and respective vehicle detail is displayed on the page. The url becomes:

https://na2.salesforce.com/a0340000004dX5AAAU?inline=1

 

But, if I try to do the same with ‘right-click-->Open link in new tab’, it doesn’t work and instead displays error ‘URL No Longer Exists’. The url, in this case, becomes:

https://na2.salesforce.com/servlet/servlet.Integration?lid=0664000000000JY&ic=1#

 

The second issue:

The second problem is that I do not see the column header for ‘vehicle’ (second column). I also tried to use facet, but it also didn’t work:

Code:
<apex:column>
 <apex:facet name="header">Vehicle</apex:facet>
 <apex:commandLink value="{!v.Vehicle__r.Name}" action="{!OpenVehiclePage}">
  <apex:param name="vehicleId" value="{!v.Vehicle__c}"/>
 </apex:commandLink>
</apex:column>


 
Please suggest.

Thanks,

-Roshan

dchasmandchasman
OK a few things:

- Using an action method for redirect navigation is often overkill (as it looks to be in your example) - I would switch to <apex:outputLink> in this situation (should also provide a solution for issue #1 below):
<apex:commandLink value="{!v.Vehicle__r.Name}" action="{!OpenVehicle}">
<apex:param name="vehicleId" value="{!v.Vehicle__c}"/>
</apex:commandLink>
becomes
<apex:outputLink value="/{!v.Vehicle__r.Id}">{!v.Vehicle__r.Name}</apex:outputLink>
- Embedded SOQL already returns a list so there is no need to construct another one, e.g.:
public List<Person_Vehicle__c> getVehicleList(){
ID pId = System.currentPageReference().getParameters().get('id');
List<Person_Vehicle__c> data = new List<Person_Vehicle__c>(
[Select p.Person__r.Name, p.Vehicle__c, p.Vehicle__r.Name from Person_Vehicle__c p where p.Person__c=:pId]);
return data;
}

can be simplified to

public List<Person_Vehicle__c> getVehicleList(){
ID pId = System.currentPageReference().getParameters().get('id');
return [Select p.Person__r.Name, p.Vehicle__c, p.Vehicle__r.Name from Person_Vehicle__c p where p.Person__c=:pId];
}
For issue #2 try using <apex:column headerValue="Your Column Header">


Message Edited by dchasman on 04-29-2008 06:17 PM
Roshan TamrakarRoshan Tamrakar
Many thanks Doug,

Your both of the solutions worked finely.
But there occurred a little problem on my first issue. Mainly this visualforce page is placed inside the Person tab by creating a new section in Person's page layout. After implementing your suggestion (by using outputLink tag), when I clicked the 'Vehicle' link, the detail of respective vehicle displayed within the section with parent tab (Person) remained unchanged. What I wanted was to refresh the whole page and display just the Vehicle detail page.

So I added another tag 'target="_top"'...

Code:
<apex:page showHeader="false" controller="VehicleListControllerClass">
<apex:form>
<apex:pageBlock title="Vehicle List for {!personname}">
<apex:pageBlockList value="{!vehicleList}" var="v">
<apex:column value="{!v.Person__r.Name}"/>
<apex:column headerValue="Vehicle">
<apex:outputLink value="/{!v.Vehicle__c}" target="_top">{!v.Vehicle__r.Name}</apex:outputLink>
</apex:column>
</apex:pageBlockList>
</apex:pageBlock>
</apex:form>
</apex:page>

...and it worked!
I just want your concern on "Is it a good approach?"

As far as constructing another list while SOQL already returns 'list', I wanted to manipulate some of the field values according to my requirement like...

Code:
 public List<Person_Vehicle__c> getVehicleList(){
ID pId = System.currentPageReference().getParameters().get('id');
List<Person_Vehicle__c> data =
new List<Person_Vehicle__c>([Select p.Person__r.Name, p.Vehicle__c,
p.Vehicle__r.Name from Person_Vehicle__c p where p.Person__c=:pId]);
for (Person_Vehicle__c p : data){
p.Person__r.Name = something(p.Person__r.Name);
}

return data;
}

public String something(String args){
String ret=args;
if(args=='Tirtha')
ret +=' (Dad)';
if(args=='Manoj')
ret +=' (Brother)';
if(args=='Bishal')
ret += ' (Son)';

return ret;
}

 This is because, I was unable to use 'IF' condition in visualforce page and I don't know how to pass parameter from visualforce page to controller.

And thanks again for your help.

-Roshan