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.

4 Answers 4


Even if Salesforce.com 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('__');
  • 2
    Nice, use more or less the same approach here, +1 from me! Jan 28, 2013 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

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.

  • 1
    Oh...I just saw that eyescream had already used split...
    – pchittum
    Jan 28, 2013 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. Jan 28, 2013 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. Jan 28, 2013 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?
    – pchittum
    Jan 29, 2013 at 10:00

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .