Friday, August 12, 2011

Sending Email With Salesforce Content as Attachment

Hey Guys,

Finally i did it.I had to send multiple salesforce contents as attachments with the Email.
I was stucked for 3 days and finally its Done.

tada here is the help.....

  ///tempList is a list that contains that contains the ContentDocumentIds of the selected Contents

  Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
  Contact contact = [select Email from Contact where ID=:test.ContactTo__c];
  toaddress.add(contact.email);

  mail.setToAddresses(toaddress);
  mail.setSubject(subject);
  mail.setPlainTextBody(body.replace('&','&'));
  mail.setUseSignature(false);

  Messaging.EmailFileAttachment[] attach = new Messaging.EmailFileAttachment[tempList.size()];
 
  ContentVersion[] contentVersion = new ContentVersion[tempList.size()];
               
  for(integer i=0;i<tempList.size();i++)
        {
          Blob attachBody;
 
          Messaging.EmailFileAttachment efa = new Messaging.EmailFileAttachment();
          String temp1 = tempList[i];
 
          contentVersion[i] = [Select id,Title,VersionData,PathOnClient from
                                ContentVersion WHERE ContentDocumentId =: temp1];

          attachBody = contentVersion[i].VersionData;
 
          String str = contentVersion[i].PathOnClient
                                        .substring(contentVersion[i].PathOnClient.indexOf('.')+1
                                        ,contentVersion[i].PathOnClient.length());

          efa.fileName = contentVersion[i].Title+'.'+str;
          efa.setInline(false);
          efa.Body=attachBody;
          attach[i]=efa;
        }
       
        
  if(tempList.size()>0)
          mail.setFileAttachments(attach);

  Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail },false);


Hope it helps.

Enjoy Salesforcing......

Get a list of all SalesForce objects in an Organization

Hello all,
I had spend a lots of time in getting a list of SalesForce Objects including the custom ones.
I know its fairly simple but may be it can provide help to noobs like me.