+ Start a Discussion
lsateeshklsateeshk 

Apex Table with multiple record types..

Hello..,

 

  I have a custom object and it has 3 record types .., say apple, banana, and orange.., 

I have  2 fields in each corresponding record type say ap_cost, ap_color for apple and ba_cost, ba_color for banana.., similarly for orange.., 

 

Now i want to dispaly in a grid having like

 

   

fruits           cost        color

apple          value       value

banana       value      value

orange         value      value

 

 

and it has to be dynamic  as i may have more records with same data type .., say i can have two records for apple in that object ..,  Quick reply is highly appreciatble..,

 

Thanks,

Lucky.

 

 

 

Best Answer chosen by Admin (Salesforce Developers) 
lsateeshklsateeshk

Yayyyyyyyyyy!!!!!!!!!!!!!!!!!!!!! Got It ...,

 

First of all Thank you so much @Prakash Purswnani 

 

 

for your idea and with the outline.., with few edits and few changes got the solution.. :)

 

here goes the vf page:

 

 

<apex:page showHeader="false" sidebar="false" StandardController="child_object__c" extensions="DisplayRecordTypes">
  <apex:pageBlock title="Services Overview">
  <apex:pageBlockTable value="{!details}" var="item">
<apex:column value="{!item.RecordType.Name}"/>
<apex:column >{!if(item.RecordType.Name=='Apple',item.ap_cost__c,
                if(item.RecordType.Name=='Banana',item.bn_cost__c,
                if(item.RecordType.Name=='Orange',item.or_cost__c,false)))}
</apex:Column>
<apex:column >{!if(item.RecordType.Name=='Apple',item.ap_color__c,
                if(item.RecordType.Name=='Banana',item.bn_color__c,
                if(item.RecordType.Name=='Orange',item.or_color__c,false)))}
</apex:Column>
</apex:pageBlockTable>
</apex:pageBlock>
</apex:page>

 

 

and Controller extension code:

 

public with sharing class DisplayRecordTypes {


private final ApexPages.StandardController controller;

    public DisplayRecordTypes(ApexPages.StandardController controller) {
        this.controller = controller;
    }
    
    Id apId = [select Id,name from RecordType where name='Apple' and SObjectType='child_object__c' limit 1].Id;
    Id bnId = [select Id,name from RecordType where name='Banana' and SObjectType='child_object__c' limit 1].Id;
    Id orId = [select Id,name from RecordType where name='Orange' and SObjectType='child_object__c' limit 1].Id;
    
List<child_object__c> mainList =new List<child_object__c>();




List<child_object__c> appleList = new List<child_object__c>([SELECT RecordType.Name,ap_cost__c, ap_color__c FROM child_object__c WHERE RecordTypeId= :apId]);
List<child_object__c> bananaList = new List<child_object__c>([SELECT RecordType.Name,bn_cost__c, bn_color__c FROM child_object__c WHERE RecordTypeId= :bnId]);
List<child_object__c> orangeList =new List<child_object__c>([SELECT RecordType.Name,or_cost__c, or_color__c FROM child_object__c WHERE RecordTypeId= :orId]);

public List<child_object__c> getDetails(){
 
 List<child_object__c> mainList =new List<child_object__c>();
mainList.addall(appleList);
mainList.addall(bananaList);
mainList.addall(orangeList);

return mainList;

}
}

 

with the above code i got the solution..,

 

Thanks again @Prakash Purswnani

 

Thanks,

Lucky.

 

 

 

All Answers

prakash_sfdcprakash_sfdc
Hi,

Sorry, I am not able to get the question properly. How does recordtype is stopping you from displaying the records.

Just create a main list of the custom object and write 3 SOQL with different RecordtypeId in WHERE condition. Add up all the records in the main list and display on the VF page with the help of repeat or pageblocktable
lsateeshklsateeshk

Hi Prakash.., 

 

      Thanks for your reply..,  

 

