5

I have some problem with package dependency. This is the scenario. There is a package named as "Package B". In my class I want to check if "Package B" exist. If it exist, I will create new record on one of the object that belongs to "Package B" or modify some data in it. If "Package B" does not exist, the code will still run. The thing I want is that when I create my own package, I don't want my package to be dependent with "Package B". I wan't it to be an independent package. Is this possible ? Because I believe everytime you use any field or object from the "Package B" it will make my package dependent. If yes, can you please give me an idea how to do this ?

7

I have never tried this before. But using dynamic objects you can do this. Here are idea to get you started.

Store the package details and object, field in some custom setting or custom label. Now make a dynamic SOQL to query package details. If you found the details then create dynamic instance of sObject and create new record of it. As you are not direct referring it so it will work and your package will not be dependent.

You can create dynamic sObject

sObject sObj = Schema.getGlobalDescribe().get(ObjectName).newSObject() ;

or a static method

public static sObject createObject(String typeName) {
        Schema.SObjectType targetType = Schema.getGlobalDescribe().get(typeName);
        if (targetType == null) {
            // throw an exception
        }

        // Instantiate an sObject with the type passed in as an argument
        //  at run time.
        return targetType.newSObject(); 
    }

and put field value

s.put('fieldAPIName, '12345');
  • 2
    Can confirm that this approach (plus always using dynamic SOQL) works. It does require care to avoid accidentally introducing a static dependency (later on by a developer who isn't aware of the need to avoid the dependency) and it is not possible to e.g. (directly) call a global API in package B from package A. – Keith C Nov 1 '17 at 8:32
  • 1
    @Tushar Sharma Thank you so much for this. Just a precaution, If I did some code that will make my managed package dependent to "Package B", can I remove the dependency by removing the part of the code that makes it dependent ? or once it is dependent it will always be dependent even if I remove some part of the code ? – Hope Nov 2 '17 at 1:55
  • 1
    @Hope I think it will remove dependency. If it does not then you need to raise a case with SF support and they can help you. KeithC Thanks for suggestion and inputs. – Tushar Sharma Nov 2 '17 at 5:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.