+ Start a Discussion
re_plagasre_plagas 

[URGENT!!] Cloning Notes & attachments in my custom object

Hi ppl, I need some sample codes to clone notes & attachment together with my records in the custom object.

 

Please help me out its urgent

Best Answer chosen by Admin (Salesforce Developers) 
Imran MohammedImran Mohammed

Hi,

 

Please use the below code

 

 

public class Hclone {
private ID hID;
public HClone()
{
PageReference pg = Apexpages.currentPage();
currentPageURL = pg.getUrl();
hID = pg.getParameters().get('id');
}
public doProcess()
{
//Add the desired fields to your query both for custom object and child objects
Handoff__c[] oppList = [select id, name, stagename, closedate, (select id, title, body from Notes) from Handoff__c where id = :hID];
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);
Handoff__c o = (Handoff__c)s;
toBeClonedNotes.put(o.name, o.notes);
}
insert cloneList;
List<note> notesListToBeInserted = new List<note>();
for(Handoff__c o: (Handoff__c[])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;
Handoff__c[] oppList = [select id, name, stagename, closedate, (select id, title, body from Notes) from Handoff__c 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);
Handoff__c o = (Handoff__c)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(Handoff__c o: (Handoff__c[])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;
}
}

public class Hclone {

 

private ID hID;

public HClone()

{

PageReference pg = Apexpages.currentPage();

currentPageURL = pg.getUrl();

hID = pg.getParameters().get('id');

}

 

public doProcess()

{

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

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

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

Handoff__c o = (Handoff__c)s;

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

 

}

 

insert cloneList;

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

for(Handoff__c o: (Handoff__c[])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;

 

 

Handoff__c[] oppList = [select id, name, stagename, closedate, (select id, title, body from Notes) from Handoff__c 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);

 

Handoff__c o = (Handoff__c)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(Handoff__c o: (Handoff__c[])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;

 

 

 

}

}

 

 

Let me know if you face any issues.

All Answers

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.

Imran MohammedImran Mohammed

Did you give a try to the code posted or was it unclear?

re_plagasre_plagas

Sry I just saw the post this morning. Was offline since ytd evening. Will try out the codes and let u know

re_plagasre_plagas

I got this error code:

 

 

Error: Compile Error: sObject type 'Handoff' is not supported. If you are attempting to use a custom object, be sure to append the '__c' after the entity name. Please reference your WSDL or the describe call for the appropriate names. at line 6 column 25

 

The codes:

 

 

public class Hclone {

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

 
    Handoff[] oppList = [select id, name, stagename, closedate, (select id, title, body from Notes) from Handoff 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);
    Handoff o = (Handoff)s;
    toBeClonedNotes.put(o.name, o.notes);
 
    }
    insert cloneList;
    List<note> notesListToBeInserted = new List<note>();
    for(Handoff o: (Handoff[])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;

 

    Handoff[] oppList = [select id, name, stagename, closedate, (select id, title, body from Notes) from Handoff 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);

    Handoff o = (Handoff)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(Handoff o: (Handoff[])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;
    
    }

Imran MohammedImran Mohammed

If HandOff is a custom object you have to append __c at the end like Handoff__c.

Let me know if you have further queries or doubts.

re_plagasre_plagas

Hi thx for replying back to my queries.

 

It's the same even putting _c at the back of Handoff realizing that since it is a custom object to begin with.

Imran MohammedImran Mohammed

I dont know whether to put this question as it will be basic one.

Did you use single underscore( _ ) or double underscore (  __ )

re_plagasre_plagas

Apparently I put double underscore __c the error went away. But now it's this error:

 

Error: Compile Error: expecting right curly bracket, found 'for' at line 11 column 4

 

Line 11:  for(SObject s: sobjList)

Line 12: {

 

do u have msn? perhaps I can add u to chat?

Imran MohammedImran Mohammed

I have gmail and msn but i use gmail more.

Can you give me your mail id?

You can find my mail id from my profile

Imran MohammedImran Mohammed

Hi,

 

Please use the below code

 

 

public class Hclone {
private ID hID;
public HClone()
{
PageReference pg = Apexpages.currentPage();
currentPageURL = pg.getUrl();
hID = pg.getParameters().get('id');
}
public doProcess()
{
//Add the desired fields to your query both for custom object and child objects
Handoff__c[] oppList = [select id, name, stagename, closedate, (select id, title, body from Notes) from Handoff__c where id = :hID];
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);
Handoff__c o = (Handoff__c)s;
toBeClonedNotes.put(o.name, o.notes);
}
insert cloneList;
List<note> notesListToBeInserted = new List<note>();
for(Handoff__c o: (Handoff__c[])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;
Handoff__c[] oppList = [select id, name, stagename, closedate, (select id, title, body from Notes) from Handoff__c 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);
Handoff__c o = (Handoff__c)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(Handoff__c o: (Handoff__c[])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;
}
}

public class Hclone {

 

private ID hID;

public HClone()

{

PageReference pg = Apexpages.currentPage();

currentPageURL = pg.getUrl();

hID = pg.getParameters().get('id');

}

 

public doProcess()

{

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

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

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

Handoff__c o = (Handoff__c)s;

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

 

}

 

insert cloneList;

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

for(Handoff__c o: (Handoff__c[])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;

 

 

Handoff__c[] oppList = [select id, name, stagename, closedate, (select id, title, body from Notes) from Handoff__c 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);

 

Handoff__c o = (Handoff__c)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(Handoff__c o: (Handoff__c[])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;

 

 

 

}

}

 

 

Let me know if you face any issues.

This was selected as the best answer
re_plagasre_plagas

I got this error msg:

 

Error: Compile Error: Duplicate variable: oppList at line 36 column 14

 

Line 36: Handoff__c[] oppList = [select id, name, (select id, title, body from Notes) from Handoff__c where id = :Handoff__c.id];

Imran MohammedImran Mohammed

Please check if oppList is defined anywhere elese.

Post your code, i will see that