Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I want to get a potentially empty namespace prefix from arbitrary sobject names by using Regexp. All following cases should be covered:

 1. StandardObject 
 2. CustomObject__c 
 3. NAMESPACE__StandardObject
 4. NAMESPACE__CustomObject__c

I've tried groups. I tried lookaheads, but I failed miserably ;-) I am a RegExp dummy.

share|improve this question
up vote 1 down vote accepted

It seems the easiest way to get this is with a String split method (I'm a big fan):

List<String> nameSegs = describe.getName().split('__');

String nameSpace = nameSegs.size() > 2 || 
     (nameSegs.size() == 2) && !nameSegs.get(1).equalsIgnoreCase('c') ? nameSegs.get(0) : 'none';

So if you split on the '__' characters, you will either have 1, 2, or 3 members of the list. If you have 1, there is no namespace. If you have 3, there is a namespace. If you have 2, and the second element of the list is not 'c' you have a namespaced standard object.

I tested on your four cases and it seems to work.

share|improve this answer
Oh...I just saw that eyescream had already used split... – Peter Jan 28 '13 at 16:22
Your solution is better than mine because the getName/getLocalName method I proposed does not work for external namespaced objects in a managed package org. There the localName and name are equal. – Robert Sösemann Jan 28 '13 at 19:18
BTW Peter: I think you don't need to differentiate between size=2 and size=3. Standard objects have never a namespace suffix nor a prefix __c. So only size=3 cases are namespaced. – Robert Sösemann Jan 28 '13 at 19:20
I wondered about standard objects and namespaces, but my experience with namespaced orgs is minimal. I was just going by the literal requirement. ;-) And still there is the context of Geolocation. But that would only be if you were also trying to parse namespaces from field names, right? – Peter Jan 29 '13 at 10:00

Even if APEX does not provide an own method I found a way to solve this without regexp:

String name = describe.getName();
String localName = describe.getLocalName();
String namespace = name.removeEnd(localName).removeEnd('__');
share|improve this answer
Nice, use more or less the same approach here, +1 from me! – Andrew Fawcett Jan 28 '13 at 15:43

Regexps are evil ;)

Something like this, translate to regexp as you see fit?

List<String> names = new List<String>{
    'StandardObject', 'CustomObject__c',
    'NAMESPACE__StandardObject', 'NAMESPACE__CustomObject__c',
    'Location__Latitude__s', 'NAMESPACE__Location__Latitude__s'}; // don't forget about the Geolocation fields ;)

for(String n : names){
    String prefix = null;
    List<String> pieces = n.split('__');
    Integer size = pieces.size();
    if(!(size == 1 || pieces[1] == 'c' || (size > 2 && pieces[2] == 's'))){
        prefix = pieces[0];

null null
share|improve this answer
Oh yes they are evil. But thats too complicated. :-( – Robert Sösemann Jan 28 '13 at 15:31
Shoot! Do Geolocation fields really use a double underscore between Location and the field name!? I'd not seen those! – Peter Jan 28 '13 at 16:21

RegEx for getting the namespace:


ie) Using MY_NAMESPACE__First_Name__c would match MY_NAMESPACE

RegEx for getting the field name minus the namespace:


ie) Using MY_NAMESPACE__First_Name__c would match First_Name__c

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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