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 have

Schema.SObjectField field = MyObject__c.myField__c;
Schema.SObjectType objectType = getTypeOf(field);

how would getTypeOfField() need to look like for this to be true?

System.assertEquals(MyObject__c.SObjectType, objectType);
share|improve this question
    
Looks like I added a #duplicate of the unanswered salesforce.stackexchange.com/questions/32528/… –  Robert Sösemann Apr 11 at 13:28
1  
Looks like you added a #duplicate of the answered salesforce.stackexchange.com/q/29911/... ;-) it's a no-go even with getReferenceTo() –  user320 Apr 11 at 13:49
    
I think in 2 years someone might have found a solution to this. So please don't close ;-) –  Robert Sösemann Apr 11 at 14:01
1  
I agree with @user320 it's a no go, just been racking my brain on this one also, the methods of SObjectFieldDescribe just don't provide any indiction, best i could suggest is if you know your always going to be passing a field from a known subset of custom objects is to build a map (via the fields.getMap()) internally and store it statically. –  Andrew Fawcett Apr 11 at 15:23
2  
Does it have to be a Schema.SObjectField? If you could start passing fieldsets around you might gain some flexibility (admin able to modify the page without the help of a developer) and the FieldSet class does have the getSobjectType(): salesforce.com/us/developer/docs/apexcode/Content/… –  eyescream Apr 11 at 20:56

1 Answer 1

While I really hope there is an easier way to achieve this, the Tooling API might be one candidate.

You can interrogate the Salesforce Tooling API with an Apex library or just use the code below by itself:

  1. First specify your field name:

    //choose your field name (without suffix!)
    String fieldName = yourField.getDescribe().getName().substringBefore('__c');
    System.debug(fieldName);
    

    'Status'

  2. Then obtain the Custom Object Id (eg 01I...) or Table Enum (eg Account) which it lives on:

    String query = 'SELECT TableEnumOrId From CustomField WHERE DeveloperName = \'' + fieldName + '\'';
    String endpoint = Url.getSalesforceBaseUrl().toExternalForm();
    endpoint += '/services/data/v29.0/tooling/query/?q=';
    endpoint += EncodingUtil.urlEncode(query, 'UTF-8');
    
    HttpRequest request = new HttpRequest();
    request.setMethod('GET');
    request.setHeader('Authorization', 'Bearer ' + UserInfo.getSessionId());
    request.setEndpoint(endpoint);
    
    HttpResponse response = new Http().send(request);
    String tableEnumOrId = response.getBody().substringAfter('"TableEnumOrId":"').substringBefore('"');
    
    System.debug(tableEnumOrId);
    

    '01Ib0000000CM8TEAW'

  3. Finally, you can pull the API / Developer Name off the Custom Object definition itself:

    query = 'SELECT DeveloperName From CustomObject Where Id = \'' + tableEnumOrId + '\'';
    endpoint = Url.getSalesforceBaseUrl().toExternalForm();
    endpoint += '/services/data/v29.0/tooling/query/?q=';
    endpoint += EncodingUtil.urlEncode(query, 'UTF-8');
    
    request = new HttpRequest();
    request.setMethod('GET');
    request.setHeader('Authorization', 'Bearer ' + UserInfo.getSessionId());
    request.setEndpoint(endpoint);
    
    response = new Http().send(request);
    System.debug(response.getBody());
    String customObjectName = response.getBody().substringAfter('"FullName":"').substringBefore('"');
    
    System.debug(customObjectName);
    

    'Schematic'

  4. Use Type.forName to get at your desired SObject, SObjectType, or DescribeSObjectResult:

    Type reflector = Type.forName('Invoice__c');
    SObject obj = (SObject)reflector.newInstance();
    SObjectType objectType = obj.getSObjectType();
    //now you can do objectType.getDescribe etc...
    

There are a number of caveats which would need serious 'hardening':

  • you may need to truncate or append the __c as necessary in the Tooling API queries
  • in managed context you will need to play with the namespace prefix.
  • gonna be tricky if you have multiple fields with the same name!
share|improve this answer
1  
Just wondering how this would work if i had the same field name on multiple custom objects? e.g. Total on object A and object B? –  Andrew Fawcett Apr 11 at 15:00
    
My code probably won't work :( definitely a tricky area, have added that to the list of caveats! Thanks @AndrewFawcett –  user320 Apr 11 at 15:11

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.