3

I want to check if the (standard) Name field of an object is an auto number field or not. This can be done by describing the field and using isAutonumber()Salesforce Documentation.

I have a custom object called Project_Resource__c with a Name field set as autonumber.

The following code returns true:

Project_Resource__c.Name.getDescribe().isAutoNumber();

In a test factory class, I have a piece of code that does operations on an sObject, this can be any object, standard or custom. What I want to achieve is to get the Name field of that particular object and check if it is an auto number field.

I've tried the following code, but with no result:

sObject sobj;
sobj = new Project_Resource__c();
insert sobj; //my assumption is that this should fill the name with the auto number

//compile error: Expression of type Schema.SObjectTypeFields has no member named Name    
System.debug(sobj.getSObjectType().getDescribe().fields.Name.getDescribe().isAutonumber()); 


// I would have expected to see the fields here
system.debug(sobj.getSObjectType().getDescribe().fields);// Returns Fields[Project_Resource__c]

Something else I find strange (but might be unrelated) is that after the insert

system.debug(sobj.get('Name'));

returns null, while

system.debug([select name from project_resource__c]);

returns a project_resource__c with an auto number name

  • 1
    About second strange things - as far as you are doing DML, record is not updated at your variable, except for record id. For example, if there is a trigger, that set some field to random value on insert, and if you insert that record, and check value of field on the same variable -- it will give you null. If you requery it - that would give proper value – kurunve Mar 10 '16 at 15:33
  • The Id field is the only field that gets populated after an insert. You'll need to re-query the object to get the Name field. – frontendloader Mar 10 '16 at 15:48
3

The only field you get for free without re-querying is Id, so yes, you have to query back for the Name to get the actual auto-number result. As for your describe, you don't need to get the describe from your record, you can just do:

DescribeSObjectResult describe = SObjectType.Project_Resource__c;
Map<String, SObjectField> fields = describe.fields.getMap(); // <- you're missing this call
DescribeFieldResult nameDescribe = fields.get('Name').getDescribe();

Note that you can get the field describe even more simply, as you have found near the top of your OP:

DescribeFieldResult nameDescribe = Project_Resource__c.Name.getDescribe();
| improve this answer | |
  • 1
    You're right about the field describe, but I don't know the object type in advance. Adding the getmap().get('Name') did the trick! sobj.getSObjectType().getDescribe().fields.getmap().get('Name').getDescribe().isAutonumber() – Folkert Mar 10 '16 at 15:57
  • @Folkert that's why I tried to break it out into steps. Wasn't sure where you were stuck. :) – Adrian Larson Mar 10 '16 at 16:50
2

I believe you are getting the error because SObject is a generic type and at the compile time, it is not known what will be the type of SObject and whether the SObject contains a field labelled as `NAME. It can only be determined at runtime.

While in the other case,

Project_Resource__c.Name.getDescribe().isAutoNumber();

The type of SObject is known at compile time and also that it contains the field labelled as Name. You can confirm this behaviour by trying this

 Project_Resource__c.NameDummy.getDescribe().isAutoNumber();

The above code will give you compile error saying that the fields NameDummy does not exists.


As a solution, you can use the following code

System.debug(sobj.getSObjectType().getDescribe().fields.getMap().get('Name').getDescribe().isAutonumber());

Example:

if(sobj.getSObjectType().getDescribe().fields.getMap().containsKey('Name')){
     System.debug(sobj.getSObjectType().getDescribe().fields.getMap().get('Name').getDescribe().isAutonumber()); 

}
| improve this answer | |
  • Isn't Name a field on any object, as it is a standard field? So no reason to check. – Folkert Mar 10 '16 at 16:04
  • 1
    No, the Name field is not present on all Objects. I am taking into consideration the objects which are not directly exposed to User. Like the History objects (CaseHistory and AccountHistory etc) do not have Name field. There are other objects as well like DataType... – manjit5190 Mar 10 '16 at 16:13
  • 1
    You probably won't create those though... most notable exception is probably Case. – Adrian Larson Mar 10 '16 at 16:48
  • @AdrianLarson Yes it is. – manjit5190 Mar 10 '16 at 17:17

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.