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
rameshramesh 

how we can move notes and attachments from one account to another account through code? Kindly help me on this.

Hi All,
how we can move notes and attachments from one account to another account through code?
Kindly help me with this.
Best Answer chosen by ramesh
SwethaSwetha (Salesforce Developers) 
I see you are using LightningExperience.The above code applies for Classic.

As per https://help.salesforce.com/s/articleView?id=000380667&type=1,
"In Lightning regardless of whether the feature is on or off, uploading a document in the Notes and Attachments related list will still result in the document being added as a Salesforce File."

This implies you need to transfer files between accounts. Files are stored in the ContentDocument Object .You can make use of below code to get started
 
// Query ContentDocumentLink records related to source account
List<ContentDocumentLink> sourceLinks = [SELECT Id, ContentDocumentId, LinkedEntityId FROM ContentDocumentLink WHERE LinkedEntityId = '001XXXXXXXXXXXX'];

// Loop through queried records and create new records for target account
List<ContentDocumentLink> targetLinks = new List<ContentDocumentLink>();

for (ContentDocumentLink sourceLink : sourceLinks) {
    ContentDocumentLink targetLink = new ContentDocumentLink();
    targetLink.ContentDocumentId = sourceLink.ContentDocumentId;
    targetLink.LinkedEntityId = '001YYYYYYYYYYYYY'; // set to target account Id
    targetLinks.add(targetLink);
}

// Insert new records
insert targetLinks;
Related: https://salesforce.stackexchange.com/questions/138860/how-to-query-a-file-uploaded-as-an-attachment-in-lightning-experience

https://theblogreaders.com/insert-files-using-salesforce-apex/

If this information helps, please mark the answer as best. Thank you

All Answers

SwethaSwetha (Salesforce Developers) 
HI Saki,
You can try below code snippet and customize as per your requirement
// Query notes and attachments related to source account
List<Note> notes = [SELECT Id, Title, Body, ParentId FROM Note WHERE ParentId = '0016F00002RVwDO'];
List<Attachment> attachments = [SELECT Id, Name, Body, ParentId FROM Attachment WHERE ParentId = '0016F00002RVwDO'];

// Loop through queried records and create new records for target account
List<Note> newNotes = new List<Note>();
List<Attachment> newAttachments = new List<Attachment>();

for (Note note : notes) {
    Note newNote = new Note();
    newNote.Title = note.Title;
    newNote.Body = note.Body;
    newNote.ParentId = '0016F00003o8wM3'; // set to target account Id
    newNotes.add(newNote);
}

for (Attachment attachment : attachments) {
    Attachment newAttachment = new Attachment();
    newAttachment.Name = attachment.Name;
    newAttachment.Body = attachment.Body;
    newAttachment.ParentId = '0016F00003o8wM3'; // set to target account Id
    newAttachments.add(newAttachment);
}

// Insert new records
insert newNotes;
insert newAttachments;

Basically, the idea here is to Query the notes and attachments related to the source account and filter by the ParentId field. Then loop through the queried records and create new Note and Attachment records with the same information, but with the ParentId set to the Id of the target account. Then Insert the newly created records

Related:
https://ideas.salesforce.com/s/idea/a0B8W00000GdiJ0UAJ/transfer-notes-attachments-to-another-object

https://developer.salesforce.com/forums/?id=906F0000000DAEIIA4

If this information helps, please mark the answer as best. Thank you

 
rameshramesh
its not working i already tried 
rameshramesh
parent id means account id only na
 
SwethaSwetha (Salesforce Developers) 
That is correct. Parentid here is the id of the source account from which you would want to transfer the notes & attachments.

Note that this code snippet does not delete the notes & attachments from your source account. But copies to target account. Thanks  
rameshramesh
but its not working 
rameshramesh
in the account object I opened account record went to related notes and attachments I created new attachment ,
i Query using same account Id as parent id  its show null values
rameshramesh
User-added image
SwethaSwetha (Salesforce Developers) 
I see you are using LightningExperience.The above code applies for Classic.

As per https://help.salesforce.com/s/articleView?id=000380667&type=1,
"In Lightning regardless of whether the feature is on or off, uploading a document in the Notes and Attachments related list will still result in the document being added as a Salesforce File."

This implies you need to transfer files between accounts. Files are stored in the ContentDocument Object .You can make use of below code to get started
 
// Query ContentDocumentLink records related to source account
List<ContentDocumentLink> sourceLinks = [SELECT Id, ContentDocumentId, LinkedEntityId FROM ContentDocumentLink WHERE LinkedEntityId = '001XXXXXXXXXXXX'];

// Loop through queried records and create new records for target account
List<ContentDocumentLink> targetLinks = new List<ContentDocumentLink>();

for (ContentDocumentLink sourceLink : sourceLinks) {
    ContentDocumentLink targetLink = new ContentDocumentLink();
    targetLink.ContentDocumentId = sourceLink.ContentDocumentId;
    targetLink.LinkedEntityId = '001YYYYYYYYYYYYY'; // set to target account Id
    targetLinks.add(targetLink);
}

// Insert new records
insert targetLinks;
Related: https://salesforce.stackexchange.com/questions/138860/how-to-query-a-file-uploaded-as-an-attachment-in-lightning-experience

https://theblogreaders.com/insert-files-using-salesforce-apex/

If this information helps, please mark the answer as best. Thank you
This was selected as the best answer
rameshramesh
Thanks Swetha its working fine