You need to sign in to do that
Don't have an account?

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 { } catch (DMLException e) { |
I think you have another Apex class called Attachment. Renaming or deleting that should solve your issue.
All Answers
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?
Hi,
Thanks for your reply.
I did exactly the same, but now its giving an error on the next line..
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......
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??
I think you have another Apex class called Attachment. Renaming or deleting that should solve your issue.
Hi Nick,
Thanks a lot.. you have saved me.... :smileyhappy:
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;
}
}