Salesforce Development with Java: Generating Java Stub Files

I’m wondering if there aren’t many people in the world like me, who sometimes solve Salesforce problems using Java console apps, but aren’t really “Java developers.” I’ve used Java for such things over the past few years because I can use it without expensive IDE licenses (in other words, I don’t have to buy a full license of Visual Studio or fight with lesser tools using C#). Java is enough like Apex that I’m comfortable enough with it, and there’s lots that can be Googled to get value out of the Salesforce SOAP, metadata, and tooling APIs using Java. That said, every so often, I find myself at a new job, or needing to upgrade, and I have a HORRIBLE time figuring out how to generate Java stub files from the Salesforce WSDLs and the Salesforce Web Services Connector (WSC). I’m going to try to write up what I just did this time around so 1) I’ll have the steps for myself the next time I find myself trying to figure this out, and 2) hopefully someone else in the world will get some use out of this. Here goes…

  1. Go straight to Maven Repository for the “Force WSC” project and pick up the latest uber binary for the WSC.
    • PLEASE NOTE: Make sure you get the uber binary. To do that:
      1. Click the API number in the “Version” column, which is a link. You’ll probably want the one at the top, which should be the most current API version available.
      2. On the revealed page, look for the “Files” row in the table toward the top, and click “View All.”
      3. In the long list of files, find the one with a name like: force-wsc-51.0.0-uber.jar
        • The numbers in the filename will reflect the API version of the binary selected above.
  2. Put that jar, and the WSDL file you generated from Salesforce (if you need help with that, just Google it; lots of people have written up how to generate the WSDL), into the same directory.
  3. Open that directory in a command line tool (I use Git Bash or PowerShell, but use what you’re comfortable with).
  4. Run the WSC jar / build your stub file.
    • You’re going to see lots of syntax for this all over the Internet. You’ll see blogs referencing a need to have various dependencies downloaded and included in your classpath when you try to run the jar (StringTemplate, Rhino, etc.). I think the developers making the WSC are, these days, including everything you need in the “uber” WSC jar. So, try this syntax (update the API version and file names):
java -classpath .\force-wsc-51.0.0-uber.jar com.sforce.ws.tools.wsdlc .\wsdl.jsp.xml .\partner.jar

That worked for me. I’m hoping this helps other Salesforce developers with “beginner-to-intermediate” Java skills like me to figure this out.

Screenshot 1: The Maven Repository view where you click the API number link…
Screenshot 2: The Maven Respository view where you click “View All” in the “Files” row to find the “uber” jar…
Screenshot 3: The list of downloadable binaries…

SOQL Relationship Query / Relationship Names for Custom Object (SObject) Share Table

I haven’t posted anything to this blog in years.  I’ve never been much of a blogger, or good at maintaining my “digital presence.”  But, I just had another one of those moments I think all us Salesforce architects / developers / admins / etc. have semi-frequently in which I searched the Internet and read results for hours for something I thought would be easy to find, but couldn’t find it.  So, here goes a write up.

My goal was to keep my SOQL query count lean in a context in which I needed to get a list of custom object records, and all the shares for each of those custom object records.  I thought to myself, “I can probably do this with a relationship query into a list of the custom object type.  I’ll just have to figure out what the relationship name is for the lookup field on the custom object’s sharing table.

Spoiler for those who just want the answer:  “Shares” is the relationship name you’re looking for.

I’ve been working with the Apttus CPQ and CLM tool sets of late.  To get specific, I was trying to query up Apttus Agreements and their related shares.  Now, what I learned here will apply for ANY custom object / sObject in Salesforce, but what follows here is going to reference an Apttus object / namespace.  I started out trying something like this:

List myAgrList = [ SELECT Id, OwnerId, (SELECT Id, ParentId, UserOrGroupId, AccessLevel, RowCause FROM Apttus__APTS_Agreement__Shares) FROM Apttus__APTS_Agreement__c ];

That wasn’t right.  I tried making the relationship name “Apttus__APTS_Agreement__Share” because I thought I could remember some standard objects not following the plural convention.  I tried a lot of other things, and looked in a lot of wrong places.   I asked myself if it had something to do with this being a managed package object and the related namespace. I tried “Parents” and “Parent” since that was the relationship name I saw for the ParentId field on the sharing object.  I

Finally, it was going into Workbench and looking at a relationship I know best…Accounts and Contacts…that led me to the answer.  I looked at the Contact table definition in Workbench, and saw the “Child Relationship” folder.  I saw there was a sub-folder called “ContactShare.ContactId.”  In that folder, I saw the relationshipName value:  Shares

So, I went to the Apttus__APTS_Agreement__c object in Workbench.  I went to the “Child Relatiohships” folder and found a sub-folder called Apttus__APTS_Agreement__Share.ParentId.  In that folder, I was the “relationshipName” attribute also has the value:  Shares

So, there you go.  Hope it helps somebody else.  If you want to write a SOQL relationship query for Share table children, for a standard or custom object, with namespaces or without, try relatiohshipName:  Share

Rounding Datetime Subtractions to the Quarter Hour in Salesforce Formulas

I’m proud of this one, so you all get to read about it.  As part of a study group I’m in at the moment, I was given an assignment to set up a little app in a Salesforce developer org that includes a time logging component.  I found myself wanting to subtract two datetime fields, display the result out to the hundredths place, and round to the nearest quarter hour.  I searched the Internet a bit looking for a formula that would do the job, but didn’t stumble across anything.  So, I decided to spend a little time and see if I could figure it out myself.

So, I knew I wanted to start with something like this:


(End_Date_Time__c - Start_Date_Time__c) * 24

That will give us the number of hours between the start datetime and end datetime.  But, I would get values like 2.27 if a session ran a minute or two over two hours and fifteen minutes.  I realized I needed to do something with a modulus function and handle the remainders.  I got myself a little confused, though, mainly around two points:

  1. If you want to use a decimal value as the divisor in the MOD() function in Salesforce’s formula language, make sure you preface your “.” with a “0.”  So, “0.25” will work, but “.25” will not.
  2. Remember that, when you use the modulus function, the remainders you get back are in “units,” if you will, of your divisor.  So, in my case, I got a little confused because I was forgetting that the returned values were going to be between 0 and 0.25.

After a little experimenting, I realized that the following formula would give me results rounded DOWN to the quarter hour:


(End_Date_Time__c - Start_Date_Time__c) * 24) - MOD((End_Date_Time__c - Start_Date_Time__c) * 24,0.25)

