Determining Salesforce Server Pod and if Sandbox via Apex

Recently, I’ve developed some apex classes that were doing callouts to external endpoints like CastIron and Worldpay. I was developing those in a sandbox and from that sandbox called to test endpoints. However, when that code was migrated to production, we obviously wanted those to point to the production endpoints. At first, I used Custom Settings to hold the endpoint value, but I had to change that each time I refreshed. I wanted to store both the test and production endpoints in the custom settings and have the code determine which to use based on if it was being called in a Sandbox or not. Unfortunately, there isn’t a IsSandbox() method in Apex. So, I figured out my own.

I started off using the X-Salesforce-Forwarded-To request header listed in the pagereference documentation because it gave me a consistent value for users accessing Salesforce via the standard interface, Partner Portal, and Sites. However, because you’re using ApexPages.currentPage(), this only works if the code is called from a page controller or extension. In my tests, I tried using URL.getSalesforceBaseUrl(), which can be called from regular classes, but it was inconsistent what was received. For example, I tried calling GetHost() and GetAuthority() from the system log and got cs12.salesforce.com. But with with internal and portal users, a visualforce page returned c.cs12.visual.force.com. Conversely, when using sites I received SiteName.SandboxName.cs12.force.com. So, my approach was determine which method to use based on where the call was coming from in order to get consistent results. 

