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
NANCY1NANCY1 

Already saved Apex Class..is giving an error even if i try to save it again???

Hi All of you,

 

This might look strange to you.....

 

I have just started working on SFDC, and i have created a class to upload an attachment form the visualforce page.

 

till Yesterday, it worked sucessfully. But when i try to use that Page today i am getting the following error: Variable does not exist: OwnerId

 

And the funny thing which i am facing is that if in the class i click on the edit button and without doing anything even if i click on the save button.. i am getting the following error....

 

 

Error: Compile Error: Variable does not exist: OwnerId at line 24 column 5

 

I am not able to understand.. why is it happening that already saved class is not getting saved again... even if i do not make any changes to that

 

And if there was an error then why did it saved then???

 

 

My class is as follows:

 

public with sharing class AttachmentUploadControllerr {
  public Attachment attachment {
  get {
      if (attachment == null)
        attachment = new Attachment();
      return attachment;
    }
  set;
  }
 public Id recId
    {    get;set;    }

    public AttachmentUploadControllerr(ApexPages.StandardController ctrl)
    {
       recId = ctrl.getRecord().Id;    
    }

 
  public PageReference upload() {
 
    attachment.OwnerId = UserInfo.getUserId();
    attachment.ParentId = recId; // the record the file is attached to
    try {
      insert attachment;
      PageReference pr;
      pr = new PageReference('/' + recId);
      pr.setRedirect(true);
      return pr;

    } catch (DMLException e) {
      ApexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR,'Error uploading attachment'));
      return null;
    } finally {
      attachment = new Attachment();
    }
    ApexPages.addMessage(new ApexPages.message(ApexPages.severity.INFO,'Attachment uploaded successfully'));
    return null;
  }
}

Best Answer chosen by Admin (Salesforce Developers) 
Nick34536345Nick34536345

I think you have another Apex class called Attachment. Renaming or deleting that should solve your issue.

 

 

 

All Answers

Cory CowgillCory Cowgill

That is certainly interesting. It looks like it should work. I even tested in my org and it worked.

 

One question though:

 

Why not just remove setting the attachment.ownerId field?

 

Based on you code, you are just setting the ownerId to the current user.

 

If you do not set the OwnerId on an Object, this is the default behavior and it will automatically assign it to the current user.

 

Why not just remove that line of code since its not adding any value?

NANCY1NANCY1

Hi,

 

Thanks for your reply.

 

I did exactly the same, but now its giving an error on the next line..

 

 

Error: Compile Error: Variable does not exist: ParentId at line 26 column 5 

 

I think something is wrong with an attachment object..... do i need to check anything... because i am facing the same issue in my other classes as well where i have used an attachment object......

NANCY1NANCY1

Hi you All,

 

This should not be major one for you people.

 

Now, the problem has been increased.. i am not able to create any class using an attachment object... why??

Nick34536345Nick34536345

I think you have another Apex class called Attachment. Renaming or deleting that should solve your issue.

 

 

 

This was selected as the best answer
NANCY1NANCY1

Hi Nick,

 

Thanks a lot.. you have saved me....   :smileyhappy:

NANCY1NANCY1

Hi,

 

Please help me in writing the test class for this class.. that covers minimum coveragwe area.... as i have no idea about how to write it.... Thanks

 

 

public with sharing class AttachmentUploadControllerr
 {
   public Attachment attachment
   {
    get
    {
     if (attachment == null)
     attachment = new Attachment();
     return attachment;
    }
    set;
   }

   public Id recId
    {    get;set;    }
   
    public AttachmentUploadControllerr(ApexPages.StandardController ctrl)
    {
       recId = ctrl.getRecord().Id;    
    }
 
   public PageReference upload()
   {
    attachment.OwnerId = UserInfo.getUserId();
    attachment.ParentId = recId; // the record the file is attached to
    try {
      insert attachment;
      PageReference pr;
      pr = new PageReference('/' + recId);
      pr.setRedirect(true);
      return pr;
    } catch (DMLException e) {
      ApexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR,'Error uploading attachment'));
      return null;
    } finally {
      attachment = new Attachment();
    }
      ApexPages.addMessage(new ApexPages.message(ApexPages.severity.INFO,'Attachment uploaded successfully'));
      return null;
   }
 }