That formula basically takes the hour value to two decimal places, and subtracts the remainder obtained by taking the same value and dividing it by 0.25.  When I figured that out, I thought, “Well, for my imaginary company in my exercise, maybe they always want to round down to the quarter hour.”  But, that seemed like taking the easy way out, so I sat down to do the little bit of extra thinking for rounding to the NEAREST quarter hour.

Since the remainders coming back were values between 0 and 0.25, I knew I wanted to round down if the value was less than 0.125 (the “half-way mark”), and round up if the value was greater than or equal to 0.125.  So, I added an IF() function to frame the two possibilities, and used the formula from above as the case if the remainder was less than 0.125.  Then, for the “else” case in my IF(), I realized that for the “round up” side of the formula, the remainder represented how much of the next quarter hour had been worked by the staff person.  So, by subtracting that remainder from 0.25, I get the amount I need to ADD to the datetime subtraction to correctly round up to the next quarter hour.

The final formula looks like this:


IF(
MOD((End_Date_Time__c - Start_Date_Time__c) * 24,0.25) < 0.125,
((End_Date_Time__c - Start_Date_Time__c) * 24) - MOD((End_Date_Time__c - Start_Date_Time__c) * 24,0.25),
((End_Date_Time__c - Start_Date_Time__c) * 24) + (0.25 - MOD((End_Date_Time__c - Start_Date_Time__c) * 24,0.25))
)

I hope someone out there gets some use out of this.  I figure I can’t be the only person using Salesforce who wants to round to the nearest quarter hour with a formula.

I’ve Gone Pokémon on Trailhead

