1

I am modifying Jeff Dougles' code found on his 'Customizing attachments' blog to take the concept of creating a way to upload attachments to a custom object.

However I'm doing this via sites so no login is required to the system. What I am doing is a user will receive a URL like this. This URL has the ID of the record I want to save the record to.

My problem is I don't know how to modify the code to pass in the URL parameter. Right now I'm just hard coding the ID.

My modified APEX class from Mr. Dougles below. The line "obj.id = 'a0G3000000H744X'" is where I believe I need to pass in the record ID from the URL parameter needs to be set.

public class UploadAttachmentController {

public String selectedType {get;set;} //** original code
//    public Boolean selectedAwesomeness {get;set;} ** Oringial code
//    public String description {get;set;} ** Oringial code
public String cof {get;set;}
//    private Contact contact {get;set;} **Oringinal code
private Stage_Library__c stage {get;set;} //new code pointing to custom object
public Integer selectRoundRequired {get;set;}
public String fileName {get;set;}
public Blob fileBody {get;set;}


public UploadAttachmentController(ApexPages.StandardController controller)  { 
    this.stage = (Stage_Library__c)controller.getRecord();
}   

/* Original code... this relates to Public get;set methods
// creates a new Contact_Attachment__c record
private Database.SaveResult saveCustomAttachment() {
    Contact_Attachment__c obj = new Contact_Attachment__c();
    obj.contact__c = contact.Id; 
    obj.description__c = description;
    obj.type__c = selectedType;
    obj.awesome__c = selectedAwesomeness;
    // fill out cust obj fields
    return Database.insert(obj);
*/

// creates a new Stage_Library__c record
private Database.SaveResult saveCustomAttachment() {
    Stage_Library__c obj = new Stage_Library__c();
//       obj.Name = Stage_Library__c.Id; 
//         obj.Course_of_Fire__c = cof;
    obj.Rounds_Required__c = selectRoundRequired;
    obj.Vicker_Type__c = selectedType;
    obj.id = 'a0G3000000H744X';
    // fill out cust obj fields
    return Database.update(obj);
}

// create an actual Attachment record with the Contact_Attachment__c as parent
private Database.SaveResult saveStandardAttachment(Id parentId) {
    Database.SaveResult result;

    Attachment attachment = new Attachment();
    attachment.body = this.fileBody;
    attachment.name = this.fileName;
    attachment.parentId = parentId;
    // inser the attahcment
    result = Database.insert(attachment);
    // reset the file for the view state
    fileBody = Blob.valueOf(' ');
    return result;
}

/**
* Upload process is:
*  1. Insert new Contact_Attachment__c record
*  2. Insert new Attachment with the new Contact_Attachment__c record as parent
*  3. Update the Contact_Attachment__c record with the ID of the new Attachment
**/
public PageReference processUpload() {
    try {
        Database.SaveResult customAttachmentResult = saveCustomAttachment();

        if (customAttachmentResult == null || !customAttachmentResult.isSuccess()) {
            ApexPages.AddMessage(new ApexPages.Message(ApexPages.Severity.ERROR, 
              'Could not save attachment.'));
            return null;
        }

        Database.SaveResult attachmentResult = saveStandardAttachment(customAttachmentResult.getId());

        if (attachmentResult == null || !attachmentResult.isSuccess()) {
            ApexPages.AddMessage(new ApexPages.Message(ApexPages.Severity.ERROR, 
              'Could not save attachment.'));            
            return null;
        } else {
            // update the custom attachment record with some attachment info
            Stage_Library__c customAttachment = [select id from Stage_Library__c where id = :customAttachmentResult.getId()];
            customAttachment.File_Name__c = this.fileName;

//              customAttachment.[field name that saves attachment id] = attachmentResult.getId();
            update customAttachment;
        }

    } catch (Exception e) {
        ApexPages.AddMessages(e);
        return null;
    }

//    return new PageReference('/'+ Stage_Library__c.Id); *this was the original
    return new PageReference('http://fifedom-developer-edition.na1.force.com/guns/Stage_Submission');
}

public PageReference back() {
    return new PageReference('/'+ Stage_Library__c.Id);
}     

}
2

You should be able to use the following code:

obj.id = ApexPages.currentPage().getParameters().get('Id'); 

which will pull in the Id parameter from the current URL.

See PageReference documentation for more on this if needed. https://www.salesforce.com/us/developer/docs/apexcode/Content/apex_system_pagereference.htm

| improve this answer | |
  • Perfect thanks! that worked. I'm not a APEX developer... just a copy and paste! Much appreciated. – Fifedog Jan 23 '15 at 22:40

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.