i do have totally 6 fields in that custom object.., say apple_cost, apple_color , banana_cost, banana_color and orange_cost, orange_color .., ok? 

 

i will display apple_cost, apple_color fields in apple record type only .., and  banana_cost, banana_color in banana record type only..., similary remianing two in orange record type.., ok?

 

now i want total consolidated view in a grid like as shown .., 

 

Fruits          cost                        color

Apple      apple_cost          apple_color

banana  banana_cost       banana_color

orange   orange_Cost       orange_color

 

the problem here is i was using apex:column and page block table .., so i need to diplay differnt fields in single column for different record types ..., i could nt get it .., understood?  is there any solution for this scenario...?

 

THanks,

Lucky.

 

 

 

 

 

 

Sunny670Sunny670
If your table is like
Fruits cost color
fruit type fruit cost fruit color
In the controller You can retrieve data based on fruit type and get values of fruit cost ,fruit color and display in the page based on fruit type. If fruit type is Apple have a condition to display apple related data. Similarly other two.
prakash_sfdcprakash_sfdc
Ya. It is possible.

Recordtypes are different but the Object is same.

//Apex
List<objectname> mainList=new List<objectname>();

List<objectname> appleList=[SELECT apple_cost, apple_color FROM objectname WHERE RecordTypeId='xxx'];
List<objectname> bananaList=[SELECT banana_cost, banana_color FROM objectname WHERE RecordTypeId='xxx'];
List<objectname> orangeList=[SELECT orange_cost, orange_color FROM objectname WHERE RecordTypeId='xxx'];

mainList.addall(appleList);
mainList.addall(bananaList);
mainList.addall(orangeList);
If you want in specific order like apple,banana,orange,apple,banana,orange.. then you have to play with the above list
//VF page
<apex:pageBlockTable value="{!mainList}" item="item">
<apex:column value="{!item.Name}"/>
<apex:column value="{!if(item.Name=='Apple',item.apple_cost,if(item.Name=='banana',item.banana_cost,if(item.Name=='Orange',orange_cost,false)))"/>
similarly for color

I would suggest to keep three fields in the object Name,Cost and Color. Anyways Name is different so your requirement will be achieved.
</apex:pageBlockTable>

lsateeshklsateeshk

@Prakash Thank you so much for your patience and for replying  me with outline..,

 

I tried your solution.., like this..,

 

vf page:

 

<apex:page showHeader="false" sidebar="false" StandardController="child_object__c" extensions="DisplayRecordTypes">
  <apex:pageBlockTable value="{!mainList}" item="item">
<apex:column value="{!item.Name}"/>
<apex:column value="{!if(item.Name=='Apple',item.ap_cost__c,if(item.Name=='Banana',item.bn_cost__c,if(item.Name=='Orange',or_cost__c,false)))"/>
<apex:column value="{!if(item.Name=='Apple',item.ap_color__c,if(item.Name=='Banana',item.bn_color__c,if(item.Name=='Orange',or_color__c,false)))"/>
</apex:page>

 

and in controller extension class:

 

public with sharing class DisplayRecordTypes {

private final ApexPages.StandardController controller;

    public DisplayRecordTypes(ApexPages.StandardController controller) {
        this.controller = controller;
    }
    
    Id apId = [select Id,name from RecordType where name='Apple' and SObjectType='child_object__c' limit 1].Id;
    Id bnId = [select Id,name from RecordType where name='Banana' and SObjectType='child_object__c' limit 1].Id;
    Id orId = [select Id,name from RecordType where name='Orange' and SObjectType='child_object__c' limit 1].Id;
    
List<child_object__c> mainList=new List<child_object__c>();

List<child_object__c> appleList=[SELECT ap_cost__c, ap_color__c FROM child_object__c WHERE RecordTypeId= :apId];
List<child_object__c> bananaList=[SELECT bn_cost__c, bn_color__c FROM child_object__c WHERE RecordTypeId= :bnId];
List<child_object__c> orangeList=[SELECT or_cost__c, or_color__c FROM child_object__c WHERE RecordTypeId= :orId];

mainList.addall(appleList);
mainList.addall(bananaList);
mainList.addall(orangeList);
}

 but

 

