1

I am trying to find whether a given field resides in a particular Sobject. I am using the code

When I try to run this code, I am getting null pointer exception in line no. 3. I guess schemaMap function is having bug. The scenarios I have tested are

1) Namespace :: Tools__ &
Object name :: Order

 Map<String, Schema.SObjectType> schemaMap = Schema.getGlobalDescribe();
Schema.SObjectType leadSchema = schemaMap.get('Tools__Order'); // Object name : Order, namespace prefix : Tools__
Map<String, Schema.SObjectField> M= leadSchema.getDescribe().fields.getMap();
Schema.SObjectField field1 = M.get('Status');
System.debug('###field1'+field1);

Result : Null pointer exception 2)without namespace &
Object name :: Order

 Map<String, Schema.SObjectType> schemaMap = Schema.getGlobalDescribe();
Schema.SObjectType leadSchema = schemaMap.get('Order'); // Object name : Order, namespace prefix : Tools__
Map<String, Schema.SObjectField> M= leadSchema.getDescribe().fields.getMap();
Schema.SObjectField field1 = M.get('Status');
System.debug('###field1'+field1);

Result: ###field1 Status

3)Namespace :: Tools__ &
Object name :: CarSet__c

Map<String, Schema.SObjectType> schemaMap = Schema.getGlobalDescribe();
Schema.SObjectType leadSchema = schemaMap.get('Tools__CarSet__c'); // Object name : Order, namespace prefix : Tools__
Map<String, Schema.SObjectField> M= leadSchema.getDescribe().fields.getMap();
Schema.SObjectField field1 = M.get('Status');
System.debug('###field1'+field1);

Result: null pointer exception

4) without namespace &
Object name :: CarSet__c

Map<String, Schema.SObjectType> schemaMap = Schema.getGlobalDescribe();
Schema.SObjectType leadSchema = schemaMap.get('CarSet__c'); // Object name : Order, namespace prefix : Tools__
Map<String, Schema.SObjectField> M= leadSchema.getDescribe().fields.getMap();
Schema.SObjectField field1 = M.get('Status');
System.debug('###field1'+field1);

Result: ###field1 null

Do anyone know why its coming like that?.

2 Answers 2

2

I would recommend double check the line no 2

Schema.SObjectType leadSchema = schemaMap.get('Tools__Order');

Double check the object name here . Tools__order does not look like proper name for an object .

Add a system.debug after line 1 and debug the whole map thats returned from the global describe .

For custom object with namespace , it will be usually namespace__object__c.

So if your object is custom use

SObjectType leadSchema = Schema.getGlobalDescribe().get('Tools__Order__c');

If you are using standard order object use the below and you don't need to specify namespace

SObjectType leadSchema = Schema.getGlobalDescribe().get('Order');

Also to find if field exists in an object use a null check like below

Map<String, Schema.SObjectField> M= leadSchema.getDescribe().fields.getMap();
 if(M.get('Status') == null){
    //Field does not exist in the map . Add further logic
 }

Also if the object is standard and global describe still does not show up , recommend checking the version of your apex class .I have seen if the object is introduced recently and your apex code is of older version global describe might not have this .Upgrade the apex class version to latest .

Add system.debug after each statement to debug further .

For Standard Objects like Order you don't need to add namespace prefix .Looks like you are using Order Object here , so just use the name as Order

Schema.SObjectType leadSchema = schemaMap.get('Order')
5
  • Actually if used without namespace prefix, its giving the result. But when used with namespace prefix its throwing null pointer exception. my org namespace is 'Tools__' and the object I want to check is 'Order'.
    – Aruna
    Jan 5, 2018 at 5:00
  • For Standard Objects like order you dont need to use namespace . Jan 5, 2018 at 5:03
  • @MohithShrivastava The comments are unclear. It's entirely possible that they mean "a custom object called order" or "the standard order object."
    – sfdcfox
    Jan 5, 2018 at 5:09
  • actually I have tested it with another another object also, there its working fine Map<String, Schema.SObjectType> schemaMap = Schema.getGlobalDescribe(); Schema.SObjectType leadSchema = schemaMap.get('Tools__CarSet__c');// custom object Map<String, Schema.SObjectField> M= leadSchema.getDescribe().fields.getMap(); Schema.SObjectField field1 = M.get('Status'); System.debug('###field1'+field1); // got null as result
    – Aruna
    Jan 5, 2018 at 5:12
  • @sfdcfox From the question above and results looks like it is a standard object Jan 5, 2018 at 6:36
0

My SOQL is of the form select Tools__CarSet__r.Tools__Order__r.Status,Tools__CarSet__r.Tools__Order__r.OwnerId from Tools__Unit_order__c;

Namespace prefix: Tools__

Initially I wrote a method to identify whether the given object is standard or custom

public String checkObjectType(String objectname) {
  SObjectType objToken = Schema.getGlobalDescribe().get(objectname);
  if(objToken  != null && objToken.getDescribe().isCustom()) {
     return 'true';
   } else {
     return 'false';
   }
}

In case of custom objects I am passing the object directly to the schmemap, else if it is standard object I am removing the namespaceprefix from the object name

 if(objval=='true')
     {System.debug('customobj found'+objectt);
      objectType=objectt.replace('__r','__c');

     }
     else if(objval=='false')
     {
     System.debug('customobj not found'+objectt);
      objectType=objectt.replace('__r','');
      objectType=objectType.replace(namespceprefix,'');
       System.debug('non customobj name::  '+objectt+'  replaced obj name ::  '+objectType);
     }

After removing/having the namespace I passed the object to schemaMap to check whether the object has a particualr field in it

        Map<String, Schema.SObjectType> schemaMap = Schema.getGlobalDescribe();
        Schema.SObjectType leadSchema = schemaMap.get(objectType);
        Map<String, Schema.SObjectField> M= leadSchema.getDescribe().fields.getMap();
        Schema.SObjectField field1 = M.get(fieldVal);
        System.debug('###field1'+field1);
if(field1!=null)
{ // field1 is a field of objectType 
}
else{
  // field1 is not a field of objectType 
  }

You must log in to answer this question.

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