I love Trailhead.  Salesforce has applied gamification to learning their platform, and it’s brilliant.  Billed as, “the fun way to learn Salesforce,” on the Trailhead site, users can pursue learning Trails, made up of Modules and Projects, that tailor the learning experience to various domains by which one can filter content.   For example, you can select “Admin” and “Beginner” in the filter list, and start going through all the training modules tagged as such.   I’m doing that right now, as I’ve begun preparation for the Admin 201 certification exam.  I’ve decided to pursue the Administrator certification track at this point.  It’s been a little over a year since I completed the Dev 501 process, and I’m ready to get back into the certification effort (I needed a break after that programming assignment).

But, here’s why I say, “I’ve Gone Pokémon on Trailhead:”  I’ve “gotta catch ’em all!”  Giving out these badges in the gamified context is a great motivator for me, it seems.  It makes cracking open these Trails, Modules, and Projects on Trailhead a lot of fun!  That’s especially helpful since a lot of the content I’m going through right now is review.  I’ve been using Salesforce for about 4 years now, and I’ve gotten familiar with a good bit of the admin functionality.  That said, I’m learning something in almost every Trailhead module I complete!  For instance, I set up Account Teams and used Contact Roles in the “Accounts & Contacts” module this morning.  My company doesn’t use those features extensively, so I’d never had reason to play with them.  And, after finishing that experience…BADGE!

I’ve also got a project for work right now in which I’m probably going to have to figure out an integration with an external partner, and I want to use Apex web services to tailor GET / PUT requests for RESTful web service calls between us and the partner.  Recently, a new “Apex Integration Services” module showed up on Trailhead.  I ran through it, got a great refresher course on using SOAP and RESTful web services for the Salesforce API,and also how to write my own web services with Apex.  And then?  BADGE!

Oh, the joy!  The endorphin rush!!  I love getting Trailhead badges!!!

I discovered Trailhead at Dreamforce last year.  Since, I’ve learned it’s a great resource, and I’m trying to motivate my team to take advantage of  this free training option.  If you haven’t experienced Trailhead yet, get over to https://developer.salesforce.com/trailhead and try it out!

Large CSV Files with Millions of Records: Delimit Rocks!

This past couple of weeks, I’ve been working furiously with a colleague to meet expectations around some data extract work for a divestiture.  My colleague wrote a PowerShell script to pull data, but we were getting heap space errors when we tried to extract all the Task records we needed to pull with the conditions we had in place.  We were pressed for time, so I went looking for a fast solution.

A while back, I’d learned of a tool that would let us open large CSV files in an Excel-like interface and work with those files.  The software is called Delimit, and is made by Delimitware, and you can read all about it at http://www.delimitware.com/.

Now, what I want to share with my Salesforce friends out in the world is that Delimit is AWESOME.  I was able to open a CSV with 1.2 million lines, and then extract/filter out 106,000 or so of those rows based on the 17,000+ IDs in a column from ANOTHER CSV I opened in Delimit.  Less than a minute after I kicked off the extract/filter, I had an extract CSV file with the filtered records I wanted.  The formula-based way to accomplish such a CSV filtering by another CSV is documented in Delimit’s help, here.

The software is reasonably priced, and the vendor worked with me around a licensing model that worked for my department.  If you’re looking for a solution for dealing with Salesforce export CSVs that contain millions of rows, check out this software.  And, do yourself a favor and do what I did (out of shear luck):  Buy a license for this before the emergency in which you really need it!

Reboot: Or, How to End a Blogging Hiatus…

Well, my grand experiment around a professional blog clearly stalled, but I think I’ve got a pretty good reason.  I was hired as a Salesforce Architect for a great company, ACI Worldwide, in November 2014.  I just had my one year anniversary.  It has been an INTENSE year.  We have covered a lot of ground, including:

  • Standing up a third-party Salesforce backup solution (Backupify) in the cloud
  • Migrating data from a Salesforce instance owned by a company ACI acquired into ACI’s Salesforce instance
  • Rolling out a new deployment process (which is still evolving)
  • Studying new integration strategies around a pending move of a back end system from on premise (Oracle 11i) to the cloud (Oracle Fusion).
  • Designing several new solutions on the Salesforce Platform.

I’m going to try to make an effort to start my mornings blogging about some of the experiences I’ve had in the past year as the technical lead of this remarkable Salesforce development team I’ve been privileged to join.  For now, suffice it to say it’s been an amazing year, and it looks like we’ve got some cool things on the horizon as well!

