Thursday, May 10, 2012

Salesforce and Grails...

Salesforce integration - it's been a while since I have tried this. I have been thinking about this for a few days about how to integrate Grails is a halfway decent way with Salesforce. It's somewhat surprising there isn't a plugin already for this. After being annoyed with Apache CXF Client for Grails all morning, I sought out to find a better solution.

I did however find a pretty slick library called 'sf-api-connector' by a group calling itself 'TeamLazerBeez'. So far it's working, and is nicely arranged. Instead of installing a bunch of stubs and dealing with WSDL and whatnot (I'm trying to use the metadata API, sorry Soap only ugh!).  After you build it, it will install its jars in your local maven repo (I'd recommend publishing on your company's Artifactory or other maven server instance, because these are not hosted on a public repo). From there I just had to add the dependencies to BuildConfig.groovy, and take care to exclude the 'stax-api' from the dependencies (otherwise it conflicts with the built-in JAXB classes in Java6 or later).

Like so:

        compile ('com.teamlazerbeez:sf-soap-api-connector:trunk-SNAPSHOT') {
            excludes "stax-api"
        build ('com.teamlazerbeez:sf-soap-api-connector:trunk-SNAPSHOT') {
            excludes "stax-api"
        test ('com.teamlazerbeez:sf-soap-api-connector:trunk-SNAPSHOT') {
            excludes "stax-api"
        runtime ('com.teamlazerbeez:sf-soap-api-connector:trunk-SNAPSHOT') {
            excludes "stax-api"

Now, I am trying to figure out why, following their example, I keep getting logon denied. First, I made sure there is a 'Allow API' permission added to my salesforce role in the admin console. Second, I make sure I have an API token generated for my account. Third I make sure I append that token to my password login credentials. Still, no luck - wait I am in the SF Sandbox - How does it know that?

Well I had to dig through their code to find it, but here is an example to make a connection to the sandbox(This is Groovy, so yes this runs!, just make up something for orgId):

ConnectionBundle login(String orgId, String userName, String password)  { 
ConnectionPool<Integer> pool = new ConnectionPoolImpl<Integer>(orgId)
        //TODO use Config.groovy property to switch between Sandbox and regular mode
        pool.configureSandboxOrg(orgId, userName, password, 20)
        ConnectionBundle bundle = pool.getSandboxConnectionBundle(orgId)
        MetadataConnection metadataConn = bundle.getMetadataConnection()
        // metadata connection
        List<FileProperties> fileProps = metadataConn
                .listMetadata(Arrays.asList(new ListMetadataQuery("CustomField")))
        for (FileProperties fp: fileProps) {
            System.out.println("Custom field " + fp.getFullName() + " has id " + fp.getId())
        return bundle

There ya go. If everything is good, you should see on your console a bunch of custom properties from your Salesforce instance. Go here for more info

No comments: