function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
jawtechjawtech 

Name field not in getDescribe.fields() on custom object

I get a map of fields using getDescribe on a custom object, but standard fields such as Name or createdbyId are not listed. How do I get them?

 

currently we have code that checks to see if the Name field is updateable, but fails because Name is not included in the fields map. How can I get it?

 

 

string objectName = 'Permit__c';
Map<String, Schema.SObjectType> gdesc = Schema.getGlobalDescribe();
Sobject objectt = gdesc.get(objectName).newSObject();
Map<String, SObjectField> fields = objectt.getSObjectType().getDescribe().fields.getMap();
for (SObjectField f: fields.values())
{
        system.debug('***objectt field: ' + f);
}
Schema.DescribeFieldResult nameFieldDesc = fields.get('Name').getDescribe(); //BOOM! (null exception)
boolean m_isNameFieldUpdateable = nameFieldDesc.isUpdateable();
system.debug('***name is updateable: ' + m_isNameFieldUpdateable);

 

 

dkadordkador

What do your system.debugs print out?

jawtechjawtech

running the code anonymously gets the error:

 

System.NullPointerException: Attempt to de-reference a null object.

 

This is because the code is wanting to describe on the Name field, but the Name field is not provided in the SF field map list.

 

The debug statements print out all of the custom fields, but no standard fields.

vhanson222vhanson222

I know it will probably be a long post, but would you mind posting the entire debug log?

sfdcfoxsfdcfox

The obvious answer would appear to be that there is no name field. I do know that some standard objects (notably Share objects) do not have a name field. It's possible that you've somehow configured the object in such a way that the system removed the name field entirely. I am not sure how one does this, as I can't seem to replicate it, but it is the only solution I can come up with.

The KnightThe Knight

Instead of

Schema.DescribeFieldResult nameFieldDesc = fields.get('Name').getDescribe(); 

 

Use

 

// In a for loop iterate over the fields and find the name field
Schema.DescribeFieldResult nameFieldDesc = fields.get(<fieldname>).getDescribe(); 
Boolean nameField = nameFieldDesc.isNameField();
if namefield
  check for updateable
   break
//end of for loop

 

jawtechjawtech

That's a good try. However, the fields map does *not* contain the Name field to even check for isNameField() to ever return true. So it removes the Null exception, but will never be true either. 

jawtechjawtech

Salesforce does not allow us to remove the standard Name field from a custom object. I can verify that it is still there. Name, Owner, CreatedBy, LastModifiedBy are all standard fields and none of them show up in the field map using the call getDescribe().fields.getMap().

 

When you run the sample code against your own custom object, do you get any standard fields?

vhanson222vhanson222

Could it be some kind of field-level security issue?

jawtechjawtech

I found a solution. I must pass in the Name field value when creating and saving the sobject. For some reason this changes the describe result.