Salesforce Weekly Export Service: Beware the Start of the NEXT Scheduled Export…

Salesforce provides a “Weekly Export Service” for customers with Enterprise, Performance, and Unlimited Editions.  I expect that many of us aiming to use this service as a Salesforce automated backup solution find our way to this “Exporting Backup Data” page in the Salesforce Success Community.  Very clearly on that page, one can read the following about how long those backup files will remain in place once they arrive upon backup completion:  “Zip files are deleted 48 hours after the email is sent. The 48-hour time limit doesn’t include weekends, which means if your download file is ready on Thursday at 4 p.m., that file isn’t deleted until Monday at 4 p.m.”

However, that’s not always the case.  I recently started a new job with a company that’s been using Salesforce for 7 years, and the org has A LOT of data; so much, in fact, that it took 12 days for the first full “weekly export” to arrive.  At least, I THINK it took that long; let me explain why I’m uncertain.  What I learned yesterday is that the initiation of the NEXT export job results in deletion of the backup files from the PREVIOUS export job.  So, I got an announcement at about 10:00 AM on Saturday, 12/20/14, that my export was complete.  On Monday morning, the FIRST business day after the announcement of the export’s completion, I started downloading files at about 8:30 AM EST.  At about 10:00 AM EST, the backup files, all 241 of them, disappeared.  I’d downloaded about 4.

When I called in to Salesforce Premier Support to rave that I should have almost two full business days left to download these files, the rep explained that it wasn’t the 48 hours over business days schedule that resulted in the deletion of the backup files.  It was, indeed, the next export job commencing that caused the deletion.

The solution I was pitched was to delete the weekly scheduled job, and allow the currently executed job to complete.  Then, when that job finishes and my team has downloaded all the files, we can manually kick off our next export.  That should prevent this problem moving forward, but it adds another point of opportunity for human error in maintaining the backup process, about which I’m not thrilled.

The truth is, this discovery is just one more factor that is increasing the urgency of figuring out an automated backup solution for our Salesforce org.  We need a frequent backup to protect us against data losses / corruptions caused by human error.  Yes, Salesforce backs up data for “disaster recovery purposes,” but make sure you read up on what that means.  According to Salesforce, it costs a minimum of $10,000 (US) and “and usually takes a minimum of 15 business days (3 calendar weeks).”  I don’t know about your company, but 15 days waiting for a data restoration in any emergency that would justify a $10,000 fee would be WAY too long to wait for us.

I’m curious about what other users with large amounts of Salesforce data have done to automate a frequent backup process.  I’m looking at scripting with Salesforce DataLoader, I’ve started experimenting with the Jitterbit Data Loader, and I’m open to just about anything.  If you have any thoughts on the subject, I’d love to get a comment from you here.  Thanks in advance!

Salesforce DataLoader Error: The sobject initialization failed…

I encountered a really strange issue today when trying to export data from Salesforce using DataLoader.  In Step 2 of the export process (the “Select Data Objects” step), for many of  the objects in my company’s org, I was getting an error message that read, “The sobject initialization failed.  Please try again,” when I would select the object and then clicked, “Next.”  I tried everything I could think of to fix the problem.  I checked object access levels in Salesforce.  I tried uninstalling and reinstalling DataLoader (version 32).  I tried restarting my machine.   None of it worked.

Then, I tried another machine that had DataLoader installed.  It was version 30, and it worked.  Sadly, though, the version difference was a red herring, and had nothing to do with the problem.   I installed version 32 on that other machine, and it still worked.  So, that led me to think their was some issue on the other machine preventing DataLoader from properly initializing some SObjects.

My Google searches for the error message didn’t yield much, but I did find my way to a labels.properties file in the GitHub space for the DataLoader project.  In that file, I saw the error message I’d been searching for on Google:  “The sobject initialization failed. Please try again.”  That made me think, “Hey, DataLoader is built in Java.  I wonder if this failure has anything to do with my Java installation.”  I actually have multiple versions of Java on the machine.  My system path had both of the following references:

C:\ProgramData\Oracle\Java\javapath;
C:\Program Files\Java\jdk1.7.0_71\bin;

I thought to myself, “I wonder if the multiple versions of Java are messing things up.  So, I removed the second of those two entries from my path variable, restarted DataLoader, and the I was then able to get past the point where I had been getting that error message.

