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
neao18neao18 

How not to include Custom Objects in a Managed Packege.

Hi All,

 

We have a senario as:


We have developed a VF page that gets data from a object as xyz__c. The page is working fine and we want to make a managed packege to deploy to all of our orgs.

But the point is that xyz__c is already present on the other orgs. And we cannot remove the dependency object from a package.

So the question is that: How I can only make a managed package with out the xyz__c object and only have the VF page and classes??

sfdcfoxsfdcfox

You have two basic options:

1) Make the object xyz__c in a managed package, then install it into the developer edition (DE) org that uses the object. This will require that the base package be installed before the extension package is installed.

 

2) Make all references to xyz__c dynamic via Schema.getGlobalDescribe(), and all field references via xyz__c.SObjectType.fields.getMap(). If your code mades no explicit references to xyz__c, it will not force the object to be part of the package. It works like this:

 

// Old code:

xyz__c value = new xyz__c();
value.field__c = '12345';
insert value;

// New Code:
SObjectType entType = Schema.getGlobalDescribe().get('xyz__c').getSObjectType();
DescribeSObjectResult entDes = entType.getDescribe();
SObjectField entField = entDes.fields.getMap().get('field__c');
sobject record = entType.newSObject();
record.put(entField,'12345');
insert record;

In short, you will have to decouple your code from any static references to the object. This is a time-consuming process, but you could always develop a utility class to handle managing the creation and manipulation of the object. Visualforce will also need dynamic bindings. You can achieve this through the use of String.valueOf(entField) to bind to, for example, {!record[entFieldString]}.

neao18neao18

Hi ,

 

The methord you provided is not correct it is getting me methord not found error.

 

So I used this:

 

Public String sObjectName = 'xyz__c';
Public Schema.SObjectType t {get;set;}
Public SObject sObjectDynamic {get;set;}
 
public void newDynObj(){
 t = Schema.getGlobalDescribe().get(sObjectName);
 sObjectDynamic = t.newSObject();
}

 

 

But now how can I initilize a List of sObject like:

 

OLD:
 
List<xyz__c> nlst = new List<xyz__c>();
 
New:
 
List<sObject> nlst = ????

 

 

neao18neao18

Hi sfdcfox ~

 

I am working on the second option but here is a query:

 

I have made all the reference as sObject : List and objects all and only reference ob the object is in string form but I didnt have to use the above code as coz I am just querying the object here and displaying the fields in VF.

 

But still when I create the package it includes the xyz__c object!!!

 

I checked the VF page it dont have any reference to xyz__c object it has customcontroller.

 

What to do in this case??

sfdcfoxsfdcfox
You can use List<SObject> or SObject[], either will work for inserting/updating/deleting your xyz__c object.

To use queries, you will have to use dynamic queries, such as:

Database.query('select id,name,field1__c from xyz__c where ...');

Be aware that you may have to dynamically build your query strings to include just the fields you need and/or the filters. To be clear, there must be absolutely zero references to xyz__c outside of a string, or it will be pulled in.
neao18neao18

Yes that I have done but the problem is that it (xyz__c) has a lookup to Lead and so when I include the other VF page in the package that has a standardcontroller then the xyz__ obj is automatically included.

 

Also if I remove the visualforce page but my test class is inserting a Lead and xyz__c is related to that object then also the object is included in my package. 

 

Also if I remove the lead insert from my test class then how can i get the data in query inside my class?

 

Am I correct??

neao18neao18

Hi sfdcfox~ 

 

I got it and have leadrned some new stuff. I will be posting the complete guide for that if any one need it.

 

neao18neao18

Hi All,

 

I am posting my learning and founding on this topic to help every one who is in need..

 

Topic: How not to include a object in a managed packadge:

 

Senario : Some times we have a senario that we may have a certain custom object that is already present in the installing org so we need not to include the reference of that object in our package.

 

Solution:

I am assuming that there is a visualforce page with standard controller and a VF component with a controller.

1. Make sure that if a standard controller is used then all its related objects (Child Side) will get a reference in the package because the standard contoller has a static reference to the object with all the related objects taking in account.

2. Make sure to remove all the static references of that objects from your code use dynamic sobject.

3. Donot use any static query in the code and any static object reference.

4. Donot take the visualforce package that has the standard controller into the package.

 

Mark as solution if this helps!!