4

I'm working on an internal code library for our company, and my main requirement (which I defined myself) is that the code that you check out from the library code repository will be disconnected from that repository to become part of the project once you do the check out. That is, you want to be able to commit the library to the project's repository.

I have created the library as a Salesforce project, so that if someone wants to make changes to the library they can check it out as an independent project and make changes this way.

My initial idea was to use a build tool (Ant, Maven, Composer...) to retrieve the library code from the repository and add it to a project this way. However, it turns out that none of these build tools support checking out the project from a Git repo (what in svn is an "svn export") without having it become a working copy. And our code repository (Atlassian's Stash) does not support pulling down the code in a tar or zip file. Oh, and we do not have an internal Maven repo, so that's not an option either.

So I have decided that I'm going to release the library as a managed package. I'm facing some issues here as well, which is what I wanted to ask the community about (although I'm open for suggestions in what I have covered so far).

The problem with my managed package is that I'm not being able to use the classes in the package from my test project's Apex code. I have made the library classes and methods global, and I have successfully installed the package in my test org. I can see the classes (though not their content of course, since it's a managed package) under the "Referenced Packages" section of my project. But when I get an "Invalid Type" error in the Force.com IDE when trying to instantiate one of the classes in the library, no matter I use the prefix or not. That is, either of these lines of code gives an error:

SoliantGenUtils__Emailer emailer = new SoliantGenUtils__Emailer();
Emailer emailer = new Emailer();

Even though in the installation details I can clearly see that it says "SoliantGenUtils" under the "Namespace Prefix" column, in the "Installed Packages" section.

At this point I'm a little confused. I have worked with managed packages a lot, but I guess I have only referenced classes in a managed package from code also inside the managed package. But I thought you could reference it also from outside the package. Isn't that one of the uses of the "global" keyword?

Any suggestions will be appreciated. Thanks

PS: Another option could be to use an unmanaged package, but that's not good because those cannot be versioned, and obviously we want the library to have version numbers

  • I see your question has been answered, but I'd also question the value of using managed packages for internal projects. I find the limitations and design of managed packages fairly annoying, to the point that I would only use them for code that is designed to be installed over the AppExchange. If version numbers are one of the primary drivers, you could do something like include auto-generated version numbers in class headers or something. The inability to delete old files and other common types of refactoring, as well as the time to upload+install, in particular get annoying. – jkraybill Apr 29 '13 at 4:35
  • I'm happy to get feedback on this area as well. How would you propose moving the code from the code repository to each project? Are you using a build tool? Or are you suggesting using an unmanaged package? – ceiroa Apr 29 '13 at 13:36
  • Yeah I'd suggest using an unmanaged package. The restrictions on managed packages aren't worth it for internal use, in my opinion. I've used various types of scripts to do this in the past, usually just basic automation of the Ant migration toolkit. – jkraybill Apr 30 '13 at 0:58
7

The syntax for referencing an apex class in a managed package is different than SObjects. Instead of a double underscore you'll need to use a period, as in:

SoliantGenUtils.Emailer emailer = new SoliantGenUtils.Emailer();
| improve this answer | |
  • Doh, I was so focused on the version settings I missed the double underscore rather than period. – Daniel Ballinger Apr 27 '13 at 7:35
  • 1
    This was the final tweak I needed, but thanks to Daniel for uncovering the package reference from the class issue. The reference to the package did not appear until I edited the class from the web UI (Setup > Develop > Apex Classes > CodeLibraryTest > Edit). – ceiroa Apr 27 '13 at 15:41
2

Check that the Version Settings tab for the Apex Class is referencing a managed package version where the Emailer class is global.

Version Settings for an Apex Class

I assume the constructor is also marked global along the methods you want to invoke.

You will need to use the managed package namespace prefix when calling into the installed managed package.

| improve this answer | |
  • Both Emailer and the class I'm trying to call it from (CodeLibraryTest) show the same: Name=Salesforce.com API, Version=27.0, Namespace Type=Salesforce.com API. I'm trying to call the class with the namespace, as SoliantGenUtils__Emailer. What is it that I'm missing? – ceiroa Apr 26 '13 at 19:39
  • And yes, the constructor is marked as global too. – ceiroa Apr 26 '13 at 19:43
  • I've had a similar issue with managed packages and Invalid Type - Managed package install fails in a single customer Org. My case was a bit different, but the org didn't have an expected feature enabled. Are you able to share the contents of the Emailer class? – Daniel Ballinger Apr 26 '13 at 19:46
  • I see what you mean now. But no, the class that is trying to call the managed package class does not have a reference to the installed package. Neither do I see the drop-down to add a reference to the Installed Package that you have in your screenshot. How do I make that appear? – ceiroa Apr 26 '13 at 19:53
  • Are you viewing the Version Settings tab when editing the class? – Daniel Ballinger Apr 26 '13 at 20:06

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.