You need to sign in to do that
Don't have an account?
Pranavaditya Kannojoo
Code to reference a standard Object dynamically
We have some apex classes which refer to ChatterConversationMessage (standard object) and use the data from the same. This object has details of private message and we could capture the same as expected. When we disable Private Messaging feature in salesforce, this object is non-existent and salesforce complains that our package has to be re-compiled.
I would like to know if there is any mechanism to check if this such an sObject exist and based on that retrieve its fields dynamically. Essentially we would want the same package to work when the sObject ChatterConversationMessage is available or not.
I would like to know if there is any mechanism to check if this such an sObject exist and based on that retrieve its fields dynamically. Essentially we would want the same package to work when the sObject ChatterConversationMessage is available or not.
You can use global describe method.
if(Schema.getGlobalDescribe().get('ChatterConversationMessage') == null){
// Object is not present
}
All Answers
You can use global describe method.
if(Schema.getGlobalDescribe().get('ChatterConversationMessage') == null){
// Object is not present
}
Map<String, Schema.SObjectType> gd = Schema.getGlobalDescribe();
if(gd.containsKey('ChatterConversationMessage')){
Schema.SobjectType chatterConSobjectType = gd.get('ChatterConversationMessage');
Schema.DescribeSObjectResult d = chatterConSobjectType.getDescribe();
Map<String, Schema.SObjectField> FsMap = d.fields.getMap();
}
https://www.salesforce.com/us/developer/docs/apexcode/Content/apex_methods_system_sobject_describe.htm#apex_Schema_DescribeSObjectResult_fields