How to customize VisualForce page to create data in a tabular form that updates data of a field with respect to another?

Problem Statement:

To display field values in a table column on a custom Visualforce depending on another object’s record type and to have the field name as the column header.

Position Object:

We have two objects, Positions and Job applications. A position can have many job applications. There are two record types in Position object, Part-time and Full-time. Each position corresponds to one of these two record types.

Job Application Object:

In the Job application object there are two fields called Part-time and Full-time. The Part-time field appears only for the part-time job applications and the full-time field appears only for the full-time job applications.

Requirement:

Our requirement is to show a custom visual force page which takes the input as the Position Id in its URL and it should display the job applications for the particular position along with the part-time or full-time field values. The main part here is to show the part-time or full-time fields on the column with their respective field label on the column header. The decision to show these fields depends upon the record type of the position.

Solution:

To show column values:

We can achieve this using ‘rendered’ keyword which enables us to conditionally render fields on Visualforce page based on some conditions. The code is as follows.

Code:

<apex:column>

<apex:outputText value=”{!app.FullTime__c}” rendered=”{!IF(app.Position__r.RecordType.Name =’FullTime’,true,false)}”/>

apex:outputText value=”{!app.PartTime__c}” rendered=”{!IF(app.Position__r.RecordType.Name =’Part Time’,true,false)}”/>

</apex:column>

Code Explanation:

If record type name is full time then Fulltime fields appear on the column and if it is Part-time then the part-time fields appear.

To show column header:

To show the column header, normally we use ‘headervalue’ and assign a string to it. But in this case,we need to use ‘If’ condition to check the record type value using ‘CONTAINS’ as shown below –

Code:

<apex:column headervalue=”{!IF(CONTAINS(recordType,’art’),”Part Time”,”Full Time”)}”>

Code Explanation:

The recordType holds the value of the Position object’s record type. If the recordType contains ‘art’ as a sub string on it, then it will return ‘Part Time’ which gets assign as the column header or it will return ‘Full Time’ which gets assign as the column header.

The complete code for column header and column values:

<apex:column headervalue=”{!IF(CONTAINS(recordType,’art’),”Part Time”,”Full Time”)}”>

<apex:outputTextvalue=”{!app.FullTime__c}”rendered=”{!IF(app.Position__r.RecordType.Name =’FullTime’,true,false)}”/>

<apex:outputTextvalue=”{!app.PartTime__c}”rendered=”{!IF(app.Position__r.RecordType.Name =’Part Time’,true,false)}”/>

</apex:column>

DisplayJobApplns.vfp

<apex:page controller=”RetjobAppRecordsController”>

<apex:form >

<apex:pageBlock >

<apex:pageBlockTable value=”{!applns}” var=”app”>

<apex:column value=”{!app.Name}”/>

<apex:column value=”{!app.Id}”/>

<apex:column value=”{!app.Position__r.Name}”/>

<apex:column value=”{!app.Status__c}”/>

<apex:column headervalue=”{!IF(CONTAINS(recordType,’art’),”Part Time”,”Full Time”)}”>

<apex:outputText value=”{!app.FullTime__c}” rendered=”{!IF(app.Position__r.RecordType.Name =’FullTime’,true,false)}”/>

<apex:outputText value=”{!app.PartTime__c}” rendered=”{!IF(app.Position__r.RecordType.Name =’Part Time’,true,false)}”/>

</apex:column>

</apex:pageBlockTable>

</apex:pageBlock>

</apex:form>

</apex:page>

RetjobAppRecordsController.apxc

public class RetjobAppRecordsController {

//To hold all SOQL record

public List<Job_Application__c> applns {

get;

set;

}

//To hold all recordType

public String recordType {

get;

set;

}

//To get the URL Id

public String currentRecordId {get;set;}

public RetjobAppRecordsController (){

currentRecordId  = ApexPages.CurrentPage().getparameters().get(‘id’);

applns=[select id, Name, Position__r.Name, Status__c, Position__r.RecordType.Name,Position__r.RecordTypeValue__c, FullTime__c, PartTime__c from Job_Application__c where Position__c =: currentRecordId ];

for (Job_Application__c app:applns){

recordType = (app.Position__r.RecordType.Name);

}

}

}

Full-time Positions

arun 01

Part-time Positions

arun 02