Here’s the funny part, though.  After I had succeeded, I put my path variable value back to what it had been previously.  I had pasted it into Notepad++ so I could restore it afterword.  I closed and reopened DataLoader…and I could still initialize the SObject that was failing before.   So, I restarted my computer, confirmed that the path was what it had been originally…and I STILL didn’t get the error.

Frankly, I can’t explain exactly what’s going on here.  But, changing the path entries for Java as I did seems to have resolved the problem. If you encounter this problem and have multiple versions of Java, you might try removing one of the installation paths from your system path variable.

For reference, here are the Java versions on that machine:

Java 7 Update 67
Java 7 Update 71 (64 bit)
Java 8 Update 25
Java SE Development Kit 7 Update 71 (64 bit)

I now post this in the hope that it will save some developer / administrator out there in the world the hours of troubleshooting I just went through…

I Passed the Salesforce.com Certified Force.com Advanced Developer Programming Assignment and Essay Exam!!

It’s been awhile since I made a Salesforce post.  I’m so happy to be able to make this one.  I feel so grateful, blessed, and humbled right now.  About four weeks ago, I was given an amazing new job opportunity with a company called ACI Worldwide as a Salesforce Architect.  They brought me in knowing I had a lot to learn to really live up to that title, but that I was passionate about the Salesforce Platform as a Service, and want to learn as much as I can about anything and everything that can be done with it.  I’m very excited about this chance to learn, but it’s pretty intense right now as I’m surrounded by a new business context and a lot of new ideas.  Tonight, right when I could use a bit of a pick-me-up as I’m working hard to learn fast and add value quickly in the new job, I found out that I PASSED the Salesforce.com Certified Force.com Advanced Developer Programming Assignment and Essay Exam on my first attempt!

I couldn’t have done this without all the encouragement I received from my former colleagues at The MENTOR Network, in particular Kate Miller, Shaun Wood, Bridget Samuel, Paul Perisic and Jeffrey Cohen, among many others.  They may never see this post, but I want to record my gratitude for the encouragement and assistance they provided as I was learning Salesforce and diving in to the certification process.  And, frankly, I want to thank Dan Appleman and Jason Ouellette.  Their books on advanced apex and Force.com platform development, respectively, are the resources that I think most helped me make the transition from intermediate-PHP-MySQL-Web 2.0-self-taught developer to someone able to pass this programming assignment.  Thank you to the review panel who graded my programming assignment for their comments, including the constructive criticism included therein.  Mostly, though, I want to thank my wife, who pretty much got used to me getting done with work, grabbing a sandwich, and going right back to my home office for most of the month of October 2014.  She picked up a lot of slack as I was focusing on the programming assignment, and I’m very grateful.

Now, back to that constructive criticism from the review panel, since anyone prepping for the Advanced Developer certification process who actually found their way to this blog post from a search engine result deserves at least an attempt from me at providing helpful information.  I don’t even want to brush the edges of sharing any proprietary information about this certification programming assignment because I worked WAY too hard to risk losing it for an ethics violation or anything like that.  So, I’m going to have to speak in pretty generic terms.  But, what I feel comfortable saying is that the comments really weren’t that unexpected, and I think they were very fair (even the negative ones, which aren’t ever really easy to get, but I’m focusing on the chance to improve that they represent!).  They focused on Salesforce best practices, that are documented all over developerforce.com, the success community, etc.  There was one comment that I should have expected.  When I was approaching one piece of the assignment, I thought to myself, “I wonder if I can do this with [a piece of standard Salesforce functionality] rather than with this cool, but perhaps over-engineered, chunk of code I’ve written.”  If you find yourself thinking something like that, stop, and go read the pertinent documentation!  That thought leads me to the two pieces of advice I would give to anyone preparing for this certification:  1)  Read Development with the Force.com Platform by Jason Ouellette and Advanced Apex Programming by Dan Appleman, and 2) remember that you’re being tested on THE SALESFORCE PLATFORM.  As you’re designing your solution, think about what Salesforce has done to make certain things easy for you as a developer, and make sure you’re not writing code to solve problems they’ve already solved.  I was “guilty as charged” on that count for one thing, and I’m going to go back and think my way through the “better way” that was described in my comments.  Thank you again for the feedback, review panel, whoever you are!!

