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
vivek ravivivek ravi 

can we send the notes and attachment of custom object to contact in accounts

im trying to send the notes and attachment of custom object to the contacts of accounts through a apex page if any one did this before can u refere me any blog r code for this im working this requriment about 2 days bt i can send the mail bt i dnt knw how to fetch the notes with the mail
can any knw pls help me in this requirement
Ankit GuptaAnkit Gupta
Hi Vivek,

I have done this earlier where I had a custom object Brand__c on which there was a button Transfer Attachments which will redirect to a VF Page from where a user can select one or more contacts to which the attachments have to be transferred. I was passing the Brand record Id in the URL when a person clicks on transfer attachments.

I am sharing my code below.

Apex class : 
public class ctrlTransferAttachments {
    public Boolean selectedCheckbox { get; set; }
    public SET<String> ids{get;set;}
    List<Attachment> attachmentList=new List<Attachment>();
    public String conId{
        set{
            if(selectedCheckbox==true){
                ids.add(value);
            }
            if(selectedCheckbox==false){
                ids.remove(value);
            }
        }
        get{return conid;}
    }   
    public ctrlTransferAttachments (){
    ids=new SET<String>();
    }
       
    public List<Contact> getContactList(){
        List<Contact> listCon =[SELECT Id,Name from Contact LIMIT 10];
        attachmentList =[SELECT Name,id,Body,ParentId from attachment where ParentId = :ApexPages.currentPage().getParameters().get('bid')];
        return listCon;
    }
   
    public void SaveAttachments() {
    List<Attachment> attachmentTemp =new List<Attachment>();  
        for(Attachment a : attachmentList ){
            for(String s : ids){
                Attachment attch=new Attachment(Name= a.Name ,Body=a.Body,ParentId =s);
                attachmentTemp .add(attch);
            }
        }
        insert attachmentTemp;
        ids.clear();
        selectedCheckbox=false;
        if(!attachmentTemp.IsEmpty() )
            ids.add('Successfully inserted');
        if(attachmentList.IsEmpty())
            ids.add('No Attachment exists for this record');
    }   
   
}

VF Page : 
<apex:page controller="ctrlTransferAttachments"> 
  <h1>Select the contacts you want to transfer to</h1>
<br/><br/>
  <apex:form >
  <apex:pageBlock >
  <apex:pageBlockTable value="{!ContactList}" var="c" id="pgblk"> 
  <apex:column value="{!c.Name}"></apex:column>
  <apex:column value="{!c.Id}"></apex:column>
  <apex:column >
      <apex:inputCheckbox value="{!selectedCheckbox}" >
      <apex:actionSupport event="onchange" reRender="selid">
          <apex:param value="{!c.id}" name="selectedContact" assignTo="{!conid}"/>
          </apex:actionSupport>
      </apex:inputCheckbox>
  </apex:column> 
  </apex:pageBlockTable>   
</apex:pageBlock>
<apex:commandButton value="Submit" action="{!SaveAttachments}"/>
</apex:form>

   <br/><br/>
Selected Ids are
<apex:dataList value="{!ids}" var="i" id="selid">
     <apex:outputText value="{!i}"></apex:outputText>
</apex:dataList>
</apex:page>

Hope this helps.

Regards,
Ankit Gupta