I want to display Fields, e.g. Name, OwnerId, CreatedById, CreatedDate, LastModifiedId, LastModifiedDate of an sObject on Visualforce. The sObject is selected on the Visualforce page and then passed to the controller for further usage.
My wrapper Method looks like this:
public wrapperRecord getRecordById_additionalInfo(String selectedRecordId, String selectedObjectId) {
if(selectedRecordId == null || selectedObjectId == null) {
return null;
}else{
Schema.DescribeSObjectResult resultd = Schema.getGlobalDescribe().get(selectedObjectId).getDescribe();
String objectFields = String.join(new List<String>(resultd.fields.getMap().keySet()), ', ');
String soql = 'SELECT ';
if (objectFields.containsIgnoreCase('Name')) {
soql += 'Name, ';
}
if (objectFields.containsIgnoreCase('OwnerId')) {
soql += 'OwnerId, Owner.Name, ';
}
if (objectFields.containsIgnoreCase('CreatedById')) {
soql += 'CreatedById, CreatedBy.Name, ';
}
if (objectFields.containsIgnoreCase('CreatedDate')) {
soql += 'CreatedDate, ';
}
if (objectFields.containsIgnoreCase('LastModifiedById')) {
soql += 'LastModifiedById, LastModifiedBy.Name, ';
}
//if (objectFields.containsIgnoreCase('LastModifiedDate')) {
soql += 'LastModifiedDate ';
//}
soql += ' FROM '+selectedObjectId;
soql += ' WHERE Id=:selectedRecordId';
list<sObject> lstUserRecords_additionalInfo = Database.query(soql);
sObject UserRecords_additionalInfo = lstUserRecords_additionalInfo[0];
list<wrapperRecord> wraplstUserRecords_additionalInfo = new list<wrapperRecord>();
wrapperRecord wrapUserRecord_additionalInfo = new wrapperRecord(UserRecords_additionalInfo, objectFields);
wraplstUserRecords_additionalInfo.add(wrapUserRecord_additionalInfo);
if(wraplstUserRecords_additionalInfo.size() == 1) {
return wraplstUserRecords_additionalInfo[0];
}
return null;
}
}
With the Wrapper defined as:
public class sObjectWrapper {
public string RecordId{get;set;}
public Id UserId{get;set;}
public string HasDeleteAccess{get;set;}
public string HasEditAccess{get;set;}
public string HasReadAccess{get;set;}
public string HasTransferAccess{get;set;}
public string MaxAccessLevel{get;set;}
}
private class wrapperRecord {
public String id { get; set; }
public String name { get; set; }
//public String ownerid { get; set; }
public String ownername { get; set; }
public String createdById { get; set; }
public String createdBy { get; set; }
public String createdDate { get; set; }
public String lastModifiedById { get; set; }
public String lastModifiedBy { get; set; }
public String lastModifiedDate { get; set; }
public wrapperRecord(sobject UserRecords_additionalInfo, String objectFields) {
this.id = String.valueOf(UserRecords_additionalInfo.get('Id'));
if (objectFields.containsIgnoreCase('Name')){
this.name =String.valueOf(UserRecords_additionalInfo.get('Name'));
} else{
this.name ='n/a';
}
if (objectFields.containsIgnoreCase('OwnerId')){
this.ownername =String.valueOf(UserRecords_additionalInfo.getSObject('Owner').get('Name'));
} else{
this.ownername ='n/a';
}
if (objectFields.containsIgnoreCase('CreatedById')){
this.createdById =String.valueOf(UserRecords_additionalInfo.getSObject('CreatedBy').get('Name'));
} else{
this.createdById ='n/a';
}
if (objectFields.containsIgnoreCase('CreatedDate')){
this.createdDate =String.valueOf(UserRecords_additionalInfo.get('CreatedDate'));
} else{
this.createdDate ='n/a';
}
if (objectFields.containsIgnoreCase('LastModifiedById')){
this.lastModifiedById =String.valueOf(UserRecords_additionalInfo.getSObject('LastModifiedBy').get('Name'));
} else{
this.lastModifiedById ='n/a';
}
if (objectFields.containsIgnoreCase('LastModifiedDate')){
this.lastModifiedDate =String.valueOf(UserRecords_additionalInfo.get('LastModifiedDate'));
} else{
this.lastModifiedDate ='n/a';
}
}
}
And the Page looks something like this:
<apex:pageBlock title="Record Information: {!objRecordInfo.Id}" rendered="{!selectedObjectId!=null && selectedRecordId!=null}">
<apex:pageBlockSection columns="2" collapsible="false" id="recordInformation”>
<apex:pageBlockSectionItem >
<apex:outputText value="{!objRecordInfo.name}" id="record__RecordName"/>
</apex:pageBlockSectionItem>
<apex:pageBlockSectionItem >
<apex:outputText value="{!objRecordInfo.ownerid}” id="record__RecordOwner"/>
</apex:pageBlockSectionItem>
<apex:pageBlockSectionItem >
<apex:outputText value="{!objRecordInfo.createdById}" id="record__CreatedBy"/>
<apex:outputText value=", {!objRecordInfo.createdDate}" id="record__createdDate"/>
</apex:pageBlockSectionItem>
<apex:pageBlockSectionItem >
<apex:outputText value="{!objRecordInfo.lastModifiedById}" id="record__LastModifiedBy"/>
<apex:outputText value=", {!objRecordInfo.lastModifiedDate}" id="record__lastModifiedDate"/>
</apex:pageBlockSectionItem>
</apex:pageBlockSection>
</apex:pageBlock>
The problem here is now, that the page works for all the objects which contain all the fields. For sObjects which do not contain the fields I do get an error:
System.QueryException: No such column 'Name' on entity 'Case'. If you are attempting to use a custom field, be sure to append the '__c' after the custom field name. Please reference your WSDL or the describe call for the appropriate names.
It seems the if clauses are ignored and all fields of the soql query outputted for each sObject. The wrapper class works fine when I put default values in. Does anybody have some advice here?