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
ZurMilesZurMiles 

Get ID from attachment that has been uploaded

Hello all,

 

I'm trying to send an email from a Visualforce page after the user is done with a form. All this is working just fine. 

Now I need to add the possibility of adding an attachment to that form.  I use this code together with a visual force page and it works.

 

 public PageReference upload() {
      
      attachment.ParentId = '500Q0000004eDnT';
      attachment.IsPrivate = true;
      
      
    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;
    
  }
  

 The problem is that now I need to attach that attachment to the email that will be send after the form is done and the attachment is uploaded. 

I was thinking on doing a SOQL on Attachments, something like this:

 

Attachment attachmentToSend = [select Body, Id, Name, ContentType from Attachment where Parentid=:' 500Q0000004eDnt'  FROM <------- after this the problem starts for me. 

 

Here is the question:

 

How can I get the id of that attachment that have been just uploaded and put it in some place to use it for my query in order for me to get the correct attachment for that email that will be send. 

I can query for all attachment but I need to get the id related to the one the user uploaded

 

Thanks

Vinit_KumarVinit_Kumar

Hi,

 

Change your SOQL from

 

Attachment attachmentToSend = [select Body, Id, Name, ContentType from Attachment where Parentid=:' 500Q0000004eDnt'];

 

to

 

Attachment attachmentToSend = [select Body, Id, Name, ContentType from Attachment where Parentid=' 500Q0000004eDnt' and CreatedById =:UserInfo.getUserId()];

Teach_me_howTeach_me_how
try using eclipse create trigger on attachment and ofcourse run only this trigger for the records who has parentid that you expected
ZurMilesZurMiles
Hello Vinit,

but the CreateById =:USerInfo.getUSerId()]; only works for records that are in SFDC, right, I don't think it will work.
The visual force page is in a web site and all that it does is to send a service request and there is not login needed, they are not contacts, they can be anyone.
There is not link between that visualforce page and any object in our org.
If a client have a problem with any of our equipment they just log a services request and they can use any name. After submitting this request a email will be send to our service department and they will get in contact with the user.

Teach me how, can't understand what you are proposing.
ZurMilesZurMiles
No one? :(
ZurMilesZurMiles

There is a way for this scrip to retrive the ID after the file is been uploaded? 

 

    
    public Attachment attachment {
  get {
      if (attachment == null)
          attachment = new Attachment();
          
      return attachment;
    }
  set;
  }
 
  public PageReference upload() {
      attachment.ParentId = '500Q0000004eDnT';
      attachment.IsPrivate = true;
      Id attId;
      attId = attachment.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;
    
  }

 Makes any sense to add these lines under the upload() method? 

 

Id attId;

attId - attachment.id; 

 

 

 

I need help with this guys ... :(