Actually, one more thing:  The certification website and other sources on the web have language like, “Plan at least 20 hours for the programming assignment, but be ready to possibly spend more to do a good job…”  I guess I have a third piece of advice for folks going after this one:  Unless you REALLY know what you’re doing, I’d at least double that 20 hour estimate.  I read the assignment the first night, read it again the next day, did some thinking / planning / design, then started coding.  I think, in the end, I put in more like 50 hours.  I grant you, some of that was in obsessing, reworking a bad design choice I caught last minute, etc.  I also seriously underestimated the amount of time it would take to write test code for EVERY permutation I could think of for use of the simple interface, and truthfully, I left some positive and negative use cases untested simply because I ran out of time.  I think I got to 98% or 99% code coverage, and I’m proud of that, but the funny thing about getting code coverage that high in this context is that the handful of lines I couldn’t cover DROVE ME CRAZY!!!  There was one line of Apex code I tried to cover about a dozen different ways, and finally had to give up.  I was SURE it was possible to cover, but for some reason my usual bag of tricks wasn’t cutting it.  Ah, well; I’m proud that I was more focused on testing every use case I could think of than just getting the code coverage.

OK, call it FOUR pieces of advice, since I think this is just restating a Force.com development best practice and doesn’t give anything away about the assignment:  Don’t be lazy; actually DO the “worst case” analysis we all know we should always do to determine if a solution will scale BEFORE you start coding.  I caught a design flaw in my assignment two nights before it was due because I went back to the beginning of the prompt and decided to think my way through the whole thing one more time.  Had I not done that and caught the issue to which I’m referring here, I’m almost certain I would have failed.

Good luck to those of you pursuing this certification!  It’s hard work, but it feels great when you pass, so go for it!!

 

dev copy dev_adv

I just turned in my Salesforce.com Certified Advanced Developer Programming Assignment!

A four-week long adventure has just culminated in the submission of my Salesforce.com Certified Advanced Developer Programming Assignment.  I’m still processing at this point, but I feel pretty good about what I turned in.  There are parts of it that are inelegant, perhaps.  And, there are some things I’ve been obsessing about in terms of design and trade offs that I can’t get out of my head at the moment.  But, I’m proud of what I did.

I can’t go into specifics about the assignment, as the certification process requires confidentiality on the content.  However, I can say I’ve learned more than I would have thought possible in the past four weeks in the course of figuring out this assignment.  It was really a great experience for me to have this assignment, that I knew was going to be reviewed by advanced developers I don’t know, and to have to do the worst-case analysis and design up front in order to carefully account for resource consumption as this application scales.  I used a SOQL relationship query more creatively than I have in the past in a pivotal part of my code, and I’m proud of that.  I also used a multi-dimensional list of a wrapper class to good effect; something I don’t think I would have been able to do a few years ago.  I also think I got the AJAX calls required/implied for the assignment handled pretty well.

My testing was fairly inelegant, and I know it.  It was very “brute force,” not very object oriented, and was resource-intensive in terms of query count and DML operations.  I think I have a lot of room to learn and grow there.   I did centralize creation of object records in my test setup, and I feel good about that.  I’m not 100% sure I caught every bug in a trigger-initiated method call for calculating and updating some figures, but I think I did perceive the “worst-case” mistake the prompt was dangling in front of me in that context, and although the method I constructed was more complex than the “easy solution that would fail when it scaled,” I think I got it 90% correct or better.  At work, I would have had a team to test it with me, users trying it out in our staging environment, and the opportunity to patch / hotfix the thing if we had problems.  In this context, I get one shot.  I think that’s a good thing.  I could use more practice in “you have to get it right the first time” situations.  Yeah, I haven’t slept much the last week in going over and over the code, and testing every permutation I can think of, but I got a better app as a result.

I don’t know if I’ll pass and get this certification on this first attempt at the programming assignment.  But, I’m glad I tried.  I’m a better developer for all I’ve learned this past four weeks, and even if I have to tackle the assignment portion again, I’ll be approaching the next attempt having learned all these lessons.  I should know in 8 or 9 weeks.  Wish me luck!