+ Start a Discussion
re_plagasre_plagas 

Cloning Notes & attachments in my custom object

Hi i wanted to clone my Notes & Attachment together when I clicked the main "Clone" button available at the details page of my custom object record.

 

But it seems that only my record is being cloned and not the related list. Was wondering if there is any ways to clone both the records as well as the Notes & Attachment to the new record.

 

E.g Record A, with Notes A,B & C

 

I wanna clone Record A together with Notes A,B,C

 

Please help out thx!

super developersuper developer

For this you need to customize this button

with vfpage.

re_plagasre_plagas

What shld I input in my VF page? As for the custom button, I know how to create one.

 

Was wondering if any kind soul would give me the codes for it?

super developersuper developer

You need to pass the current record id to vf page in that onload write a action method create a new record insert with all data of passed record (there itself insert notes and attachments)and return page reference newlycreatedid/e?returl?=newlycreatedid

re_plagasre_plagas

I'm not too sure as to how to pass the id of the record to the VF page. Must I indicate some reference to get the record id @ the custom button I made or at the very VF page itself?

 

thx

super developersuper developer

You need to call the vf page from custom button at the time of creation.(do you know this).

 

for example:

JavaScript

 

window.location.replace('/apex/yourvfpage?id='+{!customobject.id});

 And write vfpage and apex class.

re_plagasre_plagas

Ok....I got this codes at my custom button

 

window.open.replace("/apex/Progress?id="+{!Handoff__c.Id},
"Progress Window","menubar=1,resizable=1,width=390,height=250")

 

but it seem they came up with this error:

 

a019000000179h8 is not defined

 

a019000000179h8 is the id of my custom object record.

 

Do I need to add anything in the vfpage and apex class in order to get this working?

super developersuper developer

did you capture the parameters in apex class constructor.

 

like

Apexpages.currentpage.getparameters().get('id');

any thing like this in youe apex class.

 

re_plagasre_plagas

Erm nope. i'm very new to this Apex codings so I'm not quite sure of what to be input into the Apex class such that It'll clone my records as well as the related lists.

 

Please guide me along with sample codes would be freat :)

super developersuper developer

Hi,

 

it's too easy.

 

try if not i will provide you later.

re_plagasre_plagas

U are great help

re_plagasre_plagas

please help me out with any sample codes thx alot!

Imran MohammedImran Mohammed

 

Hi,

 

I did a sample example using Opportunity. You can replace opportunity object with the object you want to clone with.

 

//Add the desired fields to your query both for custom object and child objects

 Opportunity[] oppList = [select id, name, stagename, closedate, (select id, title, body from Notes) from Opportunity where name like 'Global%'];

SOBject[] sobjList = oppList;
SObject[] cloneList = new SObject[]{};
Sobject sObj;
Map<String, List<note>> toBeClonedNotes = new Map<String, List<note>>();
for(SObject s: sobjList)
{
  sobj = s.clone(false, true);
  cloneList.add(sobj);
  Opportunity o = (Opportunity)s;
  toBeClonedNotes.put(o.name, o.notes);
  
}
insert cloneList;
List<note> notesListToBeInserted = new List<note>();
for(Opportunity o: (Opportunity[])cloneList)
{
List<note> tempNotesList = toBeClonedNotes.get(o.name);
for(note a: tempNotesList)
{
note tempA = new note(title= a.title, body = a.body, parentid = o.id);
notesListToBeInserted.add(tempA);
}
}
insert notesListToBeInserted;

 

 

Opportunity[] oppList = [select id, name, stagename, closedate, (select id, title, body from Notes) from Opportunity where id = :yourObjectID];

SOBject[] sobjList = oppList;

SObject[] cloneList = new SObject[]{};

 

Sobject sObj;

Map<String, List<note>> toBeClonedNotes = new Map<String, List<note>>();

for(SObject s: sobjList)

{

  sobj = s.clone(false, true);

  cloneList.add(sobj);

  Opportunity o = (Opportunity)s;

  toBeClonedNotes.put(o.name, o.notes);

 

}

insert cloneList;

 

//Here is the code to clone Notes for each cloned object.

List<note> notesListToBeInserted = new List<note>();

for(Opportunity o: (Opportunity[])cloneList)

{

List<note> tempNotesList = toBeClonedNotes.get(o.name);

for(note a: tempNotesList)

{

note tempNotes = new note(title= a.title, body = a.body, parentid = o.id);

notesListToBeInserted.add(tempNotes);

 

}

}

insert notesListToBeInserted;

 

 

Also i did it for Notes. Please follow the same for Attachments. I think you have to do some additional work for Attachments.

 

Please let me know if you have any queries.

 

Imran MohammedImran Mohammed

Make the changes to the above code accordingly to your requirement.

super developersuper developer

Did you got that !