+ Start a Discussion
ministe2003ministe2003 

accessing Attachments via Sites

Hi all,

Trying to allow users to create a case via sites on our intranet.  That isnt a problem, however I dont seem to be able to allow the user to create Attachments.

 

There's nothing in the user profile to allow access to Attachments that I can see but I'm getting Authorization errors when I try and insert an attachment.

 

Anyone know how I can do this?

 

Thanks

Steven

Best Answer chosen by Admin (Salesforce Developers) 
ministe2003ministe2003

Curious.
The issue came when i was creating a case with both a Contact ID and an attachment - either one worked on its own but not together.

 

turned out the case ID wasnt being filled out when it had a contact on it and so when it came to inserting the attachment, it had no parent.  Presumably SF must queue up inserts and do them in some other order to that which you specify in the code because when I cut out the Contact ID, inserted the case then the attachment and then after that, updated the case with the contact ID everything worked!

 

Finally....... :smileyhappy:

All Answers

BulentBulent

Book: Force Platform Developer Guide Chapter 14 has an example where visitors upload their resumes (which are attachments)

ministe2003ministe2003

Thanks for the suggestion but It hasn't helped :(

 

The only thing I have trouble with is inserting the attachment to the database:

Database.Saveresult sr = Database.insert(attach);

 

If I comment out that line I dont have any problems but otherwise I get

Authorization Required

You must first log in or register before accessing this page.
If you have forgotten your password, click Forgot Password to reset it.

Here is my Controller:

 

public with sharing class clsWebToCase {
    public Blob file {get; set;}
    public Case newCase {get; set;}
    public String fileName {get;set;}
    public String contentType {get; set;}
    public Boolean submitted {get;set;}
    
    public clsWebToCase(){
    	submitted=false;
    	newCase = new Case();
    }
    
    //create case and upload attachment, if any exists
    public PageReference CreateCase(){
    	Database.Saveresult sr;
    	
    	newCase.AccountId = newCase.Contact.AccountId;
    	newCase.Status = 'New';
    	newCase.RecordTypeId = [SELECT Id FROM RecordType WHERE Name='Application Project' AND SobjectType='Case' LIMIT 1].Id;
    	newCase.SuppliedEmail = newCase.Contact.Email;
    	newCase.SuppliedPhone = newCase.Contact.Phone;
    	newCase.OwnerId = '00G20000001QJeE'; //Application Projects Queue
    	sr = Database.insert(newCase);
    	if(fileName != null && fileName != ''){
    		Attachment attach = new Attachment();
    		attach.Body = file;
    		attach.Name = fileName;
    		attach.ContentType = contentType;
    		attach.parentId = newCase.id;
    		sr = Database.insert(attach);
    	}
    	ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.INFO, 'Your case has been submitted - Thank You'));
    	submitted = true;
		return null;
    }
}

 

and here is the insert section of the page:

 

    <apex:pageBlock mode="edit" title="Choose file to attach" id="AttachFile" rendered="{!OR(NOT(submitted),(fileName != null))}">
        <apex:pageblockSection title="Attachment"  collapsible="false">
            <apex:inputFile value="{!file}" filename="{!fileName}" contentType="{!contentType}" rendered="{!NOT(submitted)}"/>
            <apex:outputText value="{!fileName}" rendered="{!submitted}"/>
        </apex:pageblockSection>
    </apex:pageBlock>

 

 

Anyone have any Ideas why I get this error?

Thanks

Steven


 

 

rlreynosorlreynoso

I am having the same issue.  Are attachment not allowed by Guest user license???  if someone has an answer pleae help.

rlreynosorlreynoso

It turns out that the "Authorization Required" page appears for other reasons such as an error while inserting.  I created a debug log for the Site Guest User and found that the problem was my code.  The Parent ID was not populating as i assumed it was.  When i hard coded a parent ID it work like a charm.  I just need to find out why my code is not taking the newly created record ID and assigning it to the Attachment Parent ID. 

ministe2003ministe2003

Really?  Oh thanks for the update I'll have a go at that myself then; good luck and I'll let you know if I fix mine; I probably have the same error I imagine!

 

Cheers for the heads up!

ministe2003ministe2003

Disappointing, I'm getting this:

 

Insert failed. First exception on row 0; first error: INSUFFICIENT_ACCESS_ON_CROSS_REFERENCE_ENTITY, insufficient access rights on cross-reference id: []

And when I output my attachment I get this:

 

Attachment:{Name=email TWO.txt, ParentId=5002000000AJ2ZVAA1, Body=Blob[3088], ContentType=text/plain}

 

Which appears ok to me, am I missing something?  Seems strange that I get that error!

 

