• Carlos Naranjo
  • NEWBIE
  • 0 Points
  • Member since 2014

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 2
    Questions
  • 6
    Replies

Hello all,

I was hoping that someone here could help me with an issue that it is driving me crazy.

I have a visualforce page that will allow users to insert to picture and view those pictures right after insertion. At the sametime those picture will be save on the Note & Attachment section within the object.

All this is done on a custom object with and extension to use the attachment object.

The wired thing is that if I save the first picture (pictureOne on the code) I received the page message telling me that it is been inserted successfully and the picture. That is all I see, it will remove the "Browse" from the second picture and even the one for the first. This is annoying because then the user needs to refresh the page to get the options to insert a second picture or to select another picture for the pictureOne.


-------------------------------------

public with sharing class AttachmentController {
   
   String recId;
   public boolean con { get; set; }
   public boolean con2 { get; set; }
   
   public AttachmentController(ApexPages.StandardController controller) {
   recId = controller.getId(); 
         

    }
      
   public Attachment pictureOne {
       get {
          if (pictureOne == null)
          pictureOne = new Attachment();
          return pictureOne;
           }
      set;
  }
 
   
  public PageReference savePictureOne() {

    pictureOne.OwnerId = UserInfo.getUserId();
    pictureOne.ParentId = ApexPages.currentPage().getParameters().get('id');
    pictureOne.Name = 'W1' + pictureOne.name;
   

    try {
     
      insert pictureOne;
      con = true;
     
    } catch (DMLException e) {
      ApexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR,'Error uploading attachment'));
      return null;
    } finally {
      pictureOne = new Attachment();
      
      
    }

    ApexPages.addMessage(new ApexPages.message(ApexPages.severity.INFO,'Attachment uploaded successfully'));
    return null;
   
    }   
   
    public String getFileId() {
        String fileId = '';
        List<Attachment> attachedFiles = [select Id from Attachment where Name LIKE 'W1%' AND parentId =:recId order By LastModifiedDate DESC limit 1];
        if( attachedFiles != null && attachedFiles.size() > 0 ) {
            fileId = attachedFiles[0].Id;
        }
        return fileId;   
    }
   
   
   
   
     public Attachment pictureTwo {
       get {
          if (pictureTwo == null)
          pictureTwo = new Attachment();
          return pictureTwo;
           }
      set;
    }
    public PageReference savePictureTwo() {

    pictureTwo.OwnerId = UserInfo.getUserId();
    pictureTwo.ParentId = ApexPages.currentPage().getParameters().get('id');
    pictureTwo.Name = 'W2' + pictureTwo.name;
    

    try {
     
      insert pictureTwo;
      con2 = true;
     
    } catch (DMLException e) {
      ApexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR,'Error uploading attachment'));
      return null;
    } finally {
      pictureTwo = new Attachment();
      
      
    }

    ApexPages.addMessage(new ApexPages.message(ApexPages.severity.INFO,'Attachment uploaded successfully'));
    return null;
   
    }   
   
    public String getFileId2() {
        String fileId2 = '';
        List<Attachment> attachedFiles = [select Id from Attachment where Name LIKE 'W2%' AND parentId =:recId order By LastModifiedDate DESC limit 1];
        if( attachedFiles != null && attachedFiles.size() > 0 ) {
            fileId2 = attachedFiles[0].Id;
        }
        return fileId2;   
    }
}


Visualforce page:

<apex:page StandardController="Visit_Report__c"  extensions="AttachmentController" showHeader="false">
     <apex:form id="theForm">
       <apex:pageBlock > <apex:pageMessages />
           <table>
             <tr>
              <td>
              <apex:outPutPanel id="imagePanel" rendered="{!FileId!=null}">
                 <img id="theImage"  src="/servlet/servlet.FileDownload?file={!FileId}" style="vertical-align:left" width="250" height="300" onclick="window.open('/servlet/servlet.FileDownload?file={!FileId}');"/>
              </apex:outPutPanel>
                
              <apex:outPutPanel id="thePanel" rendered="{!con == false}">
                 <apex:pageBlockSection >
                 <apex:inputFile value="{!pictureOne.body}" filename="{!pictureOne.name}"/>
                 <apex:commandButton value="Save" action="{!savepictureOne}" /><br />
                 </apex:pageBlockSection>
              </apex:outPutPanel>
              
              </td>
            <td>
              <apex:outPutPanel id="imagePanel2" rendered="{!FileId2!=null}">
                      <img id="theImage2"  src="/servlet/servlet.FileDownload?file={!FileId2}" style="vertical-align:left" width="250" height="300" onclick="window.open('/servlet/servlet.FileDownload?file={!FileId2}');"/>
                 </apex:outPutPanel>
                
               <apex:outPutPanel id="thePanel2" rendered="{!con == false}">
                   <apex:pageBlockSection >
                   <apex:inputFile value="{!pictureTwo.body}" filename="{!pictureTwo.name}"/>
                   <apex:commandButton value="Save" action="{!savepictureTwo}" /><br />
                   </apex:pageBlockSection>
               </apex:outPutPanel>
           
                </td>
             </tr>
           </table>
        </apex:pageBlock>
    </apex:form>
</apex:page>