I am getting an error called " Error: DisplayRecordTypes Compile Error: unexpected token: ')' at line 19 column 25 " i.e.., [in

mainList.addall(appleList); ]

 

So  I dont know where i am going wrong.., and if possible try to get code corrected in maps and is there any possible to get dynamically other than hardcoding in vf page? y because in future i may have new services ( in this case Fruits) .., so it would be helpful if i can make dynamic?

 

Thanks,

Lucky.

 

prakash_sfdcprakash_sfdc
Hi,

It's looking correct, I don't know why you are getting this error. I checked the syntax, it's working on my side. Please check the semi colons once again.

For dynamic adding, you should not create 3-3 fields for all the fruits. Just create 3 fields (1-dropdown with fruits name, 2-Cost and 3-Color)
lsateeshklsateeshk

Yayyyyyyyyyy!!!!!!!!!!!!!!!!!!!!! Got It ...,

 

First of all Thank you so much @Prakash Purswnani 

 

 

for your idea and with the outline.., with few edits and few changes got the solution.. :)

 

here goes the vf page:

 

 

<apex:page showHeader="false" sidebar="false" StandardController="child_object__c" extensions="DisplayRecordTypes">
  <apex:pageBlock title="Services Overview">
  <apex:pageBlockTable value="{!details}" var="item">
<apex:column value="{!item.RecordType.Name}"/>
<apex:column >{!if(item.RecordType.Name=='Apple',item.ap_cost__c,
                if(item.RecordType.Name=='Banana',item.bn_cost__c,
                if(item.RecordType.Name=='Orange',item.or_cost__c,false)))}
</apex:Column>
<apex:column >{!if(item.RecordType.Name=='Apple',item.ap_color__c,
                if(item.RecordType.Name=='Banana',item.bn_color__c,
                if(item.RecordType.Name=='Orange',item.or_color__c,false)))}
</apex:Column>
</apex:pageBlockTable>
</apex:pageBlock>
</apex:page>

 

 

and Controller extension code:

 

public with sharing class DisplayRecordTypes {


private final ApexPages.StandardController controller;

    public DisplayRecordTypes(ApexPages.StandardController controller) {
        this.controller = controller;
    }
    
    Id apId = [select Id,name from RecordType where name='Apple' and SObjectType='child_object__c' limit 1].Id;
    Id bnId = [select Id,name from RecordType where name='Banana' and SObjectType='child_object__c' limit 1].Id;
    Id orId = [select Id,name from RecordType where name='Orange' and SObjectType='child_object__c' limit 1].Id;
    
List<child_object__c> mainList =new List<child_object__c>();




List<child_object__c> appleList = new List<child_object__c>([SELECT RecordType.Name,ap_cost__c, ap_color__c FROM child_object__c WHERE RecordTypeId= :apId]);
List<child_object__c> bananaList = new List<child_object__c>([SELECT RecordType.Name,bn_cost__c, bn_color__c FROM child_object__c WHERE RecordTypeId= :bnId]);
List<child_object__c> orangeList =new List<child_object__c>([SELECT RecordType.Name,or_cost__c, or_color__c FROM child_object__c WHERE RecordTypeId= :orId]);

public List<child_object__c> getDetails(){
 
 List<child_object__c> mainList =new List<child_object__c>();
mainList.addall(appleList);
mainList.addall(bananaList);
mainList.addall(orangeList);

return mainList;

}
}

 

with the above code i got the solution..,

 

Thanks again @Prakash Purswnani

 

Thanks,

Lucky.

 

 

 

This was selected as the best answer