function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
MultimanMultiman 

Executing a random SOQL query from a managed package

I try to render a random SOQL query as a nice HTML table. The SOQL query can be configured by an admin (so it is only known at runtime).

I try to execute this code in a managed package:
 
String soqlQuery = ApexPages.currentPage().getParameters().get('soql');
// soqlQuery has now the value of a random SOQL query
// e.g. SELECT id FROM Any_Custom_Object__c LIMIT 10
LIST<sObject> records = Database.query(soqlQuery);
String str = Render(records);

That won't work as Any_Custom_Object__c is not an object within the managed package. So I try this:

1. Have an unmanaged package containing this code
 
String soqlQuery = ApexPages.currentPage().getParameters().get('soql');
// soqlQuery has now the value of a random SOQL query
// e.g. SELECT id FROM Any_Custom_Object__c LIMIT 10
LIST<sObject> records = Database.query(soqlQuery);
// render the SOQL query as a nice HTML table
String str = MyManagedPackage.MyClass.Render(records);

2. Have a managed package in the NS "MyManagedPackage" contains a class "MyClass" with a global method "Render(LIST<sObject> records)". This method renders a list of sObjects as an HTML table.

 
Question 1: Would that code within the managed package work? What if records[0] is actually a custom object? Managed packages have no access to managed packages - but actually I'm just passing it as an sObject.
Id myId = records[0].Id;

Question 2: Would  that code within the managed package work?

String objectName = records[0].getSObjectType().getDescribe().getName();

 

Any other ideas how to execute a random SOQL query and parse its result (and its meta data) from managed code?
 
Thank you so much for any hints.
Alex