• Will Slotterback
  • NEWBIE
  • 20 Points
  • Member since 2016

  • Chatter
    Feed
  • 0
    Best Answers
  • 1
    Likes Received
  • 0
    Likes Given
  • 3
    Questions
  • 4
    Replies
The title says it all really. I have a visualforce page that utilizes the standard controller for Account (standardcontroller="Account") and I'm trying to add an inputField to the page which will accept an Opportunity. How do I do this?

Thanks in advance for the help,
    Will
Hi,

I have a checkbox field on my opportunity object which I'm trying to reference in the filter logic section of the report builder, however the field isn't showing up and I can't figure out how to access the field programmaticaly (or even if that's possible). My ultimate goal is to filter a report on Tasks based on whether or not the opportunity that the task is attached to has this checkbox field checked. Any help would be much appreciated.

Thanks in advance,

Will
In one of Jeff Douglas's blog posts he demonstrated how to create a custom Attachment related list for specific objects. http://blog.jeffdouglas.com/2014/05/30/how-to-customize-salesforce-attachments/ 
I've implemented that code to work with our custom Project object by following his examples, but I don't know how to write the test classes for both the VisualForce page and the Apex class so that we can put this custom object into production. I would really appreciate it if someone could provide a test class for this code or at least get me started. 

The code I've written thus far is below:

The Apex Controller:
public with sharing class UploadAttachmentController {
    
    public String selectedType {get;set;}
    public String description {get;set;}
    public MPM4_BASE__Milestone1_Project__c project {get;set;} 
    public Project_Attachment__c attachment {get;set;}
    public String fileName {get;set;}
    public Blob fileBody {get;set;}
    
    public UploadAttachmentController(ApexPages.StandardController controller) { 
        this.project = (MPM4_BASE__Milestone1_Project__c)controller.getRecord();
    }   
    
    // creates a new Project_Attachment__c record
    private Database.SaveResult saveCustomAttachment() {
        Project_Attachment__c obj = new Project_Attachment__c();
        obj.Project__c = project.Id; 
        obj.description__c = description;
        obj.type__c = attachment.Type__c;
        // fill out cust obj fields
        return Database.insert(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;
        // insert the attachment
        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
                Project_Attachment__c customAttachment = [select id from Project_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('/'+project.Id);
    }
    
    public PageReference back() {
        PageReference pgRef = new PageReference('/'+ this.project.Id);
        pgRef.setRedirect(true);
        return pgRef; 
    }     
 
}
The VisualForce Page:
<apex:page StandardController="MPM4_BASE__Milestone1_Project__c" tabStyle="MPM4_BASE__Milestone1_Project__c" extensions="UploadAttachmentController">
 
 <apex:sectionHeader title="{!MPM4_BASE__Milestone1_Project__c.Name}" subtitle="Attach File"/>
 
 <apex:form id="form_Upload">
 <apex:pageBlock >
 
 <apex:pageBlockButtons >
   <apex:commandButton action="{!back}" value="Back to Project"/>
   <apex:commandButton action="{!back}" value="Cancel"/>
 </apex:pageBlockButtons>
 <apex:pageMessages />
 
  <apex:pageBlockSection columns="1">
  
    <apex:pageBlockSectionItem >
      <apex:outputLabel value="File" for="file_File"/>
      <apex:inputFile id="file_File" value="{!fileBody}" filename="{!fileName}"/>
    </apex:pageBlockSectionItem>
  
    <apex:pageBlockSectionItem >
      <apex:outputLabel value="Type" for="type"/>
      <apex:inputfield value="{!attachment.Type__c}" id="type" required="true"/> 
    </apex:pageBlockSectionItem> 
    
    <apex:pageBlockSectionItem >
      <apex:outputLabel value="Description" for="description"/> 
      <apex:inputTextarea id="description" value="{!description}" rows="4" cols="50"/>
    </apex:pageBlockSectionItem>
    
    <apex:pageBlockSectionItem >
      <apex:outputLabel value="" for="uploadBtn"/> 
      <apex:commandButton id="uploadBtn" value="Attach File" action="{!processUpload}" />
    </apex:pageBlockSectionItem>    
    
  </apex:pageBlockSection>
 
 </apex:pageBlock> 
 
 </apex:form>
 
</apex:page>

 
In one of Jeff Douglas's blog posts he demonstrated how to create a custom Attachment related list for specific objects. http://blog.jeffdouglas.com/2014/05/30/how-to-customize-salesforce-attachments/ 
I've implemented that code to work with our custom Project object by following his examples, but I don't know how to write the test classes for both the VisualForce page and the Apex class so that we can put this custom object into production. I would really appreciate it if someone could provide a test class for this code or at least get me started. 

The code I've written thus far is below:

The Apex Controller:
public with sharing class UploadAttachmentController {
    
    public String selectedType {get;set;}
    public String description {get;set;}
    public MPM4_BASE__Milestone1_Project__c project {get;set;} 
    public Project_Attachment__c attachment {get;set;}
    public String fileName {get;set;}
    public Blob fileBody {get;set;}
    
    public UploadAttachmentController(ApexPages.StandardController controller) { 
        this.project = (MPM4_BASE__Milestone1_Project__c)controller.getRecord();
    }   
    
    // creates a new Project_Attachment__c record
    private Database.SaveResult saveCustomAttachment() {
        Project_Attachment__c obj = new Project_Attachment__c();
        obj.Project__c = project.Id; 
        obj.description__c = description;
        obj.type__c = attachment.Type__c;
        // fill out cust obj fields
        return Database.insert(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;
        // insert the attachment
        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
                Project_Attachment__c customAttachment = [select id from Project_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('/'+project.Id);
    }
    
    public PageReference back() {
        PageReference pgRef = new PageReference('/'+ this.project.Id);
        pgRef.setRedirect(true);
        return pgRef; 
    }     
 
}
The VisualForce Page:
<apex:page StandardController="MPM4_BASE__Milestone1_Project__c" tabStyle="MPM4_BASE__Milestone1_Project__c" extensions="UploadAttachmentController">
 
 <apex:sectionHeader title="{!MPM4_BASE__Milestone1_Project__c.Name}" subtitle="Attach File"/>
 
 <apex:form id="form_Upload">
 <apex:pageBlock >
 
 <apex:pageBlockButtons >
   <apex:commandButton action="{!back}" value="Back to Project"/>
   <apex:commandButton action="{!back}" value="Cancel"/>
 </apex:pageBlockButtons>
 <apex:pageMessages />
 
  <apex:pageBlockSection columns="1">
  
    <apex:pageBlockSectionItem >
      <apex:outputLabel value="File" for="file_File"/>
      <apex:inputFile id="file_File" value="{!fileBody}" filename="{!fileName}"/>
    </apex:pageBlockSectionItem>
  
    <apex:pageBlockSectionItem >
      <apex:outputLabel value="Type" for="type"/>
      <apex:inputfield value="{!attachment.Type__c}" id="type" required="true"/> 
    </apex:pageBlockSectionItem> 
    
    <apex:pageBlockSectionItem >
      <apex:outputLabel value="Description" for="description"/> 
      <apex:inputTextarea id="description" value="{!description}" rows="4" cols="50"/>
    </apex:pageBlockSectionItem>
    
    <apex:pageBlockSectionItem >
      <apex:outputLabel value="" for="uploadBtn"/> 
      <apex:commandButton id="uploadBtn" value="Attach File" action="{!processUpload}" />
    </apex:pageBlockSectionItem>    
    
  </apex:pageBlockSection>
 
 </apex:pageBlock> 
 
 </apex:form>
 
</apex:page>

 
Hi,

I have a checkbox field on my opportunity object which I'm trying to reference in the filter logic section of the report builder, however the field isn't showing up and I can't figure out how to access the field programmaticaly (or even if that's possible). My ultimate goal is to filter a report on Tasks based on whether or not the opportunity that the task is attached to has this checkbox field checked. Any help would be much appreciated.

Thanks in advance,

Will
In one of Jeff Douglas's blog posts he demonstrated how to create a custom Attachment related list for specific objects. http://blog.jeffdouglas.com/2014/05/30/how-to-customize-salesforce-attachments/ 
I've implemented that code to work with our custom Project object by following his examples, but I don't know how to write the test classes for both the VisualForce page and the Apex class so that we can put this custom object into production. I would really appreciate it if someone could provide a test class for this code or at least get me started. 

The code I've written thus far is below:

The Apex Controller:
public with sharing class UploadAttachmentController {
    
    public String selectedType {get;set;}
    public String description {get;set;}
    public MPM4_BASE__Milestone1_Project__c project {get;set;} 
    public Project_Attachment__c attachment {get;set;}
    public String fileName {get;set;}
    public Blob fileBody {get;set;}
    
    public UploadAttachmentController(ApexPages.StandardController controller) { 
        this.project = (MPM4_BASE__Milestone1_Project__c)controller.getRecord();
    }   
    
    // creates a new Project_Attachment__c record
    private Database.SaveResult saveCustomAttachment() {
        Project_Attachment__c obj = new Project_Attachment__c();
        obj.Project__c = project.Id; 
        obj.description__c = description;
        obj.type__c = attachment.Type__c;
        // fill out cust obj fields
        return Database.insert(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;
        // insert the attachment
        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
                Project_Attachment__c customAttachment = [select id from Project_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('/'+project.Id);
    }
    
    public PageReference back() {
        PageReference pgRef = new PageReference('/'+ this.project.Id);
        pgRef.setRedirect(true);
        return pgRef; 
    }     
 
}
The VisualForce Page:
<apex:page StandardController="MPM4_BASE__Milestone1_Project__c" tabStyle="MPM4_BASE__Milestone1_Project__c" extensions="UploadAttachmentController">
 
 <apex:sectionHeader title="{!MPM4_BASE__Milestone1_Project__c.Name}" subtitle="Attach File"/>
 
 <apex:form id="form_Upload">
 <apex:pageBlock >
 
 <apex:pageBlockButtons >
   <apex:commandButton action="{!back}" value="Back to Project"/>
   <apex:commandButton action="{!back}" value="Cancel"/>
 </apex:pageBlockButtons>
 <apex:pageMessages />
 
  <apex:pageBlockSection columns="1">
  
    <apex:pageBlockSectionItem >
      <apex:outputLabel value="File" for="file_File"/>
      <apex:inputFile id="file_File" value="{!fileBody}" filename="{!fileName}"/>
    </apex:pageBlockSectionItem>
  
    <apex:pageBlockSectionItem >
      <apex:outputLabel value="Type" for="type"/>
      <apex:inputfield value="{!attachment.Type__c}" id="type" required="true"/> 
    </apex:pageBlockSectionItem> 
    
    <apex:pageBlockSectionItem >
      <apex:outputLabel value="Description" for="description"/> 
      <apex:inputTextarea id="description" value="{!description}" rows="4" cols="50"/>
    </apex:pageBlockSectionItem>
    
    <apex:pageBlockSectionItem >
      <apex:outputLabel value="" for="uploadBtn"/> 
      <apex:commandButton id="uploadBtn" value="Attach File" action="{!processUpload}" />
    </apex:pageBlockSectionItem>    
    
  </apex:pageBlockSection>
 
 </apex:pageBlock> 
 
 </apex:form>
 
</apex:page>