title: Compile Error: Field expression not allowed for generic SObject at line 33 column 69
body: <p>I am trying to write a generic controller which will work for several different object types, but when I try to access custom fields from those objects, I get the following error:</p>
  
  <blockquote>
    <p>Compile Error: Field expression not allowed for generic SObject at line 33 column 69</p>
  </blockquote>
  
  <p>Here's my generic controller code:</p>
  
  <pre><code>public with sharing class PhotoUploadController {
  
      public Attachment objAttach{get;set;}
      public sObject sobj{get;set;}
      public string ObjectName {get;set;}
      public Decimal Height{get;set;}
      public Decimal Width {get;set;}
      Public List&lt;Attachment&gt; lstAttachment {get;set;}
  
       public String setObjectType(sObject newObj)
        {
          this.ObjectName = newObj.getSObjectType().getDescribe().getName();
          return(this.ObjectName );
         }
  
      public String getObjectType() 
      {
            return(this.ObjectName);  
       }
  
     public PhotoUploadController (ApexPages.StandardController controller)
       {
  
          objAttach=new Attachment();
          lstAttachment= new List&lt;Attachment&gt;();
          sobj = controller.getRecord();
          if(sobj.id!=null){
             sobj = Database.query('select id,name,Image_Attachment_ID__c from ObjectName where id=:sobj.id');  
          } 
  
           if(sobj.Image_Attachment_ID__c!=null){
  
              lstattachment=[SELECT id,body from Attachment where Id=:sobj.Image_Attachment_ID__c limit 1];
              if(lstattachment.size()&gt;0)
              {
                  objAttach=lstattachment[0];
              }
          }         
      }
  
      public pageReference savePhoto(){  
       sobj.HeightOfPhoto__c=Height;
       sobj.WidthOfPhoto__c=Width;
  
          try {
              if (!Schema.sObjectType.sobjectName.isUpdateable())
                  {
                    return null;
                  }
              String contentType;
  
              if(objAttach.ContentType !=null){
                 contentType=objAttach.ContentType;
              }
  
              if(contentType !=null &amp;&amp; (contentType == 'image/jpeg' || contentType == 'image/CR2' || contentType== 'image/jpg' || contentType== 'image/png' || contentType== 'image/bmp' || contentType== 'image/gif')){            
              //objAttach.body=body;
              //objAttach.name=name;
              if(objAttach.body!=null &amp;&amp; objAttach.name!=null &amp;&amp; sobj.id !=null){
                   If(objAttach.Id==null)
                       objAttach.parentId=sobj.id;
                   if(!Schema.sObjectType.Attachment.isUpdateable()){
                    return null;
                    }
  
                   try{
                          upsert objAttach;
                          sobj.Image_URL__c=URL.getSalesforceBaseUrl().toExternalForm()+'/servlet/servlet.FileDownload?file='+objAttach.id;
                          sobj.Image_Attachment_ID__c= objAttach.id;
                          objAttach =null;
                          if (!Schema.sObjectType.ObjectName.isUpdateable()){
                            return null;
                          }
                         upsert sobj;
                     }
                     catch (DMLException e) {
                             if(e.getMessage().contains('STORAGE_LIMIT_EXCEEDED')){
                                ApexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR,'Your orgnization Storage Limit has been exceeded, Please contact your Administrator.'));
                             }else{
                               ApexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR,'Error uploading attachment'));
                             }
                             return null;
                      }finally {
                            objAttach = new Attachment();
                      }    
              }
  
           }
           else{
                objAttach = new Attachment();
                ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR,'Please, Upload image file with extension .JPG .JPEG .BMP .PNG or .GIF'));
                return null;
           }
          }
          catch(Exception e) {            
               return null;
          }
          return new pagereference('/'+sobj.id);    
      }
  
  }
  </code></pre>
  
  <p>Can someone explain this error message in a bit more detail? How do I access the fields I need from a generic SObject type?</p>