3

I am writing a SObject generating class for unit test. Here is my code:

public class Uth_Integration_Obj extends Uth_Sobject
{
    public String status;
    public String sf_id;
    public String sobject_type;

    public Uth_Integration_Obj(String id)
    {
        //Default values only
        status = Integration_Status.OUTPENDING;
        sobject_type = Sobject_Type.FOLDER; //This line caused the error. 
        sf_id = id;
    }
//...

And the Sobject_Type file is a fairly simple one:

public class Sobject_Type 
{
    public final static String FOLDER = 'Folder__c';
    public final static String LISTOBJ = 'List__c';
}

I am getting the error during compilation:

Compile Error for Uth_Integration_Obj.cls: Initial term of field expression must be a concrete SObject: String at line 12 column 21

Line 12 is marked in the comment.

If I change Sobject_Type.FOLDER into 'folder__c' it will cause no issue at all. But basically they should be the same thing. I have no idea what is the issue here. Any explanation?

  • if you change the local property to sObject_Types the error goes away. Using the this keyword does not fix it either. Maybe someone with more in depth knowledge than me can tell you why. – Eric Jun 28 '16 at 1:01
  • @Eric Nice one. It seems to be my naming issue. If you could make that as an answer, I will accept it. – Lance Shi Jun 28 '16 at 1:05
3

What is really happening under the hood is that you have a property named sobject_type that clashes with your class name. So when you try to reference properties on your Apex Class, it is trying to reference properties on the String class instead.


Your syntax is going to cause you headaches. You should also take better advantage of static types. To see why the naming is a poor choice, simply consider that SObject_Type.FOLDER would return String, while SObjectType.Folder__c would return Schema.DescribeSObjectResult and Folder__c.SObjectType would return Schema.SObjectType.

Here is how I might write the same object:

public class MyIntegrationObject
{
    public Schema.SObjectType schemaType;
    public String sf_id, status;
    public MyIntegrationObject(Id input)
    {
        if (input != null)
        {
            schemaType = input.getSObjectType();
            sf_id = input;
            status = MY_CONSTANT;
        }
    }
}
  • Thank you for your answer, Adrian. But I am not quite sure what you are trying to point out in my code. Those values are just default values as stated in the comment. So they should not be final – Lance Shi Jun 28 '16 at 1:06
3

Making an answer as request but I feel there can be more added to it. If someone knows the WHY of it please edit the answer to include that information.

If you change the local property to sObject_Types the error goes away. Using the this keyword does not fix it either.

  • Yeah the why isn't too complicated. I added it to my answer. – Adrian Larson Jun 28 '16 at 1:07
  • @LanceShi - I think Adrian's answer is more complete..... – Eric Jun 28 '16 at 1:11
  • @Eric Thank you for the help, Eric. It seems to be naming conflicts here. I will mark Adrian's answer as accepted as he answered why. – Lance Shi Jun 28 '16 at 1:15

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.