0

I have this code which will allow a user to add an a label and a description when making an attachment.

Scenario :

The custom primary object is Product_Review__c.

The custom child object used to make the attachment is Product_Review_Attachment__c

A custom list button for the child object brings up a VisualForce page with a "Choose File" button, Description Type picklist, a Description long text field,and an "Attach File" button (action="{!processUpload}").

I would like to give the user the ability to simply enter a Description and NOT include a file, essentially turning the whole process into a way to leave a note. But I can't figure out the best way to include this into the code. Right now it is still creating the Product_Review_Attachment__c record - but not allowing the process to complete without a file to save as well.

I think it's all just a matter of a properly placed IF statement to prevent it from going into the SaveAttachment method, but I can't figure out where to identify that the user has neither a Description AND a file. (If they have a Description then they should be able to save without the file).

Here is the controller :

public class UploadAttachmentControllerProdRev {

    public String selectedType {get;set;}
    public Boolean selectedAwesomeness {get;set;}
    public String description {get;set;}
    private Product_Review__c contact {get;set;} 
    public String fileName {get;set;}
    public Blob fileBody {get;set;}

    public String contactproduct {get;set;}

    public UploadAttachmentControllerVendorProdRev(ApexPages.StandardController controller) { 
        this.contact = (Product_Review__c)controller.getRecord();
        //contactproduct = contact.Product__c;
    }   

    // creates a new Product_Review_Attachment__c record
    private Database.SaveResult saveCustomAttachment() {
        Product_Review_Attachment__c obj = new Product_Review_Attachment__c();
        obj.Product_Review__c = contact.Id; 
        obj.Product__c = contact.Product__c;
        obj.Description__c = description;
        obj.Document_type__c = selectedType;
        //obj.awesome__c = selectedAwesomeness;
        // fill out cust obj fields
        return Database.insert(obj);
    }

    // create an actual Attachment record with the Product_Review_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 Product_Review_Attachment__c record
    *  2. Insert new Attachment with the new Product_Review_Attachment__c record as parent
    *  3. Update the Product_Review_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
                Product_Review_Attachment__c customAttachment = [select id from Product_Review_Attachment__c where id = :customAttachmentResult.getId()];
                customAttachment.name = this.fileName;
                customAttachment.Attachment__c = attachmentResult.getId();
                update customAttachment;
            }

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

        return new PageReference('/'+contact.Id);
    }

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

Thank you for your help.

1

Yes, you have to add a condition to check description has been entered by user or not. So you have to add IF statement at the start of the processUpload method of your code. If description is blank or null then you have to add a page error. Like that :

***if(description == null &|| description == '')
{
    ApexPages.AddMessage(new ApexPages.Message(ApexPages.Severity.ERROR, 
                  'Could not save record without discription.'));
    return null;
}***

Thanks, Pragati Jain

  • Please see the answer guidelines - How to Answer - code should be formatted into a code block and please do not ask for upvotes in the answer. – Dave Humm Aug 6 '16 at 11:32
  • @PragatiJain - thank you for your help. Right now the error I am getting in my debug logs is in reference to line 34 which is "attachment.body = this.fileBody;" and comes before the processUpload method. So, wouldn't I still get that error if I were to put your statement in there ? – Zoom_v Aug 8 '16 at 15:14
  • @PragatiJain - I can see the description==null would work, but that still does not prevent the system from attempting to upload a record which has a description but no attachment. Somehow I need to figure out if there is an attachment before it attempts to create one with the saveStandardattachment method. – Zoom_v Aug 8 '16 at 16:17
  • You can check filebody is null or not. – Pragati Jain Aug 8 '16 at 17: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.