Take the 2-minute tour ×
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.

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
add comment

4 Answers

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
1  
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
add comment

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('__');
share|improve this answer
2  
Nice, use more or less the same approach here, +1 from me! –  Andrew Fawcett Jan 28 '13 at 15:43
add comment

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];
    }
    System.debug(prefix);
}

null null
NAMESPACE NAMESPACE
null NAMESPACE
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
    
add comment

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:

(?<=__).*|(?<=__).*__c

ie) Using MY_NAMESPACE__First_Name__c would match First_Name__c

share|improve this answer
add comment

Your Answer

 
discard

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.