Parsing out the domains gave me the exact pod that I was on (ex NA1 or CS12) and from that I could determine if that pod was a sandbox or not from whether it started with a “c”. (You can see the full list of available Salesforce pods at http://trust.salesforce.com/trust/status/)

Note: I haven’t tested this with a Site with a Custom Web Address or URLRewriter classes. If you do, please add a comment with your results.

Here’s my code;

 


public String currentPod {
String server;
if (ApexPages.currentPage() != null){ //called from VF page
server = ApexPages.currentPage().getHeaders().get('X-Salesforce-Forwarded-To');
} else { //called via standard class
server = URL.getSalesforceBaseUrl().getHost();
}
if ( server != null && server.length() > 0){
server = server.substring(0 ,server.indexOf('.'));
}
return server ;
}
public Boolean isSandbox {
String pod = currentPod();
if (pod != null && pod.length() > 0 && pod.toUpperCase().startsWith('C')){
return true;
}
return false;
}

Advertisements

Jiujitsu in Buenos Aires, Argentina – Parte Dos

I’ve taken my sweet time in writing the follow up to my first post on researching BJJ in Argentina. In my defense, in the four months since I’ve been back I’ve been travelling like a madman — San Francisco, Boston (twice), Minnesota, and the UK (five times) — and finished deploying a huge Salesforce project. Compounding that, I had a nagging injury before I went to BA that got exasperated afterwards training in Minnesota, so I’ve been off Jiujitsu for the past few months and not happy about it. That made it a bit hard to write anything on the subject. Anyway, I’m feeling better now and getting back on that horse even if the horse is looking at me like ‘Wow! You really let yourself go.’

My original intention wasn’t to visit all the places I researched in my original post. I planned to find one that was close, with a convenient schedule and just go there like I’ve done in Panama and Boston. Since I was working during the days and had some social events in the evenings, I had all the schedules at the ready and ended up going wherever had a class scheduled whenever I found myself with a free moment. Turned out that those all ended up being at different places. I have mixed feelings about this approach; I liked spending more time at a single place. You get a change to know the people there better and they get to do the same. Though, with my limited schedule I glad to visit as many different places as I did. It’s always fresh, new and exciting. So, there’s a trade off.

I was staying in Palemo Soho near Darregueyra and Guatemala, so all the travel times are based on that. Your results may vary.

On a side note, Buenos Aires seems to attract a lot of BJJ players from abroad. I saw a guy with a Roger Gracie shirt in Puerto Madera and another with a Royce Gracie/TeamRoc shirt in a cafe near my apartment in Palermo Soho. In fact, at 2 of the 3 gyms I visited, I ran into fellow New Yorkers.


Sukata Brothers – http://sukata.com.ar 

On my first Saturday, only Sukata and Pitbull had classes and I ended up at Sukata.

On a quiet Saturday morning, Sukata is about a 15 minute ($20 AR) cab and 50 minute walk from Palermo Soho. I attended the 10:30am beginners GI class. The gym is a small storefront with a basic fitness/free weights area in front and a small room for BJJ in the back. No fancy zebra mats here; just a tarp stretched out over the type of thin rubber tiles you see in a weight room. Made a mental note not to get caught in any Judo throws.

The instructor was purple belt whose name I didn’t didn’t catch (Perhaps Luis?). He didn’t speak any English and neither did anyone else in the class of about 10 white and blue belts. No preocupes. Yo hablo spanglish. Plus, arm bar needs no translation. He showed two arm bar escapes that I really liked, a novel side control escape, and a kneebar sub from within half guard that has since become my secret half-guard killer. I do recall thinking whites belts and kneebars are usually a recipe for disaster. Then he busted out a spider guard sweep to a calf slicer. Now I’m thinking, someone here’s going home with a torn ACL and it ain’t gonna be me. We started rolling to a round timer that sounded like a school bell. I kept feeling that panic about being late for something every time it went off.

I was told that weekday mornings are the advanced belts. Unfortunately my schedule didn’t allow for that. Plus, if I’m fighting black belts I want my A game. At 8 am, I’m still working on my Zzz game. Freddy Sukata teaches the Tuesday evening class. $30 peso mat fee. Gym has showers and a locker room.

Sukata1   Sukata2   Sukata3

Gracie Academia Buenos Aires – http://www.graciebuenosaires.com.ar 

Most of the gyms in Buenos Aires have evening classes starting at 8pm (20:00) or even 9pm (21:00). On Monday, I had a late dinner scheduled so, I needed an earlier class. The Gracie Academia at Club Flex had a beginners class at 7pm and advanced at 8:30(ish) so I went there. It was a bit hard to find. There’s a single dark doorway with a staircase leading right up to the gym on the second floor. I must have walked past it about three times. The class is held on the ‘third floor’ which I suspect was once the roof now covered with a thin ceiling supported by scaffolding. This was winter in Buenos Aires and it was about 35 degrees Celsius outside. So, while I appreciated the homage to the open air gyms of Brazil, I secretly wished I could wear socks and not look like a fool.

The instructor, Sebastian, spoke English fluently and was great at making sure I was following everything. In the beginner class, I met a fellow New Yorker from the Upper East Side who was a university student doing a semester abroad. The beginner class had about 20 people but, for the advanced class that dropped to about 7; blues and purples only. That day there happened to be a black belt from Brazil visting BA on business. He was invited to teach the class and taught some interesting sweeps from half guard.

Gracie Academia is about a 15 minutes cab ride from Palermo Soho; $20 peso. No locker rooms or showers that I saw.

Gracie1   Gracie2   Gracie3   Gracie4   Gracie5

Dudu Duarte – http://www.dudubjj.com 

On Wednesday, I ran over to Dudu Duarte during a 1 pm lunch break. Based on the Google map I put together of all the gym locations, I thought it was much further, but it ended up being closer than the previous two. That’s because taxis can go across on Av. Cordoba much quicker than through the local streets. It only cost $10 pesos and was 10 minute cab ride. This was the largest of the gyms I visited with a more modern fitness/free weight in the front and a very sizable mat area in back. It also had really high ceilings and large windows, which made me feel like I was training in a cathedral. For a midday class, there was a rather large group of whites and blues. Included in the mix were a few foreigners; two Australians and another New Yorker, this one from the Upper West Side. Head instructor, Dudu Duarte, taught the class. He also spoke English and mentioned that he had lived in the US for a substantial length of time. He was a really nice guy and had a good approach to breaking down a move into its individual components. He showed us a transition from an abandoned arm bar to a single leg takedown involving a backward roll that at first glance looked complex but ended up being surprisingly simple and effective.

Following class, we rolled. After about 10 fights or so I was exhausted and started getting ready to leave. Of course, this is always when someone asks you to roll. You hesitate for a brief moment while the little angel on your left shoulder says, ‘You’re tired and should call it a day.’ and the devil on your right shoulder say ‘Put on your big girl pants and do it!’  It didn’t help that it was a young woman in her late teens who asked. Even though she was a purple belt, I probably outwieghed her by 70 pounds. Ok, one last easy roll. Mistake. I started off slow and she started off like a starving mongoose attacking a baby cobra. She went knee on belly and, before I could react, spun me into a baseball choke that caught my chin and almost snapped my head clear off my shoulders. Let’s call this one a learning moment … and I got the hell learned outta me.

The gym has a locker room with showers. I don’t recall being charged a mat fee.

Dudu1   Dudu2   Dudu3   Dudu4   Dudu5   Dudu6

For the second half of trip, my wife and I went to Cordoba for the weekend and then San Juan, where she was delivering a presentation at Argentina’s national HIV conference. Cordoba has some BJJ gyms, but I hadn’t been there before and wanted to leave my days for sightseeing. San Juan is a much smaller city with less to see, which left me with some time on my hands. Unfortunately, Google turned up nothing and after a fairly extensive (my wife said obsessive) search around the city, I’m pretty certain there weren’t any BJJ schools there.  If you know of any, please add them to the comments below.

Get your Salesforce project stats using Apex

I mentioned recently that I was working an a rather large project and posted some stats on how many classes, custom objects, etc were involved. Of course, I didn’t count those by hand. I wrote a little script that I run via Execute Anonymous either in Eclipse or in the System Log. Thought I’d share that out.

In the code below I filter out any managed packages because I just wanted stats on stuff that I had done. Feel free to modify the queries to your specific needs.

 


Integer triggerCount = 0;
Integer codeLines = 0;
Integer classCount = 0;
Integer componentCount = 0;
Integer pageCount = 0;
Integer customObjectCount = 0;
for (ApexTrigger t : [Select a.NamespacePrefix, a.Name, a.LengthWithoutComments From ApexTrigger a where NamespacePrefix = null]){
triggerCount++;
codeLines += t.LengthWithoutComments;
}
for (ApexClass a : [Select a.NamespacePrefix, a.Name, a.LengthWithoutComments From ApexClass a where NamespacePrefix = null]){
classCount++;
codeLines += a.LengthWithoutComments;
}
for (ApexComponent c : [Select a.NamespacePrefix, a.Id From ApexComponent a
From ApexComponent a where NamespacePrefix = null]){
componentCount++;
}
for (ApexPage p : [Select a.NamespacePrefix, a.Id From ApexPage a
From ApexPage a where NamespacePrefix = null]){
pageCount++;
}
Map gd = Schema.getGlobalDescribe();
for (Schema.SObjectType s : gd.values()){
Schema.DescribeSObjectResult r =s.getDescribe();
if (r.isCustom()){
customObjectCount++;
}
}
system.debug('---------Project Stats---------')
system.debug( 'triggerCount = ' + triggerCount);
system.debug( 'codeLines = ' + codeLines);
system.debug( 'classCount = ' + classCount);
system.debug( 'componentCount = ' + componentCount);
system.debug( 'pageCount = ' + pageCount);
system.debug( 'customObjectCount = ' + customObjectCount);