1

We would like to Clone Opps, currently it asks if we would like to clone with or without Products, but we need it to also ask if we would like to clone the notes/attachments. How can we achieve this without purchasing an app?

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);

}
}
  • 2
    How is the code you have included relevant to your question? – Keith C Aug 25 '15 at 20:12
  • It was shared with me to use possibly from the below site: developer.force.com/projectpage?id=a063000000EWWYrAAP – JazzyJ Aug 25 '15 at 20:24
  • 1
    you'll need a VF page to prompt user for whether to clone products, notes, attachments and then override the Clone button on Opportunity – cropredy Aug 25 '15 at 23:59
  • Well after hearing the new UI features, I wonder if I should wait out creating a VF page to avoid complications of compatibility later. However I do need a solution, any documents or links you can point me towards to create this type of VF page? I am very inexperienced in dev skills and a solo admin here. – JazzyJ Aug 26 '15 at 15:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.