+ Start a Discussion
Santi Ram RaiSanti Ram Rai 

Diaplay Record Type in visualforce page

Hi every body,

I want to display Record Type in Visualforce page. But how, i tried this but it doen`t work.
User-added image

My Code:
<apex:page standardController="Project_Task__c" extensions="projectTaskController" action="{!runSearch}" showHeader="false" standardStylesheets="true" sidebar="false" applyHtmlTag="false" applyBodyTag="false" docType="html-5.0" >
  <apex:form >
  <apex:pageMessages id="errors" />
  <apex:pageBlock title="Find Project Task" mode="edit">
  <apex:pageBlockButtons location="top">
    <apex:commandButton value="New Project Task " action="{!create}"/>
  </apex:pageBlockButtons>
  <table width="100%" border="0">
  <tr>  
    <td width="200" valign="top">
      <apex:pageBlock title="Parameters" mode="edit" id="criteria">
      <script type="text/javascript">
          function doSearch() {
            searchServer(
              document.getElementById("ProjectTaskName").value,
              document.getElementById("LeaveType").options[document.getElementById("LeaveType").selectedIndex].value);
          }
      </script> 
      <apex:actionFunction name="searchServer" action="{!runSearch}" rerender="results,debug,errors">
           <apex:param name="ProjectTaskName" value="" />
           <apex:param name="LeaveType" value="" />
      </apex:actionFunction>
      <table cellpadding="2" cellspacing="2">
       <tr>
       <td style="font-weight:bold;">Project Task Name<br/>
        <input type="text" id="ProjectTaskName" onkeyup="doSearch();"/>
        </td>
      </tr>
      <tr>
        <td style="font-weight:bold;">Leave Type<br/>
          <select id="LeaveType" onchange="doSearch();">
            <option value=""></option>
            <apex:repeat value="{!LeaveType}" var="tech">
              <option value="{!tech}">{!tech}</option>
            </apex:repeat>
          </select>
        </td>
      </tr>
      </table>
      </apex:pageBlock>
    </td>
    <td valign="top">
    <apex:pageBlock mode="edit" id="results">
        <apex:pageBlockTable value="{!projectTask}" var="pt">
		<apex:column headerValue="Action" style="float:right;width:90%">
            <apex:outputLink style="Color:blue" value="{!URLFOR($Action.Project_Task__c.Edit,pt.Id)}"> 
                Edit 
            </apex:outputLink>     
         </apex:column> 
                  <!--<apex:column >
                <apex:facet name="header">
                    <apex:commandLink value="Code" action="{!toggleSort}" rerender="results,debug">
                        <apex:param name="sortField" value="Name" assignTo="{!sortField}"/>
                    </apex:commandLink>
                </apex:facet>
                <apex:outputField value="{!pt.Name}"/>
            </apex:column>-->
            <apex:column >
                <apex:facet name="header">
                    <apex:commandLink value="Project Task Name" action="{!toggleSort}" rerender="results,debug">
                        <apex:param name="sortField" value="Project Name" assignTo="{!sortField}"/>
                    </apex:commandLink>
                </apex:facet>
                <apex:outputField value="{!pt.Project_Task_Name__c}"/>
            </apex:column>
             <apex:column >
                <apex:facet name="header">
                    <apex:commandLink value="Project Name" action="{!toggleSort}" rerender="results,debug">
                        <apex:param name="sortField" value="Project Name" assignTo="{!sortField}"/>
                    </apex:commandLink>
                </apex:facet>
                <apex:outputField value="{!pt.pro_has_many_protask__c}"/>
            </apex:column>
             <apex:column >
                <apex:facet name="header">
                    <apex:commandLink value="Leave Type" action="{!toggleSort}" rerender="results,debug">
                        <apex:param name="sortField" value="Leave Type" assignTo="{!sortField}"/>
                    </apex:commandLink>
                </apex:facet>
                <apex:outputField value="{!pt.Leave_Type__c}"/>
            </apex:column>
            <apex:column >
                <apex:facet name="header">
                    <apex:commandLink value="Reccord Type" action="{!toggleSort}" rerender="results,debug">
                        <apex:param name="sortField" value="Reccord Type" assignTo="{!sortField}"/>
                    </apex:commandLink>
                </apex:facet>
                <apex:outputField value="{!pt.RedordType}"/>
            </apex:column>
        </apex:pageBlockTable>
    </apex:pageBlock>
    </td>
  </tr>
  </table>
  </apex:pageBlock>
  </apex:form>
</apex:page>


 
logontokartiklogontokartik
The RecordType object is a relational object, for any object if you have record types defined, it creates relationship between that object and RecordType, so in order for you to refer to fields on Record Type you need to use dot notation. So the RecordType Name is retrieved using
 
{!pt.RecordType.Name}

Make sure you retrieve the same in your SOQL query when querying for RecordType i.e
 
[Select RecordType.Name,....]


Best.

 
Santi Ram RaiSanti Ram Rai
Thanks i got it, but i have another question, actually i have three record type
i) Leave
ii) Holiday
ii) Sick

Now, my question is; how to retrieve them with SOQL individually. Please can you help me.

This is my code:
public with sharing class projectTaskController {
	public projectTaskController(ApexPages.StandardController controller) {
    }
    
    //The soql without the order and limit
    private String soql {get;set;}
  
    //The collection of contacts to display
    public List<Project_Task__c> projectTask{get;set;}

   //The current sort direction. defaults to asc
    public String sortDir {
    get  { if (sortDir == null) {  sortDir = 'asc'; } return sortDir;}
    set;
    }

    //The current field to sort by. defaults to  name
    public String sortField {
    get{ if (sortField == null) {sortField = 'Name'; } return sortField;}
    set;
    }

    //Format the soql for display on the visualforce page
    public String debugSoql {
        get { return soql + ' order by ' + sortField + ' ' + sortDir; }
        set;
    }
 
    //Toggles the sorting of query from asc<-->desc
    public void toggleSort() {
        // simply toggle the direction
        sortDir = sortDir.equals('asc') ? 'desc' : 'asc';
        // run the query again
        runQuery();
    }

   //Runs the actual query
    public void runQuery(){
        try {
          projectTask = Database.query(soql + ' order by ' + sortField + ' ' + sortDir);
        }catch (Exception e) {
        ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, 'Ooops!'));
        }
    }
    
    //Search by Project Task Types.
    //Runs the search with parameters passed via Javascript
    public PageReference runSearch() {
        Id accId= Apexpages.currentPage().getParameters().get('Id');
        String LeaveType = Apexpages.currentPage().getParameters().get('LeaveType');
         String ProjectTaskName = Apexpages.currentPage().getParameters().get('ProjectTaskName');
         String RecordType= Apexpages.currentPage().getParameters().get('RecordType');
            
        soql = 'select Name,Project_Task_Name__c,Project__c,Leave_Type__c,Time_Duration__c,RecordType.Name,pro_has_many_protask__c from Project_Task__c where Name !=null';
        
        if (LeaveType!= null &&!LeaveType.equals(''))
        soql += ' and Leave_Type__c LIKE \''+ String.escapeSingleQuotes(LeaveType)+'%\'';
        if (ProjectTaskName!= null && !ProjectTaskName.equals(''))
        soql += ' and Project__c LIKE \''+String.escapeSingleQuotes(ProjectTaskName)+'%\'';
        if (RecordType!= null &&!RecordType.equals(''))
        soql += ' and RecordType.Name LIKE \''+String.escapeSingleQuotes(RecordType)+'%\'';
        
        // run the query again
        runQuery();
        return null;
    }

  	public List<String> LeaveType{
        get {
            if (LeaveType== null) {
            LeaveType = new List<String>();
            Schema.DescribeFieldResult field = Project_Task__c.Leave_Type__c.getDescribe();
            for (Schema.PicklistEntry f : field.getPicklistValues())
                LeaveType.add(f.getLabel());
            }
            return LeaveType;          
        }
        set;
    }
}

 
logontokartiklogontokartik
There are many ways through which you can retrievd the Record Types on an Object
  • SOQL query - Query RecordType object
List<RecordType> rts = [SELECT Id FROM RecordType WHERE SObjectType='OBJECT_NAME_HERE' ];
  • Dynamic Describes
//Generate a map of tokens for the sObjects in your organization
Map gd = Schema.getGlobalDescribe();

//Retrieve the describe result for the desired object
DescribeSObjectResult result = gd.get('Account').getDescribe();

//Generate a map of tokens for all the Record Types for the desired object
Map<String.String> recordTypeInfo = result.getRecordTypeInfosByName();
system.debug('RECORD TYPES:' + recordTypeInfo);




 
Santi Ram RaiSanti Ram Rai
This is my:
SOQL:soql = 'select Name,Project_Task_Name__c,Project__c,Leave_Type__c,Time_Duration__c,RecordType.Name,pro_has_many_protask__c from Project_Task__c where Name !=null AND SObjectType='Leave'';

But it doesn`t work.
logontokartiklogontokartik
f you want to filter on a single Record Type then in the Where Clause you need to use RecordType.Name
 
soql = 'select Name,Project_Task_Name__c,Project__c,Leave_Type__c,Time_Duration__c,RecordType.Name,pro_has_many_protask__c from Project_Task__c where Name !=null AND RecordType.Name=/''+String.escapeSingleQuotes('Leave')+'/''

 
Santi Ram RaiSanti Ram Rai
I got this error message "expecting a semi-colon, found '+String.escapeSingleQuotes('"
 
logontokartiklogontokartik
Its a compile error, make sure the soql is well formed.
 
soql = 'select Name,Project_Task_Name__c,Project__c,Leave_Type__c,Time_Duration__c,RecordType.Name,pro_has_many_protask__c from Project_Task__c where Name !=null AND RecordType.Name=/''+String.escapeSingleQuotes('Leave')+'/'';

 
Santi Ram RaiSanti Ram Rai
This is the page display before the adding the code:
User-added image

And this is the page display after adding the code:
User-added image
Krishna SambarajuKrishna Sambaraju
Add the variable soql in your exception (wherever you are displaying Oops, add the variable soql like this 'Oops \n' + soql). Then you will be able to see the soql query string in the error message. Then you will know if the soql is well formed or not. Correct it accordingly to fix it.