Anyone knwos why this is happening?
Hello all,

Just a question and some guidance.
I have this code and the Visualforce page that goes with it. I was wondering if the structure is good enough of if there are things that could be improve.
The code works well and all but I'm sure it could be better, so any tips on it will be very much welcome.
Thanks.

This the controller:

public with sharing class AttachmentController {
   
   String attachId;
   
   public AttachmentController(ApexPages.StandardController controller) {
   attachId = controller.getId();         

    }
      
   public Attachment attachment {
       get {
          if (attachment == null)
          attachment = new Attachment();
          return attachment;
           }
      set;
  }

  public PageReference save() {

    attachment.OwnerId = UserInfo.getUserId();
    attachment.ParentId = ApexPages.currentPage().getParameters().get('id');
   

    try {
     
      insert attachment;
     
    } catch (DMLException e) {
      ApexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR,'Error uploading attachment'));
      return null;
    }
  
     finally {

      attachment = new Attachment();
         
    }

    ApexPages.addMessage(new ApexPages.message(ApexPages.severity.INFO,'Attachment uploaded successfully'));
    return null;
    }
 
   public String getFileId() {
        String fileId = '';
        List<Attachment> attachedFiles = [select Id from Attachment where parentId =:attachId order By LastModifiedDate DESC limit 1];
        if( attachedFiles != null && attachedFiles.size() > 0 ) {
            fileId = attachedFiles[0].Id;
        }
        return fileId;   
    }
 

  
}



This is the visualforce page:

<apex:page StandardController="Whatever__c"  extensions="AttachmentController" showHeader="false">
    <apex:form id="theForm">
       <apex:pageBlock >
           <table>
             <td>
               <apex:image url="/servlet/servlet.FileDownload?file={!FileId}" style="vertical-align:left"  width="500" height="550" />
                 <apex:pageBlockSection >
                   <apex:inputFile value="{!attachment.body}" filename="{!attachment.name}"/>
                   <apex:commandButton value="Save" action="{!save}" /><br />
                   </apex:pageBlockSection>
               </td>
            </table>
        </apex:pageBlock>
    </apex:form>
   </apex:page>
-----------------------------------
Thanks
Hello all,

Just a question and some guidance.
I have this code and the Visualforce page that goes with it. I was wondering if the structure is good enough of if there are things that could be improve.
The code works well and all but I'm sure it could be better, so any tips on it will be very much welcome.
Thanks.

This the controller:

public with sharing class AttachmentController {
   
   String attachId;
   
   public AttachmentController(ApexPages.StandardController controller) {
   attachId = controller.getId();         

    }
      
   public Attachment attachment {
       get {
          if (attachment == null)
          attachment = new Attachment();
          return attachment;
           }
      set;
  }

  public PageReference save() {

    attachment.OwnerId = UserInfo.getUserId();
    attachment.ParentId = ApexPages.currentPage().getParameters().get('id');
   

    try {
     
      insert attachment;
     
    } catch (DMLException e) {
      ApexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR,'Error uploading attachment'));
      return null;
    }
  
     finally {

      attachment = new Attachment();
         
    }

    ApexPages.addMessage(new ApexPages.message(ApexPages.severity.INFO,'Attachment uploaded successfully'));
    return null;
    }
 
   public String getFileId() {
        String fileId = '';
        List<Attachment> attachedFiles = [select Id from Attachment where parentId =:attachId order By LastModifiedDate DESC limit 1];
        if( attachedFiles != null && attachedFiles.size() > 0 ) {
            fileId = attachedFiles[0].Id;
        }
        return fileId;   
    }
 

  
}



This is the visualforce page:

<apex:page StandardController="Whatever__c"  extensions="AttachmentController" showHeader="false">
    <apex:form id="theForm">
       <apex:pageBlock >
           <table>
             <td>
               <apex:image url="/servlet/servlet.FileDownload?file={!FileId}" style="vertical-align:left"  width="500" height="550" />
                 <apex:pageBlockSection >
                   <apex:inputFile value="{!attachment.body}" filename="{!attachment.name}"/>
                   <apex:commandButton value="Save" action="{!save}" /><br />
                   </apex:pageBlockSection>
               </td>
            </table>
        </apex:pageBlock>
    </apex:form>
   </apex:page>
-----------------------------------
Thanks

I am trying to create a Vforce page that will pop up a notice to the user, Is it possible to get some sample code for that?

The idea would be to popup a message to the user after they complete some require fields on a custom object.

Hi All, 

 

There are plenty of code online that looks at inserting Attachments into Salesforce objects however I have a requirement to do something a little different to this.

 

Whenever a file is manually uploaded onto a Case object as an Attachment on the Case record, I would like to have a trigger that inserts the same Attachment into the Chatter Feed as a File on the same Case record. Although I can write the trigger of picking up the Attachments fine, I do not know where to start in APEX if I wanted to insert this Chatter Feed File using the trigger.

 

Could someone please direct me on what standared objects and calls I should be making to insert this Feed File?

 

Many thanks!

I have one controller for my VF Page.
m doing insert operation from controller...
using
insert myobject;
now i want to fetch and set RecordId which is generated from this insert statement into one variable ("newId") in controller.
how can i do this?