3

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.

8

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! – Andrew Fawcett Jan 28 '13 at 15:43
2

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
1

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 '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? – pchittum Jan 29 '13 at 10:00
1

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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