function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
BobBob 

Extension controller for new account record new attachment

I am trying to figure out how to modify a extension class I found on a Salesforce blog.

 

I am trying to modify it to add a attachment to a new record.  The comments made in order to accomplish this function is to

"If you want to attach the uploads to a new, not yet extant, object, make the "myAttachment" variable belong to the controller, not just scoped within the upload function. Then the attachment remains as part of the controller until the controller finally runs save(). 

 

Within your controller's save() function you do this:

insert myNewRecord;
myAttachment.parentId = myNewRecord.Id
insert myAttachment; "

 

 

 

I cannot figure out how to accomplish this, can anyone help organize the code to create a new record with an attachment? The rest of the code is below.

 

 

Controller:

 

public class VFFileUpload  
{  
    public Id recId  
    {    get;set;    }  
      
    public VFFileUpload(ApexPages.StandardController ctlr)  
    {  
       recId = ctlr.getRecord().Id;       
    }  
      
    public string fileName   
    {    get;set;    }  
      
    public Blob fileBody   
    {    get;set;    }  
    
    public PageReference UploadFile()  
    {  
        PageReference pr;  
        if(fileBody != null && fileName != null)  
        {  
          Attachment myAttachment  = new Attachment();  
          myAttachment.Body = fileBody;  
          myAttachment.Name = fileName;  
          myAttachment.ParentId = recId;  
          insert myAttachment;  
           pr = new PageReference('/' + myAttachment.Id);  
           pr.setRedirect(true);  
           return pr;  
        }  
        return null;  
    }      
}

 

____________________________________

 

Visualforce Code:

 

<apex:page standardController="Account" extensions="VFFileUpload">  
  <apex:form >
  <apex:pageBlock title="A Block Title">
 
   <apex:outputLabel >Account Name </apex:outputLabel>
  <apex:inputField value="{!account.Name}"/>
  <apex:outputLabel >Account Phone </apex:outputLabel><br/>
  <apex:inputField value="{!account.Phone}"/><br/>
  </apex:pageBlock>
      <apex:pageBlock title="Upload Attachment">  
            <apex:inputFile style="width:100%" id="fileToUpload" value="{!fileBody}" filename="{!fileName}" />  
            <apex:commandButton value="Upload Attachment" action="{!UploadFile}"/>  
       </apex:pageBlock>  
  </apex:form>  
</apex:page>

 

 

Best Answer chosen by Admin (Salesforce Developers) 
JHayes SDJHayes SD

A few things:

 

1. You need a handle on the controller so you can upsert the account.

2. You need to initialize a controller variable of type Attachment.

3. You need to to update the set methods on your properties to update the Attachment record.

 

Replace your controller code with this, it worked for me:

 

public class VFFileUpload  
{  
    ApexPages.StandardController sc = null;
      
    public VFFileUpload(ApexPages.StandardController ctlr)  
    {  
       sc = ctlr;             
    }  
      
    public string fileName   
    {    get;
    	 set {
    	 	fileName = value;
    	 	myAtt.name = value;
    	 }
   	}  
      
    public Blob fileBody   
    {    get;
    	 set {
    	 	fileBody = value;
    	 	myAtt.body = value;
    	 }
   	}    
    
    Attachment myAtt = new Attachment();
    
    public PageReference UploadFile()  
    {  
        PageReference pr;  
        if(fileBody != null && fileName != null)  
        { 
          Account acct = (Account) sc.getRecord();
          upsert acct;                  
          myAtt.ParentId = acct.Id;  
          insert myAtt;  
          pr = new PageReference('/' + myAtt.Id);  
          pr.setRedirect(true);  
          return pr;  
        }  
        return null;  
    }      
}

 Regards, Jeremy

All Answers

JHayes SDJHayes SD

A few things:

 

1. You need a handle on the controller so you can upsert the account.

2. You need to initialize a controller variable of type Attachment.

3. You need to to update the set methods on your properties to update the Attachment record.

 

Replace your controller code with this, it worked for me:

 

public class VFFileUpload  
{  
    ApexPages.StandardController sc = null;
      
    public VFFileUpload(ApexPages.StandardController ctlr)  
    {  
       sc = ctlr;             
    }  
      
    public string fileName   
    {    get;
    	 set {
    	 	fileName = value;
    	 	myAtt.name = value;
    	 }
   	}  
      
    public Blob fileBody   
    {    get;
    	 set {
    	 	fileBody = value;
    	 	myAtt.body = value;
    	 }
   	}    
    
    Attachment myAtt = new Attachment();
    
    public PageReference UploadFile()  
    {  
        PageReference pr;  
        if(fileBody != null && fileName != null)  
        { 
          Account acct = (Account) sc.getRecord();
          upsert acct;                  
          myAtt.ParentId = acct.Id;  
          insert myAtt;  
          pr = new PageReference('/' + myAtt.Id);  
          pr.setRedirect(true);  
          return pr;  
        }  
        return null;  
    }      
}

 Regards, Jeremy

This was selected as the best answer
BobBob

Thanks Jeremy!!

 

Your code worked perfectly. This will help me with other request I have as well.