Thanks

ministe2003ministe2003

I've got it!!!

 

Edit the user profile and make sure the tick box "API Enabled" is ticked.  It is under the Administrative Permissions section.

Once I ticked this I can now create attachments in Sites :D

 

Magic

rlreynosorlreynoso

Really? You must be doing something a little different then I because i didnt need to check "api access" for my guest license and it worked. Here are the steps i took

 

1) make sure the guest license profile for the site has access to the custom object.

 2) under field level security make sure they have access to the fields you are referencing on the site

3) as for the parentid issue on the attachment; i resolved it by inserting the record before assigning the record id to the attachment parent id

 

public class subrogationController {

       public blob getfile {get; set;}

       public Object__c newResolution {get; set;}

       public Attachment newAttachment {get; set;}

       public String filename {get; set;}

 

public subrogationController()

{

newResolution = new Object__c();

newAttachment = new Attachment();

 }

 

public PageReference save()

 {

insert newResolution;

newAttachment.body = getfile;

newAttachment.parentid = newResolution.id;

newAttachment.name = filename;

insert newAttachment;

return ApexPages.currentPage();

 }

 }

ministe2003ministe2003

I'm not using any custom objects, just Cases and Attachments.  The only issue I had was when actually inserting the attachment to the database, the profile had access to everything it needed (fields and cases)

There is no option to give access to attachments (as you are probably aware) on the profile.

I had no issues with parentID or any other fields, I was already getting the Id after inserting the Case.

 

Strange how we have different fixes!

ministe2003ministe2003

Now I have another issue, it seems I can't access Contacts either through the guest license!

I thought Guest licenses were meant to have read access to all standard objects?  I've set the profile to be able to view contacts but I get Authentication Required all over again :(

 

Why can't I access these objects?!

ministe2003ministe2003

Curious.
The issue came when i was creating a case with both a Contact ID and an attachment - either one worked on its own but not together.

 

turned out the case ID wasnt being filled out when it had a contact on it and so when it came to inserting the attachment, it had no parent.  Presumably SF must queue up inserts and do them in some other order to that which you specify in the code because when I cut out the Contact ID, inserted the case then the attachment and then after that, updated the case with the contact ID everything worked!

 

Finally....... :smileyhappy:

This was selected as the best answer
rlreynosorlreynoso

Yep, thats what the issues was with mine too last week.  look at my last post (post 9) step 3.

 

For everyone trying to access the ID of a newly created recored, make sure you insert the record first before trying to access the ID, otherwise it will just be blank when you assign it to a Foreign Key of another record

ministe2003ministe2003

It wasnt quite as obvious as it seems though, I wasn't inserting the contact seperately, it was being inserted as a field on the Case which was in turn, being inserted before the attachment.

Anyway, im just glad I sorted it!

Philip_FPhilip_F

I have a quick question - how did you create a debug log for the Site Guest User?  In my sites setup, sites unauthenticated users have the Site Guest User profile, but there's no actual user account to add to the debug logging.  Is there a way around this?

 

Cheers,

-Philip

BulentBulent

each site has an actual user (guest user). You can't login as this user, only gets authenticated when a public site visitor access your public site page.

 

You can find the user by clicking the "view users" button on the public access setting page for your site.

Bonus info: you can set your site default language and locale via this user as well.

 

BulentBulent

Additional info for the folks reading this posting.

 

with Summer 10 we added the "preview as Admin" feature where you can see the exact error for your site.Search "Preview as admin" in online help for more details.

JaganCJaganC
I have the same issue. Whenever a case attahment is inserted from site for a case with Contact Id populated, I get the error, "INSUFFICIENT_ACCESS_ON_CROSS_REFERENCE_ENTITY". I worked aroud this issue by removing the "with sharing" keyword for the site page controller.
I can confirm this issues is only for Site  Guest User and Interface Users. 
Anupreeta Chakraborty 16Anupreeta Chakraborty 16
Regarding Authorization error while adding attachments in sites for case, this might be the probable reason:

The controller cannot find the Parent Id of case where the attachment needs to be added.
1.While you are navigating to the VF page of case, where you are adding attachments, make sure you pass your case id:
   reRend = new PageReference('/apex/RedirectToCaseDetailsandAttachments');
       reRend.getParameters().put('id',objCase.id);

2. then in controller, where you are attaching file, pass the same case id in ParentId as below:
  public PageReference Submit() {
     caseId = System.currentPageReference().getParameters().get('id');
     insert new Attachment(Name = filename, Body = filebody, ParentId = caseId);
        return null;
    } 

This is fix the Authorization error.